Class: AcceptLanguage::Parser

Inherits:
Object
  • Object
show all
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.

Examples:

parser = Parser.new("da, en-GB;q=0.8, en;q=0.7")
parser.match(:en, :da) # => :da

See Also:

Constant Summary collapse

DEFAULT_QUALITY =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

"1"
SEPARATOR =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

","
SPACE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

" "
SUFFIX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

";q="
QVALUE_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

RFC 2616 Section 3.9 qvalue syntax:

qvalue = ( "0" [ "." 0*3DIGIT ] ) | ( "1" [ "." 0*3("0") ] )
/\A(?:0(?:\.[0-9]{1,3})?|1(?:\.0{1,3})?)\z/
LANGTAG_PATTERN =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Language tag pattern supporting BCP 47 (RFC 5646) alphanumeric subtags.

RFC 2616 Section 3.10 references RFC 1766, which only allowed ALPHA in subtags. However, BCP 47 (the current standard) permits alphanumeric subtags:

subtag = 1*8alphanum
alphanum = ALPHA / DIGIT

Examples of valid BCP 47 tags with numeric subtags:

- "de-CH-1996" (German, Switzerland, orthography variant 1996)
- "sl-IT-nedis" (Slovenian, Italy, Nadiza dialect)
- "zh-Hans-CN" (Chinese, Simplified script, China)
/\A(?:\*|[a-zA-Z]{1,8}(?:-[a-zA-Z0-9]{1,8})*)\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field) ⇒ Parser

Initializes a new Parser instance by importing and processing the given Accept-Language header field.

Parameters:

  • field (String)

    The Accept-Language header field to parse.



49
50
51
# File 'lib/accept_language/parser.rb', line 49

def initialize(field)
  @languages_range = import(field)
end

Instance Attribute Details

#languages_rangeHash<String, BigDecimal> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Parsed language tags and their quality values.

Returns:

  • (Hash<String, BigDecimal>)

    Parsed language tags and their quality values



44
45
46
# File 'lib/accept_language/parser.rb', line 44

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”).

Examples:

Match against specific language options

parser.match("en", "fr", "de") # => "en" if English is preferred

Match with region-specific tags

parser.match("en-US", "en-GB", "fr") # => "en-GB" if British English is preferred

Parameters:

  • available_langtags (Array<String, Symbol>)

    Languages supported by your application

Returns:

  • (String, Symbol, nil)

    Best matching language tag or nil if no match found



63
64
65
# File 'lib/accept_language/parser.rb', line 63

def match(*available_langtags)
  Matcher.new(**languages_range).call(*available_langtags)
end