module Rack::Accept::Header

Contains methods that are useful for working with Accept-style HTTP headers. The MediaType, Charset, Encoding, and Language classes all mixin this module.

Public Class Methods

join(qvalues) click to toggle source

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
31 def join(qvalues)
32   qvalues.map {|k, v| k + (v == 1 ? '' : ";q=#{v}") }.join(', ')
33 end
normalize_qvalue(q) click to toggle source

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
59 def normalize_qvalue(q)
60   (q == 1 || q == 0) && q.is_a?(Float) ? q.to_i : q
61 end
parse(header) click to toggle source

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
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].downcase] = 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
parse_media_type(media_type) click to toggle source

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
40 def parse_media_type(media_type)
41   m = media_type.to_s.match(/^([a-z*]+)\/([a-z0-9*\-.+]+)(?:;([a-z0-9=;]+))?$/)
42   m ? [m[1], m[2], m[3] || ''] : []
43 end
parse_range_params(params) click to toggle source

Parses a string of media type range parameters into a hash of parameters to their respective values.

   # File lib/rack/accept/header.rb
48 def parse_range_params(params)
49   params.split(';').inject({}) do |m, p|
50     k, v = p.split('=', 2)
51     m[k] = v if v
52     m
53   end
54 end

Private Instance Methods

join(qvalues) click to toggle source

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
31 def join(qvalues)
32   qvalues.map {|k, v| k + (v == 1 ? '' : ";q=#{v}") }.join(', ')
33 end
normalize_qvalue(q) click to toggle source

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
59 def normalize_qvalue(q)
60   (q == 1 || q == 0) && q.is_a?(Float) ? q.to_i : q
61 end
parse(header) click to toggle source

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
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].downcase] = 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
parse_media_type(media_type) click to toggle source

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
40 def parse_media_type(media_type)
41   m = media_type.to_s.match(/^([a-z*]+)\/([a-z0-9*\-.+]+)(?:;([a-z0-9=;]+))?$/)
42   m ? [m[1], m[2], m[3] || ''] : []
43 end
parse_range_params(params) click to toggle source

Parses a string of media type range parameters into a hash of parameters to their respective values.

   # File lib/rack/accept/header.rb
48 def parse_range_params(params)
49   params.split(';').inject({}) do |m, p|
50     k, v = p.split('=', 2)
51     m[k] = v if v
52     m
53   end
54 end