Class: AcceptLanguage::Matcher
- Inherits:
-
Object
- Object
- AcceptLanguage::Matcher
- Defined in:
- lib/accept_language/matcher.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
-
#excluded_langtags ⇒ Object
readonly
Returns the value of attribute excluded_langtags.
-
#preferred_langtags ⇒ Object
readonly
Returns the value of attribute preferred_langtags.
Instance Method Summary collapse
-
#call(*available_langtags) ⇒ String, ...
The language that best matches.
-
#initialize(**languages_range) ⇒ Matcher
constructor
A new instance of Matcher.
Constructor Details
#initialize(**languages_range) ⇒ Matcher
Returns a new instance of Matcher.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/accept_language/matcher.rb', line 15 def initialize(**languages_range) @excluded_langtags = Set[] = [] languages_range.select do |langtag, quality| if quality.zero? @excluded_langtags << langtag unless langtag.eql?("*") else level = (quality * 1_000).to_i [level] = langtag end end @preferred_langtags = .compact.reverse end |
Instance Attribute Details
#excluded_langtags ⇒ Object (readonly)
Returns the value of attribute excluded_langtags.
11 12 13 |
# File 'lib/accept_language/matcher.rb', line 11 def @excluded_langtags end |
#preferred_langtags ⇒ Object (readonly)
Returns the value of attribute preferred_langtags.
11 12 13 |
# File 'lib/accept_language/matcher.rb', line 11 def @preferred_langtags end |
Instance Method Details
#call(*available_langtags) ⇒ String, ...
Returns The language that best matches.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/accept_language/matcher.rb', line 36 def call(*) = drop_unacceptable(*) .each do |preferred_tag| if preferred_tag.eql?("*") langtag = any_other_langtag(*) return langtag unless langtag.nil? else .each do |available_langtag| return available_langtag if available_langtag.match?(/\A#{preferred_tag}/i) end end end nil end |