class Webmachine::Decision::Conneg::PriorityList
@private Content-negotiation priority list that takes into account both assigned priority (“q” value) as well as order, since items that come earlier in an acceptance list have higher priority by fiat.
Public Class Methods
Given an acceptance list, create a PriorityList
from them.
# File lib/webmachine/decision/conneg.rb, line 159 def self.build(list) new.tap do |plist| list.each { |item| plist.add_header_val(item) } end end
Creates a {PriorityList}. @see PriorityList::build
# File lib/webmachine/decision/conneg.rb, line 169 def initialize @hash = Hash.new { |h, k| h[k] = [] } @index = {} end
Public Instance Methods
@param [Float] q the priority to lookup @return [Array<String>] the list of acceptable items at
the given priority
# File lib/webmachine/decision/conneg.rb, line 199 def [](q) @hash[q] end
Adds an acceptable item with the given priority to the list. @param [Float] q the priority @param [String] choice the acceptable item
# File lib/webmachine/decision/conneg.rb, line 177 def add(q, choice) @index[choice] = q @hash[q] << choice end
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/webmachine/decision/conneg.rb, line 186 def add_header_val(c) if c =~ CONNEG_REGEX choice, q = $1, $2 q = '0' << q if /^\./.match?(q) # handle strange FeedBurner Accept add(q.to_f, choice) else add(1.0, c) end end
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/webmachine/decision/conneg.rb, line 215 def each @hash.to_a.sort.reverse_each do |q, l| l.each { |v| yield q, v } end end
@param [String] choice the acceptable item @return [Float] the priority of that value
# File lib/webmachine/decision/conneg.rb, line 205 def priority_of(choice) @index[choice] end