Class: AcceptLanguage::Parser

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

Overview

Note:

Parser for Accept-Language header fields.

Examples:

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

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

Returns a new instance of Parser.

Parameters:

  • field (String)

    The Accept-Language header field to parse.

See Also:


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

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

Instance Attribute Details

#languages_rangeObject (readonly)

Returns the value of attribute languages_range.


16
17
18
# File 'lib/accept_language/parser.rb', line 16

def languages_range
  @languages_range
end

Instance Method Details

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

Returns The language that best matches.

Examples:

Uyghur, Kazakh, Russian and English languages are available.

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

Parameters:

  • available_langtags (Array<String, Symbol>)

    The list of available languages.

Returns:

  • (String, Symbol, nil)

    The language that best matches.


29
30
31
# File 'lib/accept_language/parser.rb', line 29

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