class Rack::Accept::MediaType
Represents an HTTP Accept header according to the HTTP 1.1 specification, and provides several convenience methods for determining acceptable media types.
Public Class Methods
new(header)
click to toggle source
Calls superclass method
# File lib/rack/accept/media_type.rb, line 42 def initialize(header) # Strip accept-extension for now. We may want to do something with this # later if people actually start to use it. header = header.to_s.split(/,\s*/).map {|part| part.sub(/(;\s*q\s*=\s*[\d.]+).*$/, '\1') }.join(', ') super(header) 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/media_type.rb, line 25 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/media_type.rb, line 11 def name 'Accept' end
qvalue(media_type)
click to toggle source
Determines the quality factor (qvalue) of the given
media_type
.
# File lib/rack/accept/media_type.rb, line 16 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/media_type.rb, line 54 def params_match?(params, match) return true if params == match parsed = parse_range_params(params) parsed == parsed.merge(parse_range_params(match)) end