Class: Protocol::HTTP::Header::Multiple

Inherits:
Array
  • Object
show all
Defined in:
lib/protocol/http/header/multiple.rb

Overview

Represents headers that can contain multiple distinct values separated by newline characters.

This isn’t a specific header but is used as a base for headers that store multiple values, such as cookies. The values are split and stored as an array internally, and serialized back to a newline-separated string when needed.

Direct Known Subclasses

Cookie

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Multiple

Initializes the multiple header with the given values.



41
42
43
44
45
46
47
# File 'lib/protocol/http/header/multiple.rb', line 41

def initialize(value = nil)
  super()
  
  if value
    self.concat(value)
  end
end

Class Method Details

.coerce(value) ⇒ Object

Coerces a value into a parsed header object.

This method is used by the Headers class when setting values via ‘[]=` to convert application values into the appropriate policy type.



29
30
31
32
33
34
35
36
# File 'lib/protocol/http/header/multiple.rb', line 29

def self.coerce(value)
  case value
  when Array
    self.new(value.map(&:to_s))
  else
    self.parse(value.to_s)
  end
end

.parse(value) ⇒ Object

Parses a raw header value.

Multiple headers receive each value as a separate header entry, so this method takes a single string value and creates a new instance containing it.



19
20
21
# File 'lib/protocol/http/header/multiple.rb', line 19

def self.parse(value)
  self.new([value])
end

.trailer?Boolean

Whether this header is acceptable in HTTP trailers. This is a base class for headers with multiple values, default is to disallow in trailers.



61
62
63
# File 'lib/protocol/http/header/multiple.rb', line 61

def self.trailer?
  false
end

Instance Method Details

#to_sObject

Converts the parsed header value into a raw header value.

Multiple headers are transmitted as separate header entries, so this serializes to a newline-separated string for storage.



54
55
56
# File 'lib/protocol/http/header/multiple.rb', line 54

def to_s
  join("\n")
end