Skip to content

Commit

Permalink
Improved documentation for Protocol::HTTP::Cookie.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Nov 28, 2024
1 parent 78ddfc7 commit 437268a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions lib/protocol/http/cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,39 @@ module Protocol
module HTTP
# Represents an individual cookie key-value pair.
class Cookie
# Initialize the cookie with the given name, value, and directives.
#
# @parameter name [String] The name of the cookiel, e.g. "session_id".
# @parameter value [String] The value of the cookie, e.g. "1234".
# @parameter directives [Hash] The directives of the cookie, e.g. `{"path" => "/"}`.
def initialize(name, value, directives)
@name = name
@value = value
@directives = directives
end

# @attribute [String] The name of the cookie.
attr :name

# @attribute [String] The value of the cookie.
attr :value

# @attribute [Hash] The directives of the cookie.
attr :directives

# Encode the name of the cookie.
def encoded_name
URL.escape(@name)
end

# Encode the value of the cookie.
def encoded_value
URL.escape(@value)
end

# Convert the cookie to a string.
#
# @returns [String] The string representation of the cookie.
def to_s
buffer = String.new.b

Expand All @@ -49,6 +64,10 @@ def to_s
return buffer
end

# Parse a string into a cookie.
#
# @parameter string [String] The string to parse.
# @returns [Cookie] The parsed cookie.
def self.parse(string)
head, *directives = string.split(/\s*;\s*/)

Expand All @@ -62,6 +81,10 @@ def self.parse(string)
)
end

# Parse a list of strings into a hash of directives.
#
# @parameter strings [Array(String)] The list of strings to parse.
# @returns [Hash] The hash of directives.
def self.parse_directives(strings)
strings.collect do |string|
key, value = string.split("=", 2)
Expand Down

0 comments on commit 437268a

Please sign in to comment.