Class: MediaTypes::Serialization::Utils::AcceptLanguageHeader::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/media_types/serialization/utils/accept_language_header.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.



41
42
43
44
45
46
47
48
49
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 41

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.



39
40
41
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 39

def language
  @language
end

#localeObject

Returns the value of attribute locale.



39
40
41
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 39

def locale
  @locale
end

#regionObject

Returns the value of attribute region.



39
40
41
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 39

def region
  @region
end

Instance Method Details

#<=>(other) ⇒ Object



56
57
58
59
60
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 56

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

#[](parameter) ⇒ Object



62
63
64
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 62

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

#qObject

noinspection RubyInstanceMethodNamingConvention



52
53
54
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 52

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

#to_headerObject



66
67
68
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 66

def to_header
  to_s
end

#to_sObject



70
71
72
# File 'lib/media_types/serialization/utils/accept_language_header.rb', line 70

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