class Scorched::Accept::AcceptHeader

Public Class Methods

new(raw_str = nil) click to toggle source
   # File lib/scorched/accept/accept_header.rb
11 def initialize(raw_str = nil)
12   @raw_str = (raw_str && not(raw_str =~ /^\s*$/)) ? raw_str : '*/*'
13 end

Public Instance Methods

acceptable?(media_type) click to toggle source

Returns true if the given media type is acceptable.

   # File lib/scorched/accept/accept_header.rb
16 def acceptable?(media_type)
17   !matches(media_type).empty?
18 end
best_of(*media_types) click to toggle source

Of the media types given, returns the most appropriate. If none are appropriate, returns nil. If all media types are of equal appropriatness, or multiple media types are equal best, the media type that was listed highest in argument list is returned.

   # File lib/scorched/accept/accept_header.rb
36 def best_of(*media_types)
37   media_types.min_by { |m| rank(m) }
38 end
each(*args, &block) click to toggle source

Enumerabilitisationing

   # File lib/scorched/accept/accept_header.rb
60 def each(*args, &block)
61   ordered_values.each(*args, &block)
62 end
length() click to toggle source

Returns the number of media range definitions in the accept header of the request.

   # File lib/scorched/accept/accept_header.rb
65 def length
66   values.length
67 end
ordered_values() click to toggle source

Orders media ranges based on q-value, specificity, and the order they are defined

   # File lib/scorched/accept/accept_header.rb
55 def ordered_values
56   @ordered_values ||= values.sort_by { |m| [m[:q] || DEFAULT_QVALUE, specificity(m)] }.reverse
57 end
rank(media_type, invert = false) click to toggle source

Ranks the supplied media type based on its appropriatness. Scores range from 0 (the most appropriate), to ‘n-1`, where `n` is the number of media types defined in the accept header of the request. If invert is true, `n-1` is returned for the most appropriate match, and 0 for the least appropriate. `nil` is returned if the media type is unacceptable.

   # File lib/scorched/accept/accept_header.rb
24 def rank(media_type, invert = false)
25   match = matches(media_type).first
26   if(invert)
27     match && length - 1 - match.last
28   else
29     match && match.last
30   end
31 end
to_h() click to toggle source

Returns a hash with each media range as the key, and the rest of the attributes as the value. Possible attributes are: :parameters, :q, and :extensions.

   # File lib/scorched/accept/accept_header.rb
42 def to_h
43   @to_h ||= ordered_values.map { |v|
44     v = v.dup
45     media_type = v.delete(:media_type)
46     [media_type, v]
47   }.to_h
48 end
values() click to toggle source
   # File lib/scorched/accept/accept_header.rb
50 def values
51   @values ||= AcceptHeaderTransform.new.apply(AcceptHeaderParser.new.parse(@raw_str))
52 end

Protected Instance Methods

matches(media_type) click to toggle source
   # File lib/scorched/accept/accept_header.rb
71 def matches(media_type)
72   matched = {}
73   ordered_values.each_with_index do |v,i|
74     matched[v] = i if Regexp.new(v[:media_type].split('/').map { |v| v == '*' ? '.+' : v }.join('/')) =~ media_type
75   end
76   matched
77 end
specificity(m) click to toggle source

Returns a number representing how specific the media range definition is. Higher numbers represent a more specific media range definition.

   # File lib/scorched/accept/accept_header.rb
81 def specificity(m)
82   if m[:media_type] == '*/*' then 0
83   elsif m[:media_type] =~ %r{/\*$} then 1
84   else 2 + (m[:parameters] ? m[:parameters].count : 0)
85   end
86 end