lib/grape/middleware/formatter.rb in grape-0.11.0 vs lib/grape/middleware/formatter.rb in grape-0.12.0
- old
+ new
@@ -24,21 +24,25 @@
end
def after
status, headers, bodies = *@app_response
# allow content-type to be explicitly overwritten
- api_format = mime_types[headers['Content-Type']] || env['api.format']
+ api_format = mime_types[headers[Grape::Http::Headers::CONTENT_TYPE]] || env['api.format']
formatter = Grape::Formatter::Base.formatter_for api_format, options
begin
- bodymap = bodies.collect do |body|
- formatter.call body, env
- end
+ bodymap = if bodies.respond_to?(:collect)
+ bodies.collect do |body|
+ formatter.call body, env
+ end
+ else
+ bodies
+ end
rescue Grape::Exceptions::InvalidFormatter => e
throw :error, status: 500, message: e.message
end
- headers['Content-Type'] = content_type_for(env['api.format']) unless headers['Content-Type']
- Rack::Response.new(bodymap, status, headers).to_a
+ headers[Grape::Http::Headers::CONTENT_TYPE] = content_type_for(env['api.format']) unless headers[Grape::Http::Headers::CONTENT_TYPE]
+ Rack::Response.new(bodymap, status, headers)
end
private
def request
@@ -48,11 +52,11 @@
# store read input in env['api.request.input']
def read_body_input
if (request.post? || request.put? || request.patch? || request.delete?) &&
(!request.form_data? || !request.media_type) &&
(!request.parseable_data?) &&
- (request.content_length.to_i > 0 || request.env['HTTP_TRANSFER_ENCODING'] == 'chunked')
+ (request.content_length.to_i > 0 || request.env[Grape::Http::Headers::HTTP_TRANSFER_ENCODING] == 'chunked')
if (input = env['rack.input'])
input.rewind
body = env['api.request.input'] = input.read
begin
@@ -113,11 +117,11 @@
end
nil
end
def format_from_params
- fmt = Rack::Utils.parse_nested_query(env['QUERY_STRING'])['format']
+ fmt = Rack::Utils.parse_nested_query(env[Grape::Http::Headers::QUERY_STRING])[Grape::Http::Headers::FORMAT]
# avoid symbol memory leak on an unknown format
return fmt.to_sym if content_type_for(fmt)
fmt
end
@@ -127,10 +131,10 @@
end
nil
end
def mime_array
- accept = headers['accept']
+ accept = headers[Grape::Http::Headers::ACCEPT]
return [] unless accept
accept_into_mime_and_quality = %r{
(
\w+/[\w+.-]+) # eg application/vnd.example.myformat+xml