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 = {})
|
def respond_with(resource, options = {})
|
||||||
result = respond(resource, options)
|
result = respond(resource, options)
|
||||||
result = result ? result.to_json : 404
|
result = result.to_json if result && response.content_type =~ /application\/json/
|
||||||
halt result
|
halt result || 404
|
||||||
end
|
end
|
||||||
|
|
||||||
def body(value = nil, options = {}, &block)
|
def body(value = nil, options = {}, &block)
|
||||||
|
@ -24,10 +24,18 @@ class Travis::Api::App
|
||||||
def respond(resource, options)
|
def respond(resource, options)
|
||||||
resource = apply_service_responder(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|
|
responders(resource, options).find do |const|
|
||||||
responder = const.new(self, resource, options.dup.merge(accept: accept))
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ module Travis::Api::App::Responders
|
||||||
headers['Pragma'] = "no-cache"
|
headers['Pragma'] = "no-cache"
|
||||||
headers['Expires'] = Time.now.utc.httpdate
|
headers['Expires'] = Time.now.utc.httpdate
|
||||||
headers['Content-Disposition'] = %(inline; filename="#{File.basename(filename)}")
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -10,7 +10,7 @@ class Travis::Api::App
|
||||||
def apply
|
def apply
|
||||||
super
|
super
|
||||||
|
|
||||||
halt result.to_json if result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -21,7 +21,7 @@ module Travis::Api::App::Responders
|
||||||
|
|
||||||
headers['Content-Disposition'] = %(#{disposition}; filename="#{filename}")
|
headers['Content-Disposition'] = %(#{disposition}; filename="#{filename}")
|
||||||
|
|
||||||
halt(params[:deansi] ? clear_ansi(resource.content) : resource.content)
|
params[:deansi] ? clear_ansi(resource.content) : resource.content
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -22,7 +22,7 @@ module Travis::Api::App::Responders
|
||||||
def apply
|
def apply
|
||||||
super
|
super
|
||||||
|
|
||||||
halt TEMPLATE % data
|
TEMPLATE % data
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
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' } }
|
let(:resource) { { foo: 'bar' } }
|
||||||
|
|
||||||
it 'returns resource converted to_json' do
|
it 'returns resource converted to_json' do
|
||||||
json.expects(:halt).with({ foo: 'bar' }.to_json)
|
json.apply.should == { foo: 'bar' }
|
||||||
json.apply
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -46,8 +45,7 @@ module Travis::Api::App::Responders
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'returns proper data converted to json' do
|
it 'returns proper data converted to json' do
|
||||||
json.expects(:halt).with({ foo: 'bar' }.to_json)
|
json.apply.should == { foo: 'bar' }
|
||||||
json.apply
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user