Allow to pass additional responders to respond_with
This commit is contained in:
parent
01356df26f
commit
1340fdb316
|
@ -10,8 +10,8 @@ class Travis::Api::App
|
|||
|
||||
def respond_with(resource, options = {})
|
||||
result = respond(resource, options)
|
||||
result = result ? result.to_json : 404
|
||||
halt result
|
||||
result = result.to_json if result && response.content_type =~ /application\/json/
|
||||
halt result || 404
|
||||
end
|
||||
|
||||
def body(value = nil, options = {}, &block)
|
||||
|
@ -24,10 +24,18 @@ class Travis::Api::App
|
|||
def respond(resource, options)
|
||||
resource = apply_service_responder(resource, options)
|
||||
|
||||
response = acceptable_formats.find do |accept|
|
||||
response = nil
|
||||
acceptable_formats.find do |accept|
|
||||
responders(resource, options).find do |const|
|
||||
responder = const.new(self, resource, options.dup.merge(accept: accept))
|
||||
responder.apply if responder.apply?
|
||||
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
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ module Travis::Api::App::Responders
|
|||
headers['Pragma'] = "no-cache"
|
||||
headers['Expires'] = Time.now.utc.httpdate
|
||||
headers['Content-Disposition'] = %(inline; filename="#{File.basename(filename)}")
|
||||
halt send_file(filename, type: :png, last_modified: last_modified)
|
||||
send_file(filename, type: :png, last_modified: last_modified)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -10,7 +10,7 @@ class Travis::Api::App
|
|||
def apply
|
||||
super
|
||||
|
||||
halt result.to_json if result
|
||||
result
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -21,7 +21,7 @@ module Travis::Api::App::Responders
|
|||
|
||||
headers['Content-Disposition'] = %(#{disposition}; filename="#{filename}")
|
||||
|
||||
halt(params[:deansi] ? clear_ansi(resource.content) : resource.content)
|
||||
params[:deansi] ? clear_ansi(resource.content) : resource.content
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -22,7 +22,7 @@ module Travis::Api::App::Responders
|
|||
def apply
|
||||
super
|
||||
|
||||
halt TEMPLATE % data
|
||||
TEMPLATE % data
|
||||
end
|
||||
|
||||
private
|
||||
|
|
30
spec/integration/responders_spec.rb
Normal file
30
spec/integration/responders_spec.rb
Normal file
|
@ -0,0 +1,30 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe 'App' do
|
||||
before do
|
||||
FactoryGirl.create(:test, :number => '3.1', :queue => 'builds.common')
|
||||
|
||||
responder = Class.new(Travis::Api::App::Responders::Base) do
|
||||
def apply?
|
||||
true
|
||||
end
|
||||
|
||||
def apply
|
||||
resource[:extra] = 'moar!'
|
||||
|
||||
resource
|
||||
end
|
||||
end
|
||||
|
||||
add_endpoint '/foo' do
|
||||
get '/hash' do
|
||||
respond_with({ foo: 'bar' }, responders: [responder])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it '' do
|
||||
response = get '/foo/hash', {}, 'HTTP_ACCEPT' => 'application/json'
|
||||
JSON.parse(response.body).should == { 'foo' => 'bar', 'extra' => 'moar!' }
|
||||
end
|
||||
end
|
|
@ -23,8 +23,7 @@ module Travis::Api::App::Responders
|
|||
let(:resource) { { foo: 'bar' } }
|
||||
|
||||
it 'returns resource converted to_json' do
|
||||
json.expects(:halt).with({ foo: 'bar' }.to_json)
|
||||
json.apply
|
||||
json.apply.should == { foo: 'bar' }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -46,8 +45,7 @@ module Travis::Api::App::Responders
|
|||
end
|
||||
|
||||
it 'returns proper data converted to json' do
|
||||
json.expects(:halt).with({ foo: 'bar' }.to_json)
|
||||
json.apply
|
||||
json.apply.should == { foo: 'bar' }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue
Block a user