Contains methods that are useful for working with Accept-style HTTP headers. The MediaType, Charset, Encoding, and Language classes all mixin this module.
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 def join(qvalues) qvalues.map {|k, v| k + (v == 1 ? '' : ";q=#{v}") }.join(', ') 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 def normalize_qvalue(q) (q == 1 || q == 0) && q.is_a?(Float) ? q.to_i : q 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 def parse(header) qvalues = {} header.to_s.split(/,\s*/).each do |part| m = /^([^\s,]+?)(?:\s*;\s*q\s*=\s*(\d+(?:\.\d+)?))?$/.match(part) if m qvalues[m[1]] = normalize_qvalue((m[2] || 1).to_f) else raise "Invalid header value: #{part.inspect}" end end qvalues 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 def parse_media_type(media_type) m = media_type.to_s.match(/^([a-z*]+)\/([a-z*-]+)(?:;([a-z0-9=;]+))?$/) m ? [m[1], m[2], m[3] || ''] : [] end
Parses a string of media type range parameters into a hash of parameters to their respective values.
# File lib/rack/accept/header.rb, line 48 def parse_range_params(params) params.split(';').inject({}) do |m, p| k, v = p.split('=', 2) m[k] = v if v m end end
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 def join(qvalues) qvalues.map {|k, v| k + (v == 1 ? '' : ";q=#{v}") }.join(', ') 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 def normalize_qvalue(q) (q == 1 || q == 0) && q.is_a?(Float) ? q.to_i : q 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 def parse(header) qvalues = {} header.to_s.split(/,\s*/).each do |part| m = /^([^\s,]+?)(?:\s*;\s*q\s*=\s*(\d+(?:\.\d+)?))?$/.match(part) if m qvalues[m[1]] = normalize_qvalue((m[2] || 1).to_f) else raise "Invalid header value: #{part.inspect}" end end qvalues 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 def parse_media_type(media_type) m = media_type.to_s.match(/^([a-z*]+)\/([a-z*-]+)(?:;([a-z0-9=;]+))?$/) m ? [m[1], m[2], m[3] || ''] : [] end
Parses a string of media type range parameters into a hash of parameters to their respective values.
# File lib/rack/accept/header.rb, line 48 def parse_range_params(params) params.split(';').inject({}) do |m, p| k, v = p.split('=', 2) m[k] = v if v m end end