Sha256: 133816e8dcf905181d9cf12b13c51550f352325fc2cd9919b4b4d8dd9de7e788

Contents?: true

Size: 728 Bytes

Versions: 6

Compression:

Stored size: 728 Bytes

Contents

# frozen_string_literal: true

module Modern
  module Util
    module HeaderParsing
      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
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
modern-0.5.0 lib/modern/util/header_parsing.rb
modern-0.4.6 lib/modern/util/header_parsing.rb
modern-0.4.5 lib/modern/util/header_parsing.rb
modern-0.4.4 lib/modern/util/header_parsing.rb
modern-0.4.3 lib/modern/util/header_parsing.rb
modern-0.4.2 lib/modern/util/header_parsing.rb