class Protocol::HTTP::Cookie

Represents an individual cookie key-value pair.

Attributes

directives[R]
name[R]
value[R]

Public Class Methods

new(name, value, directives) click to toggle source
# File lib/protocol/http/cookie.rb, line 29
def initialize(name, value, directives)
        @name = name
        @value = value
        @directives = directives
end
parse(string) click to toggle source
# File lib/protocol/http/cookie.rb, line 68
def self.parse(string)
        head, *directives = string.split(/\s*;\s*/)
        
        key, value = head.split('=')
        directives = self.parse_directives(directives)
        
        self.new(
                URL.unescape(key),
                URL.unescape(value),
                directives,
        )
end
parse_directives(strings) click to toggle source
# File lib/protocol/http/cookie.rb, line 81
def self.parse_directives(strings)
        strings.collect do |string|
                key, value = string.split('=', 2)
                [key, value || true]
        end.to_h
end

Public Instance Methods

encoded_name() click to toggle source
# File lib/protocol/http/cookie.rb, line 39
def encoded_name
        URL.escape(@name)
end
encoded_value() click to toggle source
# File lib/protocol/http/cookie.rb, line 43
def encoded_value
        URL.escape(@value)
end
to_s() click to toggle source
# File lib/protocol/http/cookie.rb, line 47
def to_s
        buffer = String.new.b
        
        buffer << encoded_name << '=' << encoded_value
        
        if @directives
                @directives.collect do |key, value|
                        buffer << ';'
                        
                        case value
                        when String
                                buffer << key << '=' << value
                        when TrueClass
                                buffer << key
                        end
                end
        end
        
        return buffer
end