Class: Webmachine::Decision::Conneg::PriorityList
- Inherits:
-
Object
- Object
- Webmachine::Decision::Conneg::PriorityList
- Includes:
- Enumerable
- Defined in:
- lib/webmachine/decision/conneg.rb
Overview
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.
Direct Known Subclasses
Class Method Summary collapse
-
.build(list) ⇒ Object
Given an acceptance list, create a PriorityList from them.
Instance Method Summary collapse
-
#[](q) ⇒ Array<String>
The list of acceptable items at the given priority.
-
#add(q, choice) ⇒ Object
Adds an acceptable item with the given priority to the list.
-
#add_header_val(c) ⇒ Object
Given a raw acceptable value from an acceptance header, parse and add it to the list.
-
#each {|q, v| ... } ⇒ Object
Iterates over the list in priority order, that is, taking into account the order in which items were added as well as their priorities.
-
#initialize ⇒ PriorityList
constructor
Creates a PriorityList.
-
#priority_of(choice) ⇒ Float
The priority of that value.
Constructor Details
#initialize ⇒ PriorityList
Creates a Webmachine::Decision::Conneg::PriorityList.
169 170 171 172 |
# File 'lib/webmachine/decision/conneg.rb', line 169 def initialize @hash = Hash.new { |h, k| h[k] = [] } @index = {} end |
Class Method Details
.build(list) ⇒ Object
Given an acceptance list, create a PriorityList from them.
159 160 161 162 163 |
# 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 |
Instance Method Details
#[](q) ⇒ Array<String>
Returns the list of acceptable items at the given priority.
199 200 201 |
# File 'lib/webmachine/decision/conneg.rb', line 199 def [](q) @hash[q] end |
#add(q, choice) ⇒ Object
Adds an acceptable item with the given priority to the list.
177 178 179 180 |
# File 'lib/webmachine/decision/conneg.rb', line 177 def add(q, choice) @index[choice] = q @hash[q] << choice end |
#add_header_val(c) ⇒ Object
Given a raw acceptable value from an acceptance header, parse and add it to the list.
186 187 188 189 190 191 192 193 194 |
# 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 |
#each {|q, v| ... } ⇒ Object
Iterates over the list in priority order, that is, taking into account the order in which items were added as well as their priorities.
215 216 217 218 219 |
# 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 |
#priority_of(choice) ⇒ Float
Returns the priority of that value.
205 206 207 |
# File 'lib/webmachine/decision/conneg.rb', line 205 def priority_of(choice) @index[choice] end |