Class: Grape::Middleware::Formatter
- Inherits:
-
Base
- Object
- Base
- Grape::Middleware::Formatter
show all
- Defined in:
- lib/grape/middleware/formatter.rb
Constant Summary
Constants inherited
from Base
Base::CONTENT_TYPES
Instance Attribute Summary
Attributes inherited from Base
#app, #env, #options
Instance Method Summary
collapse
Methods inherited from Base
#call, #call!, #content_type, #content_types, #initialize, #mime_types, #request, #response
Instance Method Details
#after ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/grape/middleware/formatter.rb', line 73
def after
status, , bodies = *@app_response
formatter = Grape::Formatter::Base.formatter_for env['api.format'], options
bodymap = bodies.collect do |body|
formatter.call body, env
end
['Content-Type'] = content_types[env['api.format']] unless ['Content-Type']
Rack::Response.new(bodymap, status, ).to_a
end
|
#before ⇒ Object
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/grape/middleware/formatter.rb', line 19
def before
fmt = format_from_extension || format_from_params || options[:format] || || options[:default_format]
if content_types.key?(fmt)
if !env['rack.input'].nil? and (body = env['rack.input'].read).strip.length != 0
parser = Grape::Parser::Base.parser_for fmt, options
unless parser.nil?
begin
body = parser.call body, env
env['rack.request.form_hash'] = !env['rack.request.form_hash'].nil? ? env['rack.request.form_hash'].merge(body) : body
env['rack.request.form_input'] = env['rack.input']
rescue
end
end
env['rack.input'].rewind
end
env['api.format'] = fmt
else
throw :error, :status => 406, :message => 'The requested format is not supported.'
end
end
|
#default_options ⇒ Object
7
8
9
10
11
12
13
|
# File 'lib/grape/middleware/formatter.rb', line 7
def default_options
{
:default_format => :txt,
:formatters => {},
:parsers => {}
}
end
|
41
42
43
44
45
46
47
48
49
|
# File 'lib/grape/middleware/formatter.rb', line 41
def format_from_extension
parts = request.path.split('.')
if parts.size > 1
extension = parts.last.to_sym
return extension if content_types.key?(extension)
end
nil
end
|
56
57
58
59
60
61
62
63
|
# File 'lib/grape/middleware/formatter.rb', line 56
def
mime_array.each do |t|
if mime_types.key?(t)
return mime_types[t]
end
end
nil
end
|
51
52
53
54
|
# File 'lib/grape/middleware/formatter.rb', line 51
def format_from_params
fmt = Rack::Utils.parse_nested_query(env['QUERY_STRING'])["format"]
fmt ? fmt.to_sym : nil
end
|
15
16
17
|
# File 'lib/grape/middleware/formatter.rb', line 15
def
env.dup.inject({}){|h,(k,v)| h[k.to_s.downcase[5..-1]] = v if k.to_s.downcase.start_with?('http_'); h}
end
|
#mime_array ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/grape/middleware/formatter.rb', line 65
def mime_array
accept = ['accept'] or return []
accept.gsub(/\b/,'').scan(%r((\w+/[\w+.-]+)(?:(?:;[^,]*?)?;\s*q=([\d.]+))?)).sort_by { |_, q| -q.to_f }.map {|mime, _|
mime.sub(%r(vnd\.[^+]+\+), '')
}
end
|