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 42 def initialize(header) 43 # Strip accept-extension for now. We may want to do something with this 44 # later if people actually start to use it. 45 header = header.to_s.split(/,\s*/).map {|part| 46 part.sub(/(;\s*q\s*=\s*[\d.]+).*$/, '\1') 47 }.join(', ') 48 49 super(header) 50 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 25 def matches(media_type) 26 type, subtype, params = parse_media_type(media_type) 27 values.select {|v| 28 if v == media_type || v == '*/*' 29 true 30 else 31 t, s, p = parse_media_type(v) 32 t == type && (s == '*' || s == subtype) && (p == '' || params_match?(params, p)) 33 end 34 }.sort_by {|v| 35 # Most specific gets precedence. 36 v.length 37 }.reverse 38 end
name()
click to toggle source
The name of this header.
# File lib/rack/accept/media_type.rb 11 def name 12 'Accept' 13 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 16 def qvalue(media_type) 17 return 1 if @qvalues.empty? 18 m = matches(media_type) 19 return 0 if m.empty? 20 normalize_qvalue(@qvalues[m.first]) 21 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 54 def params_match?(params, match) 55 return true if params == match 56 parsed = parse_range_params(params) 57 parsed == parsed.merge(parse_range_params(match)) 58 end