travis-api/lib/travis/api/app/helpers/respond_with.rb
Hiro Asari 3fa96de682 WIP: Atom feed for /repos/:owner_name/:name/builds
See travis-ci/travis-core#82

TODO: Link to indivisual build.
TODO: Add specs.
TODO: Review `#apply?`
2013-11-11 09:03:23 -05:00

59 lines
1.8 KiB
Ruby

require 'travis/api/app'
class Travis::Api::App
module Helpers
# Allows routes to return either hashes or anything Travis::API.data can
# convert (in addition to the return values supported by Sinatra, of
# course). These values will be encoded in JSON.
module RespondWith
include Accept
def respond_with(resource, options = {})
result = respond(resource, options)
result = result.to_json if result && response.content_type =~ /application\/json/
halt result || 404
end
def body(value = nil, options = {}, &block)
value = value.to_json if value.is_a?(Hash)
super(value, &block)
end
private
def respond(resource, options)
resource = apply_service_responder(resource, options)
response = nil
acceptable_formats.find do |accept|
responders(resource, options).find do |const|
responder = const.new(self, resource, options.dup.merge(accept: accept))
response = responder.apply if responder.apply?
end
end
if responders = options[:responders]
responders.each do |klass|
responder = klass.new(self, response, options)
response = responder.apply if responder.apply?
end
end
response || (resource ? error(406) : error(404))
end
def apply_service_responder(resource, options)
responder = Responders::Service.new(self, resource, options)
resource = responder.apply if responder.apply?
resource
end
def responders(resource, options)
[:Atom, :Json, :Image, :Xml, :Plain].map do |name|
Responders.const_get(name)
end
end
end
end
end