From 437268ab510f8b2e6fc1a836b2a0bd5efdf8b717 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 28 Nov 2024 16:33:48 +1300 Subject: [PATCH] Improved documentation for `Protocol::HTTP::Cookie`. --- lib/protocol/http/cookie.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/lib/protocol/http/cookie.rb b/lib/protocol/http/cookie.rb index dcec882..84a44f5 100644 --- a/lib/protocol/http/cookie.rb +++ b/lib/protocol/http/cookie.rb @@ -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 @@ -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*/) @@ -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)