travis-api/lib/travis/api/app/helpers/json_renderer.rb
2012-07-27 15:55:57 +02:00

33 lines
975 B
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 JsonRenderer
def respond_with(resource, options = {})
halt render_json(resource, options)
end
def body(value = nil, &block)
value = render_json(value) if value
super(value, &block)
end
private
def render_json(resource, options = {})
options[:version] ||= 'v2' # TODO: Content negotiation
options[:params] ||= params
builder = Travis::Api.builder(resource, options)
resource = builder.new(resource, options[:params]).data.to_json if builder
resource = resource.to_json if resource.is_a? Hash
resource
end
end
end
end