Class: AcceptLanguage::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/accept_language/parser.rb

Overview

Parser is a utility class responsible for parsing Accept-Language header fields. It processes the field to extract language tags and their respective quality values.

Examples:

Parser.new("da, en-GB;q=0.8, en;q=0.7")
# => #<AcceptLanguage::Parser:0x00007 @languages_range={"da"=>1.0, "en-GB"=>0.8, "en"=>0.7}>

See Also:

Constant Summary collapse

DEFAULT_QUALITY =
BigDecimal("1")
SEPARATOR =
","
SPACE =
" "
SUFFIX =
";q="

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.



25
26
27
# File 'lib/accept_language/parser.rb', line 25

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

Instance Attribute Details

#languages_rangeObject (readonly)

Returns the value of attribute languages_range.



20
21
22
# File 'lib/accept_language/parser.rb', line 20

def languages_range
  @languages_range
end

Instance Method Details

#match(*available_langtags) ⇒ String, ...

Uses the Matcher class to find the best language match from the list of available languages.

Examples:

When Uyghur, Kazakh, Russian and English languages are available.

match(:ug, :kk, :ru, :en)

Parameters:

  • available_langtags (Array<String, Symbol>)

    An array of language tags that are available for matching.

Returns:

  • (String, Symbol, nil)

    The language tag that best matches the parsed languages from the Accept-Language header, or nil if no match found.



37
38
39
# File 'lib/accept_language/parser.rb', line 37

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