Class: MediaTypes::Serialization::Utils::AcceptHeader::Entry

Inherits:
Object
  • Object
show all
Defined in:
lib/media_types/serialization/utils/accept_header.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Entry.



36
37
38
39
40
41
42
# File 'lib/media_types/serialization/utils/accept_header.rb', line 36

def initialize(media_type, index:, parameters:)
  self.media_type = media_type
  self.parameters = parameters
  self.index = index

  freeze
end

Instance Attribute Details

#media_typeObject

Returns the value of attribute media_type.



44
45
46
# File 'lib/media_types/serialization/utils/accept_header.rb', line 44

def media_type
  @media_type
end

Instance Method Details

#<=>(other) ⇒ Object



51
52
53
54
55
# File 'lib/media_types/serialization/utils/accept_header.rb', line 51

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

#[](parameter) ⇒ Object



57
58
59
# File 'lib/media_types/serialization/utils/accept_header.rb', line 57

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

#qObject

noinspection RubyInstanceMethodNamingConvention



47
48
49
# File 'lib/media_types/serialization/utils/accept_header.rb', line 47

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

#to_headerObject



61
62
63
# File 'lib/media_types/serialization/utils/accept_header.rb', line 61

def to_header
  to_s
end

#to_sObject



65
66
67
# File 'lib/media_types/serialization/utils/accept_header.rb', line 65

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