Class: Grape::Middleware::Formatter
- Inherits:
-
Base
- Object
- Base
- Grape::Middleware::Formatter
show all
- Defined in:
- lib/grape/middleware/formatter.rb
Instance Attribute Summary
Attributes inherited from Base
#app, #env, #options
Instance Method Summary
collapse
Methods inherited from Base
#call, #call!, #content_type, #content_type_for, #content_types, #initialize, #mime_types, #response
Instance Method Details
#after ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/grape/middleware/formatter.rb', line 19
def after
status, , bodies = *@app_response
if bodies.is_a?(Grape::Util::FileResponse)
= ensure_content_type()
response =
Rack::Response.new([], status, ) do |resp|
resp.body = bodies.file
end
else
api_format = mime_types[[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
= ensure_content_type()
response = Rack::Response.new(bodymap, status, )
rescue Grape::Exceptions::InvalidFormatter => e
throw :error, status: 500, message: e.message
end
end
response
end
|
#before ⇒ Object
14
15
16
17
|
# File 'lib/grape/middleware/formatter.rb', line 14
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_options
{
default_format: :txt,
formatters: {},
parsers: {}
}
end
|