Skip to content

Commit db8fdbd

Browse files
author
Daniel Doubrovkine (dB.)
committed
Merge pull request ruby-grape#141 from meh/fix-formatter
Return correct mime-types when passed a version header.
2 parents 6cf671f + 1e0a1c2 commit db8fdbd

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

lib/grape/middleware/formatter.rb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,11 @@ def format_from_header
6161
end
6262

6363
def mime_array
64-
accept = headers['accept']
65-
if accept
66-
accept.gsub(/\b/,'').
67-
scan(/(\w+\/[\w+]+)(?:;[^,]*q=([0-9.]+)[^,]*)?/i).
68-
sort_by{|a| -a[1].to_f}.
69-
map{|a| a[0]}
70-
else
71-
[]
72-
end
64+
accept = headers['accept'] or return []
65+
66+
accept.gsub(/\b/,'').scan(%r((\w+/[\w+.-]+)(?:(?:;[^,]*?)?;\s*q=([\d.]+))?)).sort_by { |_, q| -q.to_f }.map {|mime, _|
67+
mime.sub(%r(vnd\.[^+]+\+), '')
68+
}
7369
end
7470

7571
def after

spec/grape/middleware/formatter_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ def to_xml
9494
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json; abc=2.3; q=1.0,application/xml; q=0.7'})
9595
subject.env['api.format'].should == :json
9696
end
97+
98+
it 'should properly parse headers with vendor and api version' do
99+
subject.call({'PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test-v1+xml'})
100+
subject.env['api.format'].should == :xml
101+
end
97102
end
98103

99104
context 'Content-type' do

0 commit comments

Comments
 (0)