Module: Rack::Accept::Header::PublicInstanceMethods
- Included in:
- Rack::Accept::Header
- Defined in:
- lib/rack/accept/header.rb
Instance Attribute Summary collapse
-
#qvalues ⇒ Object
A table of all values of this header to their respective quality factors (qvalues).
Instance Method Summary collapse
-
#accept?(value) ⇒ Boolean
Determines if the given
valueis acceptable (does not have a qvalue of 0) according to this header. -
#best_of(values, keep_unacceptables = false) ⇒ Object
A shortcut for retrieving the first result of #sort.
- #initialize(header = '') ⇒ Object
-
#name ⇒ Object
The name of this header.
-
#qvalue(value) ⇒ Object
Returns the quality factor (qvalue) of the given
value. -
#sort(values, keep_unacceptables = false) ⇒ Object
Sorts the given
valuesaccording to the qvalue of each while preserving the original order. -
#sort_with_qvalues(values, keep_unacceptables = true) ⇒ Object
Returns a copy of the given
valuesarray, sorted by quality factor (qvalue). -
#to_s ⇒ Object
Returns a string representation of this header.
-
#value ⇒ Object
Returns the value of this header as a string.
-
#values ⇒ Object
Returns an array of all values of this header, in no particular order.
Instance Attribute Details
#qvalues ⇒ Object
A table of all values of this header to their respective quality factors (qvalues).
67 68 69 |
# File 'lib/rack/accept/header.rb', line 67 def qvalues @qvalues end |
Instance Method Details
#accept?(value) ⇒ Boolean
Determines if the given value is acceptable (does not have a qvalue
of 0) according to this header.
97 98 99 |
# File 'lib/rack/accept/header.rb', line 97 def accept?(value) qvalue(value) != 0 end |
#best_of(values, keep_unacceptables = false) ⇒ Object
A shortcut for retrieving the first result of #sort.
135 136 137 |
# File 'lib/rack/accept/header.rb', line 135 def best_of(values, keep_unacceptables=false) sort(values, keep_unacceptables).first end |
#initialize(header = '') ⇒ Object
69 70 71 |
# File 'lib/rack/accept/header.rb', line 69 def initialize(header='') @qvalues = parse(header) end |
#name ⇒ Object
The name of this header. Should be overridden in classes that mixin this module.
75 76 77 |
# File 'lib/rack/accept/header.rb', line 75 def name '' end |
#qvalue(value) ⇒ Object
Returns the quality factor (qvalue) of the given value. Should be
overridden in classes that mixin this module.
81 82 83 |
# File 'lib/rack/accept/header.rb', line 81 def qvalue(value) 1 end |
#sort(values, keep_unacceptables = false) ⇒ Object
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.
130 131 132 |
# 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 |
#sort_with_qvalues(values, keep_unacceptables = true) ⇒ Object
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.
114 115 116 117 118 119 120 121 122 123 124 125 |
# 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 |
#to_s ⇒ Object
Returns a string representation of this header.
140 141 142 |
# File 'lib/rack/accept/header.rb', line 140 def to_s [name, value].join(': ') end |
#value ⇒ Object
Returns the value of this header as a string.
86 87 88 |
# File 'lib/rack/accept/header.rb', line 86 def value join(@qvalues) end |
#values ⇒ Object
Returns an array of all values of this header, in no particular order.
91 92 93 |
# File 'lib/rack/accept/header.rb', line 91 def values @qvalues.keys end |