Class: Rack::Accept::MediaType

Inherits:
Object
  • Object
show all
Includes:
Header
Defined in:
lib/rack/accept/media_type.rb

Overview

Represents an HTTP Accept header according to the HTTP 1.1 specification, and provides several convenience methods for determining acceptable media types.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1

Instance Attribute Summary

Attributes included from Header::PublicInstanceMethods

#qvalues

Instance Method Summary collapse

Methods included from Header

join, normalize_qvalue, parse, parse_media_type, parse_range_params

Methods included from Header::PublicInstanceMethods

#accept?, #best_of, #sort, #sort_with_qvalues, #to_s, #value, #values

Instance Method Details

#matches(media_type) ⇒ Object

Returns an array of media types from this header that match the given media_type, ordered by precedence.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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 ⇒ Object

The name of this header.



11
12
13
# File 'lib/rack/accept/media_type.rb', line 11

def name
  'Accept'
end

#qvalue(media_type) ⇒ Object

Determines the quality factor (qvalue) of the given media_type.



16
17
18
19
20
21
# 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