class Rack::AcceptHeaders::MediaType
Represents an HTTP Accept header according to the HTTP 1.1 specification, and provides several convenience methods for determining acceptable media types.
Attributes
extensions[RW]
Public Class Methods
new(header)
click to toggle source
# File lib/rack/accept_headers/media_type.rb, line 51 def initialize(header) @extensions = {} @qvalues = {} header.to_s.split(',').each do |part| type, subtype, raw_params = parse_media_type(part) raise InvalidHeader, "Invalid header value: #{part.inspect}" if !type || !subtype media_type = "#{type}/#{subtype}" params = parse_range_params(raw_params) @extensions[media_type] = params @qvalues[media_type] = normalize_qvalue(params['q']).to_f end end
Public Instance Methods
matches(media_type)
click to toggle source
Returns an array of media types from this header that match the given media_type
, ordered by precedence.
# File lib/rack/accept_headers/media_type.rb, line 27 def matches(media_type) type, subtype, params = parse_media_type(media_type) values.select {|v| if v == media_type || v == '*/*' true else t, s, p = parse_media_type(v) t == type && (s == '*' || s == subtype) && (p == '' || params_match?(params, p)) end }.sort_by {|v| # Most specific gets precedence. v.length }.reverse end
name()
click to toggle source
The name of this header.
# File lib/rack/accept_headers/media_type.rb, line 13 def name 'Accept' end
params(media_type)
click to toggle source
Returns a params hash for the media type that matches
# File lib/rack/accept_headers/media_type.rb, line 43 def params(media_type) return {} unless media_type key = matches(media_type).first @extensions[key] || {} end
qvalue(media_type)
click to toggle source
Determines the quality factor (qvalue) of the given media_type
.
# File lib/rack/accept_headers/media_type.rb, line 18 def qvalue(media_type) return 1 if @qvalues.empty? m = matches(media_type) return 0 if m.empty? normalize_qvalue(@qvalues[m.first]) end
Private Instance Methods
params_match?(params, match)
click to toggle source
Returns true if all parameters and values in match
are also present in params
.
# File lib/rack/accept_headers/media_type.rb, line 67 def params_match?(params, match) return true if params == match parsed = parse_range_params(params) parsed == parsed.merge(parse_range_params(match)) end