Class: Browser::AcceptLanguage

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(part) ⇒ AcceptLanguage

Returns a new instance of AcceptLanguage.



22
23
24
# File 'lib/browser/accept_language.rb', line 22

def initialize(part)
  @part = part
end

Instance Attribute Details

#partObject (readonly)

Returns the value of attribute part.



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

def part
  @part
end

Class Method Details

.languagesObject



5
6
7
# File 'lib/browser/accept_language.rb', line 5

def self.languages
  @languages ||= YAML.load_file(Browser.root.join("languages.yml"))
end

.parse(accept_language) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/browser/accept_language.rb', line 9

def self.parse(accept_language)
  return [] unless accept_language

  accept_language
    .split(",")
    .map {|string| string.squeeze(" ").strip }
    .map {|part| new(part) }
    .reject {|al| al.quality.zero? }
    .sort_by.with_index {|al, idx| [-al.quality, idx] }
end

Instance Method Details

#codeObject



34
35
36
37
38
39
# File 'lib/browser/accept_language.rb', line 34

def code
  @code ||= begin
    code = part[/\A([^-;]+)/, 1]
    code&.downcase
  end
end

#fullObject



26
27
28
# File 'lib/browser/accept_language.rb', line 26

def full
  @full ||= [code, region].compact.join("-")
end

#nameObject



30
31
32
# File 'lib/browser/accept_language.rb', line 30

def name
  self.class.languages[full] || self.class.languages[code]
end

#qualityObject



48
49
50
51
52
53
54
# File 'lib/browser/accept_language.rb', line 48

def quality
  @quality ||= begin
    Float(quality_value || 1.0)
  rescue ArgumentError
    0.1
  end
end

#regionObject



41
42
43
44
45
46
# File 'lib/browser/accept_language.rb', line 41

def region
  @region ||= begin
    region = part[/\A(?:.*?)-([^;-]+)/, 1]
    region&.upcase
  end
end