module Rack::Accept::Header::PublicInstanceMethods

Attributes

qvalues[RW]

A table of all values of this header to their respective quality factors (qvalues).

Public Class Methods

new(header='') click to toggle source
   # File lib/rack/accept/header.rb
69 def initialize(header='')
70   @qvalues = parse(header)
71 end

Public Instance Methods

accept?(value) click to toggle source

Determines if the given value is acceptable (does not have a qvalue of 0) according to this header.

   # File lib/rack/accept/header.rb
97 def accept?(value)
98   qvalue(value) != 0
99 end
best_of(values, keep_unacceptables=false) click to toggle source

A shortcut for retrieving the first result of sort.

    # File lib/rack/accept/header.rb
135 def best_of(values, keep_unacceptables=false)
136   sort(values, keep_unacceptables).first
137 end
name() click to toggle source

The name of this header. Should be overridden in classes that mixin this module.

   # File lib/rack/accept/header.rb
75 def name
76   ''
77 end
qvalue(value) click to toggle source

Returns the quality factor (qvalue) of the given value. Should be overridden in classes that mixin this module.

   # File lib/rack/accept/header.rb
81 def qvalue(value)
82   1
83 end
sort(values, keep_unacceptables=false) click to toggle source

Sorts the given values according to the qvalue of each while preserving the original order. See sort_with_qvalues for more information on exactly how the sort is performed.

    # File lib/rack/accept/header.rb
130 def sort(values, keep_unacceptables=false)
131   sort_with_qvalues(values, keep_unacceptables).map {|q, v| v }
132 end
sort_with_qvalues(values, keep_unacceptables=true) click to toggle source

Returns a copy of the given values array, sorted by quality factor (qvalue). Each element of the returned array is itself an array containing two objects: 1) the value's qvalue and 2) the original value.

It is important to note that this sort is a “stable sort”. In other words, the order of the original values is preserved so long as the qvalue for each is the same. This expectation can be useful when trying to determine which of a variety of options has the highest qvalue. If the user prefers using one option over another (for any number of reasons), he should put it first in values. He may then use the first result with confidence that it is both most acceptable to the client and most convenient for him as well.

    # File lib/rack/accept/header.rb
114 def sort_with_qvalues(values, keep_unacceptables=true)
115   qvalues = {}
116   values.each do |v|
117     q = qvalue(v)
118     if q != 0 || keep_unacceptables
119       qvalues[q] ||= []
120       qvalues[q] << v
121     end
122   end
123   order = qvalues.keys.sort.reverse
124   order.inject([]) {|m, q| m.concat(qvalues[q].map {|v| [q, v] }) }
125 end
to_s() click to toggle source

Returns a string representation of this header.

    # File lib/rack/accept/header.rb
140 def to_s
141   [name, value].join(': ')
142 end
value() click to toggle source

Returns the value of this header as a string.

   # File lib/rack/accept/header.rb
86 def value
87   join(@qvalues)
88 end
values() click to toggle source

Returns an array of all values of this header, in no particular order.

   # File lib/rack/accept/header.rb
91 def values
92   @qvalues.keys
93 end