Class: Scorched::Accept::AcceptHeader

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/scorched/accept/accept_header.rb

Instance Method Summary collapse

Constructor Details

#initialize(raw_str = nil) ⇒ AcceptHeader

Returns a new instance of AcceptHeader.



11
12
13
# File 'lib/scorched/accept/accept_header.rb', line 11

def initialize(raw_str = nil)
  @raw_str = (raw_str && not(raw_str =~ /^\s*$/)) ? raw_str : '*/*'
end

Instance Method Details

#acceptable?(media_type) ⇒ Boolean

Returns true if the given media type is acceptable.

Returns:

  • (Boolean)


16
17
18
# File 'lib/scorched/accept/accept_header.rb', line 16

def acceptable?(media_type)
  !matches(media_type).empty?
end

#best_of(*media_types) ⇒ Object

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.



36
37
38
# File 'lib/scorched/accept/accept_header.rb', line 36

def best_of(*media_types)
  media_types.min_by { |m| rank(m) }
end

#each(*args, &block) ⇒ Object

Enumerabilitisationing



60
61
62
# File 'lib/scorched/accept/accept_header.rb', line 60

def each(*args, &block)
  ordered_values.each(*args, &block)
end

#lengthObject

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



65
66
67
# File 'lib/scorched/accept/accept_header.rb', line 65

def length
  values.length
end

#ordered_valuesObject

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



55
56
57
# File 'lib/scorched/accept/accept_header.rb', line 55

def ordered_values
  @ordered_values ||= values.sort_by { |m| [m[:q] || DEFAULT_QVALUE, specificity(m)] }.reverse
end

#rank(media_type, invert = false) ⇒ Object

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.



24
25
26
27
28
29
30
31
# File 'lib/scorched/accept/accept_header.rb', line 24

def rank(media_type, invert = false)
  match = matches(media_type).first
  if(invert)
    match && length - 1 - match.last
  else
    match && match.last
  end
end

#to_hObject

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.



42
43
44
45
46
47
48
# File 'lib/scorched/accept/accept_header.rb', line 42

def to_h
  @to_h ||= ordered_values.map { |v|
    v = v.dup
    media_type = v.delete(:media_type)
    [media_type, v]
  }.to_h
end

#valuesObject



50
51
52
# File 'lib/scorched/accept/accept_header.rb', line 50

def values
  @values ||= AcceptHeaderTransform.new.apply(AcceptHeaderParser.new.parse(@raw_str))
end