Class: AcceptLanguage::Intersection
- Inherits:
-
Object
- Object
- AcceptLanguage::Intersection
- 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.
Instance Attribute Summary collapse
-
#preferences ⇒ Object
readonly
Returns the value of attribute preferences.
-
#supported_langs ⇒ Object
readonly
Returns the value of attribute supported_langs.
Instance Method Summary collapse
- #call ⇒ Object
-
#initialize(raw_input, *supported_langs, two_letter_truncate: true) ⇒ Intersection
constructor
A new instance of Intersection.
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
#preferences ⇒ Object (readonly)
Returns the value of attribute preferences.
11 12 13 |
# File 'lib/accept_language/intersection.rb', line 11 def preferences @preferences end |
#supported_langs ⇒ Object (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
#call ⇒ Object
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 |