Class: Protocol::HTTP::Header::Split

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

Overview

Represents headers that can contain multiple distinct values separated by commas.

This isn’t a specific header class is a utility for handling headers with comma-separated values, such as ‘accept`, `cache-control`, and other similar headers. The values are split and stored as an array internally, and serialized back to a comma-separated string when needed.

Constant Summary collapse

COMMA =

Regular expression used to split values on commas, with optional surrounding whitespace.

/\s*,\s*/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Split

Initializes a ‘Split` header with the given values.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/protocol/http/header/split.rb', line 44

def initialize(value = nil)
  if value.is_a?(Array)
    super(value)
  elsif value.is_a?(String)
    # Compatibility with the old constructor, prefer to use `parse` instead:
    super()
    self << value
  elsif value
    raise ArgumentError, "Invalid value: #{value.inspect}"
  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.



32
33
34
35
36
37
38
39
# File 'lib/protocol/http/header/split.rb', line 32

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.

Split headers receive comma-separated values in a single header entry. This method splits the raw value into individual entries.



22
23
24
# File 'lib/protocol/http/header/split.rb', line 22

def self.parse(value)
  self.new(value.split(COMMA))
end

.trailer?Boolean

Whether this header is acceptable in HTTP trailers. This is a base class for comma-separated headers, default is to disallow in trailers.

Returns:

  • (Boolean)


75
76
77
# File 'lib/protocol/http/header/split.rb', line 75

def self.trailer?
  false
end

Instance Method Details

#<<(value) ⇒ Object

Adds one or more comma-separated values to the header.

The input string is split into distinct entries and appended to the array.



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

def << value
  self.concat(value.split(COMMA))
end

#to_sObject

Converts the parsed header value into a raw header value.



68
69
70
# File 'lib/protocol/http/header/split.rb', line 68

def to_s
  join(",")
end