Class: HttpHeaders::Accept::Entry

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Entry.



11
12
13
14
15
16
17
# File 'lib/http_headers/accept.rb', line 11

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.



19
20
21
# File 'lib/http_headers/accept.rb', line 19

def media_type
  @media_type
end

Instance Method Details

#<=>(other) ⇒ Object



26
27
28
29
30
# File 'lib/http_headers/accept.rb', line 26

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

#[](parameter) ⇒ Object



32
33
34
# File 'lib/http_headers/accept.rb', line 32

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

#qObject

noinspection RubyInstanceMethodNamingConvention



22
23
24
# File 'lib/http_headers/accept.rb', line 22

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

#to_headerObject



36
37
38
# File 'lib/http_headers/accept.rb', line 36

def to_header
  to_s
end

#to_sObject



40
41
42
# File 'lib/http_headers/accept.rb', line 40

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