class Cookie < Array
- Inherits from
Array
The cookie header contains stored HTTP cookies previously sent by the server with the set-cookie header.
It is used by clients to send key-value pairs representing stored cookies back to the server.
Multiple cookies within a single Cookie header are joined with "; " per RFC 6265.
Definitions
def self.parse(value)
Parses a raw header value.
Signature
-
parameter
valueString a single raw header value.
-
returns
Cookie a new instance containing the parsed value.
Implementation
def self.parse(value)
self.new([value])
end
def self.coerce(value)
Coerces a value into a parsed header object.
Signature
-
parameter
valueString | Array the value to coerce.
-
returns
Cookie a parsed header object.
Implementation
def self.coerce(value)
case value
when Array
self.new(value.map(&:to_s))
else
self.parse(value.to_s)
end
end
def initialize(value = nil)
Initializes the cookie header with the given values.
Signature
-
parameter
valueArray | Nil an array of cookie strings, or
nilfor an empty header.
Implementation
def initialize(value = nil)
super()
if value
self.concat(value)
end
end
def to_h
Parses the cookie header into a hash of cookie names and their corresponding cookie objects.
Signature
-
returns
Hash(String, HTTP::Cookie) a hash where keys are cookie names and values are
class Protocol::HTTP::Cookieobjects.
Implementation
def to_h
cookies = self.flat_map do |string|
# Each header field can contain multiple cookie pairs separated by semicolons:
string.split(/\s*;\s*/).map do |pair|
HTTP::Cookie.parse(pair)
end
end
cookies.map{|cookie| [cookie.name, cookie]}.to_h
end
def to_s
Serializes the cookie header by joining individual cookie strings with "; " per RFC 6265.
Implementation
def to_s
join("; ")
end
def self.trailer?
Whether this header is acceptable in HTTP trailers. Cookie headers should not appear in trailers as they contain state information needed early in processing.
Signature
-
returns
Boolean false, as cookie headers are needed during initial request processing.
Implementation
def self.trailer?
false
end