Skip to content

Commit bb7f7ac

Browse files
committed
extract accept-extensions from the header. #12
1 parent 031dd2e commit bb7f7ac

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

lib/rack/accept/media_type.rb

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Rack::Accept
77
class MediaType
88
include Header
99

10+
attr_accessor :extensions
11+
1012
# The name of this header.
1113
def name
1214
'Accept'
@@ -40,13 +42,24 @@ def matches(media_type)
4042
private
4143

4244
def initialize(header)
43-
# Strip accept-extension for now. We may want to do something with this
44-
# later if people actually start to use it.
45-
header = header.to_s.split(/,\s*/).map {|part|
46-
part.sub(/(;\s*q\s*=\s*[\d.]+).*$/, '\1')
47-
}.join(', ')
45+
@extensions = {}
46+
header.to_s.split(',').each do |raw_media_type|
47+
params = { 'q' => 1.0 }
48+
parts = raw_media_type.split(';')
49+
media_type = parts.shift.strip.downcase
50+
parts.each do |part|
51+
pair = part.split('=', 2)
52+
pair[0].strip.downcase
53+
pair[1].strip
54+
params[pair[0]] = pair[0] == 'q' ? normalize_qvalue(pair[1]).to_f : pair[1]
55+
end
56+
@extensions[media_type] = params
57+
end
4858

49-
super(header)
59+
@qvalues = {}
60+
@extensions.each do |k, v|
61+
@qvalues[k] = v['q']
62+
end
5063
end
5164

5265
# Returns true if all parameters and values in +match+ are also present in

0 commit comments

Comments
 (0)