Class: Webbed::ContentNegotiator
- Inherits:
-
Object
- Object
- Webbed::ContentNegotiator
- Defined in:
- lib/webbed/content_negotiator.rb
Instance Attribute Summary (collapse)
-
- ranges
Returns the value of attribute ranges.
Instance Method Summary (collapse)
-
- (ContentNegotiator) initialize(ranges)
constructor
A new instance of ContentNegotiator.
- - negotiate(tags)
Constructor Details
- (ContentNegotiator) initialize(ranges)
A new instance of ContentNegotiator
5 6 7 |
# File 'lib/webbed/content_negotiator.rb', line 5 def initialize(ranges) @ranges = ranges end |
Instance Attribute Details
- ranges
Returns the value of attribute ranges
3 4 5 |
# File 'lib/webbed/content_negotiator.rb', line 3 def ranges @ranges end |
Instance Method Details
- negotiate(tags)
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/webbed/content_negotiator.rb', line 9 def negotiate() return nil if .empty? sorted_ranges = @ranges.sort do |range0, range1| range1.precedence_sort_array <=> range0.precedence_sort_array end pairs = .inject([]) do |acc, tag| range = sorted_ranges.find { |range| range.include?(tag) } acc.push([tag, range]) if range && range.quality != 0 acc end return nil if pairs.empty? pairs.max do |pair0, pair1| range0 = pair0[1] range1 = pair1[1] range0.quality_sort_array <=> range1.quality_sort_array end.first end |