diff --git a/lib/travis/api/app/responders/base.rb b/lib/travis/api/app/responders/base.rb index 8849f553..5f463d9c 100644 --- a/lib/travis/api/app/responders/base.rb +++ b/lib/travis/api/app/responders/base.rb @@ -32,6 +32,10 @@ module Travis::Api::App::Responders endpoint.headers end + def apply + endpoint.content_type content_type + end + def apply? resource && acceptable_format? end diff --git a/lib/travis/api/app/responders/image.rb b/lib/travis/api/app/responders/image.rb index 604094cf..d80d28e4 100644 --- a/lib/travis/api/app/responders/image.rb +++ b/lib/travis/api/app/responders/image.rb @@ -13,6 +13,10 @@ module Travis::Api::App::Responders private + def content_type + 'image/png' + end + def filename "#{root}/public/images/result/#{result}.png" end diff --git a/lib/travis/api/app/responders/json.rb b/lib/travis/api/app/responders/json.rb index 13065d0e..15388f6b 100644 --- a/lib/travis/api/app/responders/json.rb +++ b/lib/travis/api/app/responders/json.rb @@ -8,11 +8,17 @@ class Travis::Api::App end def apply + super + halt result.to_json if result end private + def content_type + 'application/json;charset=utf-8' + end + def accepts_log? return true unless resource.is_a?(Log) diff --git a/lib/travis/api/app/responders/plain.rb b/lib/travis/api/app/responders/plain.rb index 2320b1e2..9d045478 100644 --- a/lib/travis/api/app/responders/plain.rb +++ b/lib/travis/api/app/responders/plain.rb @@ -14,17 +14,22 @@ module Travis::Api::App::Responders end def apply + super + filename = resource.id disposition = params[:attachment] ? 'attachment' : 'inline' headers['Content-Disposition'] = %(#{disposition}; filename="#{filename}") - endpoint.content_type 'text/plain' halt(params[:deansi] ? clear_ansi(resource.content) : resource.content) end private + def content_type + 'text/plain' + end + def clear_ansi(content) content.gsub(/\r\r/, "\r") .gsub(/^.*\r(?!$)/, '') diff --git a/lib/travis/api/app/responders/xml.rb b/lib/travis/api/app/responders/xml.rb index 9fa43fcf..e2137f4a 100644 --- a/lib/travis/api/app/responders/xml.rb +++ b/lib/travis/api/app/responders/xml.rb @@ -20,11 +20,17 @@ module Travis::Api::App::Responders end def apply + super + halt TEMPLATE % data end private + def content_type + 'application/xml;charset=utf-8' + end + def data { name: resource.slug, diff --git a/spec/unit/responders/json_spec.rb b/spec/unit/responders/json_spec.rb index eb2ffa33..37146d58 100644 --- a/spec/unit/responders/json_spec.rb +++ b/spec/unit/responders/json_spec.rb @@ -6,7 +6,7 @@ module Travis::Api::App::Responders end let(:request) { stub 'request', params: {} } - let(:endpoint) { stub 'endpoint', request: request } + let(:endpoint) { stub 'endpoint', request: request, content_type: nil } let(:resource) { stub 'resource' } let(:accept) { stub 'accept entry', version: '2', params: {} } let(:options) { { :accept => accept} }