class RoadForest::ContentHandling::MediaTypeList

Public Class Methods

build(list) click to toggle source

Given an acceptance list, create a PriorityList from them.

# File lib/roadforest/content-handling/media-type.rb, line 148
def self.build(list)
  return list if self === list

  case list
  when Array
  when String
    list = list.split(/\s*,\s*/)
  else
    raise "Cannot build a MediaTypeList from #{list.inspect}"
  end

  new.tap do |plist|
    list.each {|item| plist.add_header_val(item) }
  end
end
new() click to toggle source

Creates a {PriorityList}. @see PriorityList::build

# File lib/roadforest/content-handling/media-type.rb, line 168
def initialize
  @list = []
end

Public Instance Methods

accept_header() click to toggle source
# File lib/roadforest/content-handling/media-type.rb, line 172
def accept_header
  @list.map(&:accept_header).join(", ")
end
Also aliased as: to_s
add(type) click to toggle source

Adds an acceptable item with the given priority to the list. @param [Float] q the priority @param [String] choice the acceptable item

# File lib/roadforest/content-handling/media-type.rb, line 207
def add(type)
  @list << type
  self
end
add_header_val(type_string) click to toggle source

Given a raw acceptable value from an acceptance header, parse and add it to the list. @param [String] c the raw acceptable item @see add

# File lib/roadforest/content-handling/media-type.rb, line 216
def add_header_val(type_string)
  add(MediaType.parse(type_string))
rescue ArgumentError
  raise "Invalid media type"
end
best_match_from(other) click to toggle source

Given another MediaTypeList, find the media type that is the best match between them - generally, the idea is to match an Accept header with a local list of provided types

# File lib/roadforest/content-handling/media-type.rb, line 180
def best_match_from(other)
  other.max_by do |their_type|
    best_type = self.by_precedence.find do |our_type|
      their_type =~ our_type
    end
    if best_type.nil?
      0
    else
      best_type.quality * their_type.quality
    end
  end
end
by_precedence() click to toggle source
# File lib/roadforest/content-handling/media-type.rb, line 198
def by_precedence
  self.sort do |left, right|
    right.precedence_index <=> left.precedence_index
  end.enum_for(:each)
end
each() { |item| ... } click to toggle source

Iterates over the list in priority order, that is, taking into account the order in which items were added as well as their priorities. @yield [q,v] @yieldparam [Float] q the acceptable item’s priority @yieldparam [String] v the acceptable item

# File lib/roadforest/content-handling/media-type.rb, line 228
def each
  return enum_for(:each) unless block_given?
  @list.each do |item|
    yield item
  end
end
matches?(other) click to toggle source
# File lib/roadforest/content-handling/media-type.rb, line 193
def matches?(other)
  type = best_match_from(other)
  include?(type) && other.include?(type)
end
to_s()
Alias for: accept_header