Module | Rack::Accept::Header::PublicInstanceMethods |
In: |
lib/rack/accept/header.rb
|
qvalues | [RW] | A table of all values of this header to their respective quality factors (qvalues). |
# File lib/rack/accept/header.rb, line 69 69: def initialize(header='') 70: @qvalues = parse(header) 71: 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 130: def sort(values, keep_unacceptables=false) 131: sort_with_qvalues(values, keep_unacceptables).map {|q, v| v } 132: 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 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 string representation of this header.
# File lib/rack/accept/header.rb, line 140 140: def to_s 141: [name, value].join(': ') 142: end