Module: Rack::Accept::Header
- Includes:
- PublicInstanceMethods
- Defined in:
- lib/rack/accept/header.rb
Overview
Contains methods that are useful for working with Accept-style HTTP headers. The MediaType, Charset, Encoding, and Language classes all mixin this module.
Defined Under Namespace
Modules: PublicInstanceMethods
Instance Attribute Summary
Attributes included from PublicInstanceMethods
Class Method Summary collapse
-
.join(qvalues) ⇒ Object
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).
-
.normalize_qvalue(q) ⇒ Object
Converts 1.0 and 0.0 qvalues to 1 and 0 respectively.
-
.parse(header) ⇒ Object
Parses the value of an Accept-style request header into a hash of acceptable values and their respective quality factors (qvalues).
-
.parse_media_type(media_type) ⇒ Object
Parses a media type string into its relevant pieces.
-
.parse_range_params(params) ⇒ Object
Parses a string of media type range parameters into a hash of parameters to their respective values.
Methods included from PublicInstanceMethods
#accept?, #best_of, #initialize, #name, #qvalue, #sort, #sort_with_qvalues, #to_s, #value, #values
Class Method Details
.join(qvalues) ⇒ Object
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.
31 32 33 |
# File 'lib/rack/accept/header.rb', line 31 def join(qvalues) qvalues.map {|k, v| k + (v == 1 ? '' : ";q=#{v}") }.join(', ') end |
.normalize_qvalue(q) ⇒ Object
Converts 1.0 and 0.0 qvalues to 1 and 0 respectively. Used to maintain consistency across qvalue methods.
59 60 61 |
# 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 |
.parse(header) ⇒ Object
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.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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].downcase] = normalize_qvalue((m[2] || 1).to_f) else raise "Invalid header value: #{part.inspect}" end end qvalues end |
.parse_media_type(media_type) ⇒ Object
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.
40 41 42 43 |
# File 'lib/rack/accept/header.rb', line 40 def parse_media_type(media_type) m = media_type.to_s.match(/^([a-z*]+)\/([a-z0-9*\-.+]+)(?:;([a-z0-9=;]+))?$/) m ? [m[1], m[2], m[3] || ''] : [] end |
.parse_range_params(params) ⇒ Object
Parses a string of media type range parameters into a hash of parameters to their respective values.
48 49 50 51 52 53 54 |
# 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 |