@@ -20,37 +20,37 @@ def before
20
20
21
21
def after
22
22
status , headers , bodies = *@app_response
23
- return @app_response if Rack ::Utils ::STATUS_WITH_NO_ENTITY_BODY . include? ( status )
24
23
25
- if bodies . is_a? ( Grape ::Util ::FileResponse )
26
- headers = ensure_content_type ( headers )
27
-
28
- response =
29
- Rack ::Response . new ( [ ] , status , headers ) do |resp |
30
- resp . body = bodies . file
31
- end
24
+ if Rack ::Utils ::STATUS_WITH_NO_ENTITY_BODY . include? ( status )
25
+ @app_response
32
26
else
33
- # Allow content-type to be explicitly overwritten
34
- api_format = mime_types [ headers [ Grape :: Http :: Headers :: CONTENT_TYPE ] ] || env [ Grape :: Env :: API_FORMAT ]
35
- formatter = Grape :: Formatter :: Base . formatter_for ( api_format , options )
27
+ build_formatted_response ( status , headers , bodies )
28
+ end
29
+ end
36
30
37
- begin
38
- bodymap = bodies . collect do |body |
39
- formatter . call ( body , env )
40
- end
31
+ private
41
32
42
- headers = ensure_content_type ( headers )
33
+ def build_formatted_response ( status , headers , bodies )
34
+ headers = ensure_content_type ( headers )
43
35
44
- response = Rack :: Response . new ( bodymap , status , headers )
45
- rescue Grape :: Exceptions :: InvalidFormatter => e
46
- throw :error , status : 500 , message : e . message
36
+ if bodies . is_a? ( Grape :: Util :: FileResponse )
37
+ Rack :: Response . new ( [ ] , status , headers ) do | resp |
38
+ resp . body = bodies . file
47
39
end
40
+ else
41
+ # Allow content-type to be explicitly overwritten
42
+ formatter = fetch_formatter ( headers , options )
43
+ bodymap = bodies . collect { |body | formatter . call ( body , env ) }
44
+ Rack ::Response . new ( bodymap , status , headers )
48
45
end
49
-
50
- response
46
+ rescue Grape :: Exceptions :: InvalidFormatter => e
47
+ throw :error , status : 500 , message : e . message
51
48
end
52
49
53
- private
50
+ def fetch_formatter ( headers , options )
51
+ api_format = mime_types [ headers [ Grape ::Http ::Headers ::CONTENT_TYPE ] ] || env [ Grape ::Env ::API_FORMAT ]
52
+ Grape ::Formatter ::Base . formatter_for ( api_format , options )
53
+ end
54
54
55
55
# Set the content type header for the API format if it is not already present.
56
56
#
0 commit comments