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