module AcceptHeaders::Negotiatable

Constants

Q_PATTERN

Attributes

list[R]

Public Class Methods

new(header) click to toggle source
# File lib/accept_headers/negotiatable.rb, line 10
def initialize(header)
  @list = parse(header)
end

Public Instance Methods

accept?(other) click to toggle source
# File lib/accept_headers/negotiatable.rb, line 33
def accept?(other)
  negotiate(other) ? true : false
end
negotiate(supported) click to toggle source
# File lib/accept_headers/negotiatable.rb, line 14
def negotiate(supported)
  return nil if list.empty?
  supported = [*supported]
  # TODO: Maybe q=0 should be first by default when sorting
  rejects, acceptable = list.partition { |m| m.q == 0.0 }
  (rejects + acceptable).each do |part|
    supported.each do |support|
      if part.match(support)
        if part.q == 0.0
          next
        else
          return [support, part]
        end
      end
    end
  end
  nil
end
to_s() click to toggle source
# File lib/accept_headers/negotiatable.rb, line 37
def to_s
  list.join(',')
end

Private Instance Methods

no_header() click to toggle source
# File lib/accept_headers/negotiatable.rb, line 42
def no_header
  raise NotImplementedError.new("#no_header is not implemented")
end
parse(header, &block) click to toggle source
# File lib/accept_headers/negotiatable.rb, line 50
def parse(header, &block)
  return no_header unless header
  header = header.dup
  header.sub!(/\A#{self.class::HEADER_PREFIX}\s*/, '')
  header.strip!
  return no_header if header.empty?
  list = []
  header.split(',').each do |entry|
    begin
      item = parse_item(entry)
      next if item.nil?
      list << item
    rescue Error
      next
    end
  end
  list.sort! { |x,y| y <=> x }
end
parse_item(entry) click to toggle source
# File lib/accept_headers/negotiatable.rb, line 46
def parse_item(entry)
  raise NotImplementedError.new("#parse_item(entry) is not implemented")
end
parse_q(header) click to toggle source
# File lib/accept_headers/negotiatable.rb, line 69
def parse_q(header)
  q = 1
  return q unless header
  q_match = Q_PATTERN.match(header)
  if q_match && q_match[:exists]
    if q_match[:q]
      q = q_match[:q]
    else
      q = 0.001
    end
  end
  q
end