module Rack::AcceptHeaders::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
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_headers/header.rb, line 33 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_headers/header.rb, line 61 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_headers/header.rb, line 12 def parse(header) qvalues = {} header.to_s.split(',').each do |part| m = /^\s*([^\s,]+?)(?:\s*;\s*q\s*=\s*(\d+(?:\.\d+)?))?$/.match(part) if m qvalues[m[1].downcase] = normalize_qvalue((m[2] || 1).to_f) else raise InvalidHeader, "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_headers/header.rb, line 42 def parse_media_type(media_type) m = media_type.to_s.match(/^\s*([a-zA-Z*]+)\s*\/\s*([a-zA-Z0-9*\-.+]+)\s*(?:;(.+))?$/) m ? [m[1].downcase, m[2].downcase, m[3] || ''] : [] end
Parses a string of media type range parameters into a hash of parameters to their respective values.
# File lib/rack/accept_headers/header.rb, line 50 def parse_range_params(params) params.split(';').inject({'q' => '1'}) do |m, p| k, v = p.split('=', 2) m[k.strip] = v.strip if v m end end
Private Instance Methods
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_headers/header.rb, line 33 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_headers/header.rb, line 61 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_headers/header.rb, line 12 def parse(header) qvalues = {} header.to_s.split(',').each do |part| m = /^\s*([^\s,]+?)(?:\s*;\s*q\s*=\s*(\d+(?:\.\d+)?))?$/.match(part) if m qvalues[m[1].downcase] = normalize_qvalue((m[2] || 1).to_f) else raise InvalidHeader, "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_headers/header.rb, line 42 def parse_media_type(media_type) m = media_type.to_s.match(/^\s*([a-zA-Z*]+)\s*\/\s*([a-zA-Z0-9*\-.+]+)\s*(?:;(.+))?$/) m ? [m[1].downcase, m[2].downcase, m[3] || ''] : [] end
Parses a string of media type range parameters into a hash of parameters to their respective values.
# File lib/rack/accept_headers/header.rb, line 50 def parse_range_params(params) params.split(';').inject({'q' => '1'}) do |m, p| k, v = p.split('=', 2) m[k.strip] = v.strip if v m end end