@@ -20,37 +20,37 @@ def before
2020
2121 def after
2222 status , headers , bodies = *@app_response
23- return @app_response if Rack ::Utils ::STATUS_WITH_NO_ENTITY_BODY . include? ( status )
2423
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
3226 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
3630
37- begin
38- bodymap = bodies . collect do |body |
39- formatter . call ( body , env )
40- end
31+ private
4132
42- headers = ensure_content_type ( headers )
33+ def build_formatted_response ( status , headers , bodies )
34+ headers = ensure_content_type ( headers )
4335
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
4739 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 )
4845 end
49-
50- response
46+ rescue Grape :: Exceptions :: InvalidFormatter => e
47+ throw :error , status : 500 , message : e . message
5148 end
5249
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
5454
5555 # Set the content type header for the API format if it is not already present.
5656 #
0 commit comments