allow returning a symbol as a result from services, set status based on the symbol
This commit is contained in:
parent
a636e911bd
commit
05494f4acf
|
@ -8,14 +8,16 @@ class Travis::Api::App
|
|||
module RespondWith
|
||||
include Accept
|
||||
|
||||
STATUS = {
|
||||
success: 200,
|
||||
not_found: 404
|
||||
}
|
||||
|
||||
def respond_with(resource, options = {})
|
||||
result = respond(resource, options)
|
||||
if result && response.content_type =~ /application\/json/
|
||||
if !params[:pretty].nil? && (params[:pretty].downcase == 'true' || params[:pretty].to_i > 0)
|
||||
result = JSON.pretty_generate(result)
|
||||
else
|
||||
result = result.to_json
|
||||
end
|
||||
status STATUS[result[:result]] if result.is_a?(Hash) && result[:result].is_a?(Symbol)
|
||||
result = prettify_result? ? JSON.pretty_generate(result) : result.to_json
|
||||
end
|
||||
halt result || 404
|
||||
end
|
||||
|
@ -48,6 +50,10 @@ class Travis::Api::App
|
|||
response || (resource ? error(406) : error(404))
|
||||
end
|
||||
|
||||
def prettify_result?
|
||||
!params[:pretty].nil? && (params[:pretty].downcase == 'true' || params[:pretty].to_i > 0)
|
||||
end
|
||||
|
||||
def apply_service_responder(resource, options)
|
||||
responder = Responders::Service.new(self, resource, options)
|
||||
resource = responder.apply if responder.apply?
|
||||
|
|
|
@ -56,7 +56,7 @@ module Travis::Api
|
|||
# If it's nil we also pass it but yield not_found.
|
||||
def normalize(result)
|
||||
case result
|
||||
when String, true, false
|
||||
when Symbol, String, true, false
|
||||
{ result: result }
|
||||
else
|
||||
result
|
||||
|
|
|
@ -21,12 +21,12 @@ class Travis::Api::App
|
|||
Metriks.meter('api.request.create').mark
|
||||
Travis::Sidekiq::BuildRequest.perform_async(type: 'api', payload: payload, credentials: {})
|
||||
messages << { notice: 'Build request scheduled.' }
|
||||
true
|
||||
:success
|
||||
end
|
||||
|
||||
def not_found
|
||||
messages << { error: "Repository #{slug} not found." }
|
||||
false
|
||||
:not_found
|
||||
end
|
||||
|
||||
def active?
|
||||
|
|
|
@ -25,7 +25,7 @@ describe Travis::Api::App::Endpoint::Requests do
|
|||
end
|
||||
|
||||
it 'includes a notice' do
|
||||
expect(response.body).to eq '{"result":false,"flash":[{"error":"Repository owner/name not found."}]}'
|
||||
expect(response.body).to eq '{"result":"not_found","flash":[{"error":"Repository owner/name not found."}]}'
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -41,7 +41,7 @@ describe Travis::Api::App::Endpoint::Requests do
|
|||
end
|
||||
|
||||
it 'includes a notice' do
|
||||
expect(response.body).to eq '{"result":true,"flash":[{"notice":"Build request scheduled."}]}'
|
||||
expect(response.body).to eq '{"result":"success","flash":[{"notice":"Build request scheduled."}]}'
|
||||
end
|
||||
|
||||
it 'schedules the build request' do
|
||||
|
|
Loading…
Reference in New Issue
Block a user