Class: Grape::Middleware::Formatter
- Defined in:
- lib/grape/middleware/formatter.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
Methods inherited from Base
#call, #call!, #content_type, #content_type_for, #content_types, #initialize, #mime_types, #response
Constructor Details
This class inherits a constructor from Grape::Middleware::Base
Instance Method Details
#after ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/grape/middleware/formatter.rb', line 26 def after status, headers, bodies = *@app_response # allow content-type to be explicitly overwritten api_format = mime_types[headers[Grape::Http::Headers::CONTENT_TYPE]] || env['api.format'] formatter = Grape::Formatter::Base.formatter_for api_format, begin 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. end 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 |
#before ⇒ Object
21 22 23 24 |
# File 'lib/grape/middleware/formatter.rb', line 21 def before negotiate_content_type read_body_input end |
#default_options ⇒ Object
6 7 8 9 10 11 12 |
# File 'lib/grape/middleware/formatter.rb', line 6 def { default_format: :txt, formatters: {}, parsers: {} } end |
#headers ⇒ Object
14 15 16 17 18 19 |
# File 'lib/grape/middleware/formatter.rb', line 14 def headers env.dup.inject({}) do |h, (k, v)| h[k.to_s.downcase[5..-1]] = v if k.to_s.downcase.start_with?('http_') h end end |