Module: Modern::Util::HeaderParsing

Defined in:
lib/modern/util/header_parsing.rb

Class Method Summary collapse

Class Method Details

.parse_accept_header(value) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/modern/util/header_parsing.rb', line 6

def self.parse_accept_header(value)
  # TODO: this is probably more garbage creation than necessary.
  # TODO: may poorly prioritize specificity, i.e. `text/*` over `*/*`
  # TODO: this doesn't support `;level=`, but should we bother?
  value.split(",").map do |type_declaration|
    tuple = type_declaration.strip.split(";q=")
    tuple[1] = tuple[1]&.to_f || 1.0

    tuple
  end.sort do |a, b|
    comp = a.last <=> b.last

    if comp != 0
      comp
    else
      -(a.first <=> b.first)
    end
  end.map(&:first)
end