Class: HttpHeaders::AcceptLanguage::Entry

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

Constant Summary collapse

DELIMITER =
'-'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locale, index:, parameters:) ⇒ Entry

Returns a new instance of Entry.



16
17
18
19
20
21
22
23
24
# File 'lib/http_headers/accept_language.rb', line 16

def initialize(locale, index:, parameters:)
  self.locale = locale
  # TODO: support extlang correctly, maybe we don't even need this
  self.language, self.region = locale.split(DELIMITER)
  self.parameters = parameters
  self.index = index

  freeze
end

Instance Attribute Details

#languageObject

Returns the value of attribute language.



14
15
16
# File 'lib/http_headers/accept_language.rb', line 14

def language
  @language
end

#localeObject

Returns the value of attribute locale.



14
15
16
# File 'lib/http_headers/accept_language.rb', line 14

def locale
  @locale
end

#regionObject

Returns the value of attribute region.



14
15
16
# File 'lib/http_headers/accept_language.rb', line 14

def region
  @region
end

Instance Method Details

#<=>(other) ⇒ Object



31
32
33
34
35
# File 'lib/http_headers/accept_language.rb', line 31

def <=>(other)
  quality = other.q <=> q
  return quality unless quality.zero?
  index <=> other.send(:index)
end

#[](parameter) ⇒ Object



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

def [](parameter)
  parameters.fetch(String(parameter).to_sym)
end

#qObject

noinspection RubyInstanceMethodNamingConvention



27
28
29
# File 'lib/http_headers/accept_language.rb', line 27

def q
  parameters.fetch(:q) { 1.0 }.to_f
end

#to_headerObject



41
42
43
# File 'lib/http_headers/accept_language.rb', line 41

def to_header
  to_s
end

#to_sObject



45
46
47
# File 'lib/http_headers/accept_language.rb', line 45

def to_s
  [locale].concat(parameters.map { |k, v| "#{k}=#{v}" }).compact.reject(&:empty?).join('; ')
end