Class: Browser::AcceptLanguage
- Inherits:
-
Object
- Object
- Browser::AcceptLanguage
- Defined in:
- lib/browser/accept_language.rb
Instance Attribute Summary collapse
-
#part ⇒ Object
readonly
Returns the value of attribute part.
Class Method Summary collapse
Instance Method Summary collapse
- #code ⇒ Object
- #full ⇒ Object
-
#initialize(part) ⇒ AcceptLanguage
constructor
A new instance of AcceptLanguage.
- #name ⇒ Object
- #quality ⇒ Object
- #region ⇒ Object
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
#part ⇒ Object (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
.languages ⇒ Object
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
#code ⇒ Object
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 |
#full ⇒ Object
26 27 28 |
# File 'lib/browser/accept_language.rb', line 26 def full @full ||= [code, region].compact.join("-") end |
#name ⇒ Object
30 31 32 |
# File 'lib/browser/accept_language.rb', line 30 def name self.class.languages[full] || self.class.languages[code] end |
#quality ⇒ Object
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 |
#region ⇒ Object
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 |