| Class | Rack::Accept::MediaType |
| In: |
lib/rack/accept/media_type.rb
|
| Parent: | Object |
Represents an HTTP Accept header according to the HTTP 1.1 specification, and provides several convenience methods for determining acceptable media types.
# File lib/rack/accept/media_type.rb, line 42
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
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
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