| Module | Rack::Accept::Header |
| In: |
lib/rack/accept/header.rb
|
Returns a string suitable for use as the value of an Accept-style HTTP header from the map of acceptable values to their respective quality factors (qvalues). The parse method may be used on the resulting string to obtain a hash that is the equivalent of the one provided.
# File lib/rack/accept/header.rb, line 31
31: def join(qvalues)
32: qvalues.map {|k, v| k + (v == 1 ? '' : ";q=#{v}") }.join(', ')
33: end
Converts 1.0 and 0.0 qvalues to 1 and 0 respectively. Used to maintain consistency across qvalue methods.
# File lib/rack/accept/header.rb, line 59
59: def normalize_qvalue(q)
60: (q == 1 || q == 0) && q.is_a?(Float) ? q.to_i : q
61: end
Parses the value of an Accept-style request header into a hash of acceptable values and their respective quality factors (qvalues). The join method may be used on the resulting hash to obtain a header string that is the semantic equivalent of the one provided.
# File lib/rack/accept/header.rb, line 10
10: def parse(header)
11: qvalues = {}
12:
13: header.to_s.split(/,\s*/).each do |part|
14: m = /^([^\s,]+?)(?:\s*;\s*q\s*=\s*(\d+(?:\.\d+)?))?$/.match(part)
15:
16: if m
17: qvalues[m[1]] = normalize_qvalue((m[2] || 1).to_f)
18: else
19: raise "Invalid header value: #{part.inspect}"
20: end
21: end
22:
23: qvalues
24: end
Parses a media type string into its relevant pieces. The return value will be an array with three values: 1) the content type, 2) the content subtype, and 3) the media type parameters. An empty array is returned if no match can be made.
# File lib/rack/accept/header.rb, line 40
40: def parse_media_type(media_type)
41: m = media_type.to_s.match(/^([a-z*]+)\/([a-z*-]+)(?:;([a-z0-9=;]+))?$/)
42: m ? [m[1], m[2], m[3] || ''] : []
43: end