diff --git a/lib/travis/api/app/helpers/respond_with.rb b/lib/travis/api/app/helpers/respond_with.rb index 55d38ed7..746152fb 100644 --- a/lib/travis/api/app/helpers/respond_with.rb +++ b/lib/travis/api/app/helpers/respond_with.rb @@ -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? diff --git a/lib/travis/api/app/responders/service.rb b/lib/travis/api/app/responders/service.rb index 55316b27..57942234 100644 --- a/lib/travis/api/app/responders/service.rb +++ b/lib/travis/api/app/responders/service.rb @@ -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 diff --git a/lib/travis/api/app/services/schedule_request.rb b/lib/travis/api/app/services/schedule_request.rb index 341d968b..80fd9e20 100644 --- a/lib/travis/api/app/services/schedule_request.rb +++ b/lib/travis/api/app/services/schedule_request.rb @@ -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? diff --git a/spec/unit/endpoint/requests_spec.rb b/spec/unit/endpoint/requests_spec.rb index 03ec98f8..4e04e82d 100644 --- a/spec/unit/endpoint/requests_spec.rb +++ b/spec/unit/endpoint/requests_spec.rb @@ -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