module Rack::Accept::Header::PublicInstanceMethods
Attributes
A table of all values of this header to their respective quality factors (qvalues).
Public Class Methods
# File lib/rack/accept/header.rb, line 69 def initialize(header='') @qvalues = parse(header) end
Public Instance Methods
Determines if the given value
is acceptable (does not have a
qvalue of 0) according to this header.
# File lib/rack/accept/header.rb, line 97 def accept?(value) qvalue(value) != 0 end
A shortcut for retrieving the first result of sort.
# File lib/rack/accept/header.rb, line 135 def best_of(values, keep_unacceptables=false) sort(values, keep_unacceptables).first end
The name of this header. Should be overridden in classes that mixin this module.
# File lib/rack/accept/header.rb, line 75 def name '' end
Returns the quality factor (qvalue) of the given value
. Should
be overridden in classes that mixin this module.
# File lib/rack/accept/header.rb, line 81 def qvalue(value) 1 end
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, line 130 def sort(values, keep_unacceptables=false) sort_with_qvalues(values, keep_unacceptables).map {|q, v| v } end
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, line 114 def sort_with_qvalues(values, keep_unacceptables=true) qvalues = {} values.each do |v| q = qvalue(v) if q != 0 || keep_unacceptables qvalues[q] ||= [] qvalues[q] << v end end order = qvalues.keys.sort.reverse order.inject([]) {|m, q| m.concat(qvalues[q].map {|v| [q, v] }) } end
Returns a string representation of this header.
# File lib/rack/accept/header.rb, line 140 def to_s [name, value].join(': ') end
Returns the value of this header as a string.
# File lib/rack/accept/header.rb, line 86 def value join(@qvalues) end
Returns an array of all values of this header, in no particular order.
# File lib/rack/accept/header.rb, line 91 def values @qvalues.keys end