Class: Webmachine::Decision::Conneg::PriorityList

Inherits:
Object
  • Object
show all
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

MediaTypeList

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePriorityList

See Also:

  • PriorityList::build


168
169
170
171
# File 'lib/webmachine/decision/conneg.rb', line 168

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.



158
159
160
161
162
# File 'lib/webmachine/decision/conneg.rb', line 158

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.

Parameters:

  • q (Float)

    the priority to lookup

Returns:

  • (Array<String>)

    the list of acceptable items at the given priority



198
199
200
# File 'lib/webmachine/decision/conneg.rb', line 198

def [](q)
  @hash[q]
end

#add(q, choice) ⇒ Object

Adds an acceptable item with the given priority to the list.

Parameters:

  • q (Float)

    the priority

  • choice (String)

    the acceptable item



176
177
178
179
# File 'lib/webmachine/decision/conneg.rb', line 176

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.

Parameters:

  • c (String)

    the raw acceptable item

See Also:



185
186
187
188
189
190
191
192
193
# File 'lib/webmachine/decision/conneg.rb', line 185

def add_header_val(c)
  if c =~ CONNEG_REGEX
    choice, q = $1, $2
    q = "0" << q if 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.

Yields:

  • (q, v)

Yield Parameters:

  • q (Float)

    the acceptable item’s priority

  • v (String)

    the acceptable item



214
215
216
217
218
# File 'lib/webmachine/decision/conneg.rb', line 214

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.

Parameters:

  • choice (String)

    the acceptable item

Returns:

  • (Float)

    the priority of that value



204
205
206
# File 'lib/webmachine/decision/conneg.rb', line 204

def priority_of(choice)
  @index[choice]
end