Class: AcceptLanguage::Parser
- Inherits:
-
Object
- Object
- AcceptLanguage::Parser
- Defined in:
- lib/accept_language/parser.rb
Overview
Parses Accept-Language header fields into structured data, extracting language tags and their quality values (q-values). Validates input according to RFC 2616 specifications and handles edge cases like malformed inputs and implicit quality values.
Constant Summary collapse
- DEFAULT_QUALITY =
"1"
- SEPARATOR =
","
- SPACE =
" "
- SUFFIX =
";q="
- QVALUE_PATTERN =
Validates q-values according to RFC 2616:
-
Must be between 0 and 1
-
Can have up to 3 decimal places
-
Allows both forms: .8 and 0.8
-
/\A(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?|\.[0-9]{1,3})\z/
Instance Attribute Summary collapse
-
#languages_range ⇒ Object
readonly
Returns the value of attribute languages_range.
Instance Method Summary collapse
-
#initialize(field) ⇒ Parser
constructor
Initializes a new Parser instance by importing and processing the given Accept-Language header field.
-
#match(*available_langtags) ⇒ String, ...
Finds the best matching language from available options based on user preferences.
Constructor Details
#initialize(field) ⇒ Parser
Initializes a new Parser instance by importing and processing the given Accept-Language header field.
32 33 34 |
# File 'lib/accept_language/parser.rb', line 32 def initialize(field) @languages_range = import(field) end |
Instance Attribute Details
#languages_range ⇒ Object (readonly)
Returns the value of attribute languages_range.
27 28 29 |
# File 'lib/accept_language/parser.rb', line 27 def languages_range @languages_range end |
Instance Method Details
#match(*available_langtags) ⇒ String, ...
Finds the best matching language from available options based on user preferences. Considers quality values and language tag specificity (e.g., “en-US” vs “en”).
45 46 47 |
# File 'lib/accept_language/parser.rb', line 45 def match(*) Matcher.new(**languages_range).call(*) end |