Class: AcceptLanguage::Intersection

Inherits:
Object
  • Object
show all
Defined in:
lib/accept_language/intersection.rb

Overview

Note:

Compare an Accept-Language header value with your application’s supported languages to find the common languages that could be presented to a user.

Examples:

AcceptLanguage::Intersection.new('ja, en-gb;q=0.8, en;q=0.7', :ar, :ja).call # => :ja

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_input, *supported_langs, two_letter_truncate: true) ⇒ Intersection

Returns a new instance of Intersection.



13
14
15
16
17
18
19
20
21
# File 'lib/accept_language/intersection.rb', line 13

def initialize(raw_input, *supported_langs, two_letter_truncate: true)
  @preferences = Parser.call(raw_input, two_letter_truncate: two_letter_truncate)

  @supported_langs = supported_langs.map do |lang|
    lang = lang.downcase
    lang = lang[0, 2] if two_letter_truncate
    lang.to_sym
  end.uniq
end

Instance Attribute Details

#preferencesObject (readonly)

Returns the value of attribute preferences.



11
12
13
# File 'lib/accept_language/intersection.rb', line 11

def preferences
  @preferences
end

#supported_langsObject (readonly)

Returns the value of attribute supported_langs.



11
12
13
# File 'lib/accept_language/intersection.rb', line 11

def supported_langs
  @supported_langs
end

Instance Method Details

#callObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/accept_language/intersection.rb', line 23

def call
  qualities_without_zero_in_desc_order.each do |quality|
    tag = preferences.key(quality)

    if wildcard?(tag)
      lang = any_tag_not_matched_by_any_other_range
      return lang unless lang.nil?
    end

    return tag if supported_langs.include?(tag)
  end

  nil
end