module Rack::Accept::Header::PublicInstanceMethods
Attributes
A table of all values of this header to their respective quality factors (qvalues).
Public Class Methods
Source
# File lib/rack/accept/header.rb 69 def initialize(header='') 70 @qvalues = parse(header) 71 end
Public Instance Methods
Source
# File lib/rack/accept/header.rb 97 def accept?(value) 98 qvalue(value) != 0 99 end
Determines if the given value
is acceptable (does not have a qvalue of 0) according to this header.
Source
# File lib/rack/accept/header.rb 135 def best_of(values, keep_unacceptables=false) 136 sort(values, keep_unacceptables).first 137 end
A shortcut for retrieving the first result of sort
.
Source
# File lib/rack/accept/header.rb 75 def name 76 '' 77 end
The name of this header. Should be overridden in classes that mixin this module.
Source
# File lib/rack/accept/header.rb 81 def qvalue(value) 82 1 83 end
Returns the quality factor (qvalue) of the given value
. Should be overridden in classes that mixin this module.
Source
# 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
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.
Source
# 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
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.
Source
# File lib/rack/accept/header.rb 140 def to_s 141 [name, value].join(': ') 142 end
Returns a string representation of this header.
Source
# File lib/rack/accept/header.rb 86 def value 87 join(@qvalues) 88 end
Returns the value of this header as a string.
Source
# File lib/rack/accept/header.rb 91 def values 92 @qvalues.keys 93 end
Returns an array of all values of this header, in no particular order.