Class: Protocol::HTTP::Cookie

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

Overview

Represents an individual cookie key-value pair.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value, directives) ⇒ Cookie

Returns a new instance of Cookie.



13
14
15
16
17
# File 'lib/protocol/http/cookie.rb', line 13

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

Instance Attribute Details

#directivesObject (readonly)

Returns the value of attribute directives.



21
22
23
# File 'lib/protocol/http/cookie.rb', line 21

def directives
  @directives
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/protocol/http/cookie.rb', line 19

def name
  @name
end

#valueObject (readonly)

Returns the value of attribute value.



20
21
22
# File 'lib/protocol/http/cookie.rb', line 20

def value
  @value
end

Class Method Details

.parse(string) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/protocol/http/cookie.rb', line 52

def self.parse(string)
	head, *directives = string.split(/\s*;\s*/)
	
	key, value = head.split('=', 2)
	directives = self.parse_directives(directives)
	
	self.new(
		URL.unescape(key),
		URL.unescape(value),
		directives,
	)
end

.parse_directives(strings) ⇒ Object



65
66
67
68
69
70
# File 'lib/protocol/http/cookie.rb', line 65

def self.parse_directives(strings)
	strings.collect do |string|
		key, value = string.split('=', 2)
		[key, value || true]
	end.to_h
end

Instance Method Details

#encoded_nameObject



23
24
25
# File 'lib/protocol/http/cookie.rb', line 23

def encoded_name
	URL.escape(@name)
end

#encoded_valueObject



27
28
29
# File 'lib/protocol/http/cookie.rb', line 27

def encoded_value
	URL.escape(@value)
end

#to_sObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/protocol/http/cookie.rb', line 31

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