Class: AcceptLanguage::Matcher
- Inherits:
-
Object
- Object
- AcceptLanguage::Matcher
- Defined in:
- lib/accept_language/matcher.rb
Overview
Matches Accept-Language header values against application-supported languages to determine the optimal language choice. Handles quality values, wildcards, and language tag matching according to RFC 2616 specifications.
Constant Summary collapse
- WILDCARD =
"*"
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, ...
Finds the optimal language match by comparing user preferences against available languages.
-
#initialize(**languages_range) ⇒ Matcher
constructor
Initialize a new Matcher object with the languages_range parameter representing the user’s preferred languages and their respective quality values.
Constructor Details
#initialize(**languages_range) ⇒ Matcher
Initialize a new Matcher object with the languages_range parameter representing the user’s preferred languages and their respective quality values.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/accept_language/matcher.rb', line 21 def initialize(**languages_range) @excluded_langtags = ::Set[] = [] languages_range.select do |langtag, quality| if quality.zero? @excluded_langtags << langtag unless wildcard?(langtag) 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.
14 15 16 |
# File 'lib/accept_language/matcher.rb', line 14 def @excluded_langtags end |
#preferred_langtags ⇒ Object (readonly)
Returns the value of attribute preferred_langtags.
14 15 16 |
# File 'lib/accept_language/matcher.rb', line 14 def @preferred_langtags end |
Instance Method Details
#call(*available_langtags) ⇒ String, ...
Finds the optimal language match by comparing user preferences against available languages. Handles priorities based on:
-
Explicit quality values (q-values)
-
Language tag specificity (exact matches preferred over partial matches)
-
Order of preference in the original Accept-Language header
46 47 48 49 50 51 52 53 |
# File 'lib/accept_language/matcher.rb', line 46 def call(*) raise ::ArgumentError, "Language tags cannot be nil" if .any?(&:nil?) = drop_unacceptable(*) return nil if .empty? find_best_match() end |