Sha256: 62055e22fd24c70d50e7c685b527e181be2e2f076e4855fe3f82ef30f8f1857f
Contents?: true
Size: 1.33 KB
Versions: 1
Compression:
Stored size: 1.33 KB
Contents
# frozen_string_literal: true module RackHttpAcceptLanguage class Parser attr_reader :http_accept_language def initialize(http_accept_language) @http_accept_language = http_accept_language end def preferred_language return nil if early_return? order_languages_by_qvalue.first end def preferred_languages return nil if early_return? order_languages_by_qvalue end private def early_return? http_accept_language.nil? || http_accept_language == '*' end def order_languages_by_qvalue order_languages_by_qvalue_hash.values.flatten end def split_languages @split_languages ||= http_accept_language.to_s.split(',') end def order_languages_by_qvalue_hash @order_languages_by_qvalue ||= split_languages.each_with_object({}) do |language_with_qvalue, memo| language, qvalue = language_with_qvalue.split(';q=') next if language == '*' handle_language(language, qvalue, memo) end.sort.reverse.to_h end def handle_language(language, qvalue, memo) language = language.strip.downcase.gsub(/-[a-z0-9]+$/i, &:upcase) qvalue = qvalue ? qvalue.to_f : 1.0 if memo[qvalue].nil? memo[qvalue] = [language] else memo[qvalue] << language end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rack-http-accept-language-0.1.1 | lib/rack-http-accept-language/parser.rb |