Class: Protocol::HTTP::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/protocol/http/cookie.rb

Overview

Represents an individual cookie key-value pair.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, directives) ⇒ Cookie

Returns a new instance of Cookie.



29
30
31
32
33
# File 'lib/protocol/http/cookie.rb', line 29

def initialize(name, value, directives)
	@name = name
	@value = value
	@directives = directives
end

Class Method Details

.parse(string) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/protocol/http/cookie.rb', line 64

def self.parse string
	head, *options = string.split(/\s*;\s*/)
	
	key, value = head.split('=')
	directives.collect{|directive| directive.split('=', 2)}.to_h
	
	self.new(
		URI.decode(key),
		URI.decode(value),
		directives,
	)
end

Instance Method Details

#encoded_nameObject



35
36
37
# File 'lib/protocol/http/cookie.rb', line 35

def encoded_name
	URL.escape(@name)
end

#encoded_valueObject



39
40
41
# File 'lib/protocol/http/cookie.rb', line 39

def encoded_value
	URL.escape(@value)
end

#to_sObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/protocol/http/cookie.rb', line 43

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