Class: HTTPal::Cookie

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/httpal/cookie.rb

Constant Summary collapse

@@fields =
[:path, :domain, :expires]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Cookie

Returns a new instance of Cookie.



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

def initialize(string)
	components = string.split(';')
	firstpair = nil
	components.each do |p|
		sp = p.split('=',2)
		k = sp[0]
		v = sp[1]
		k.strip!
		v.strip!
		if firstpair == nil
			send(:name=, k)
			send(:value=, v)
			firstpair = true
		else
			next unless @@fields.include? k.downcase.to_sym
			v = v.gsub(/^\./,'') if k.downcase.to_sym == :domain
			send((k+'=').downcase.to_sym, v)
		end
	end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



31
32
33
# File 'lib/httpal/cookie.rb', line 31

def name
  @name
end

#valueObject

Returns the value of attribute value.



31
32
33
# File 'lib/httpal/cookie.rb', line 31

def value
  @value
end

Instance Method Details

#<=>(anOther) ⇒ Object



67
68
69
70
71
72
73
74
75
# File 'lib/httpal/cookie.rb', line 67

def <=> (anOther)
	#sort by domain, then path, then name
	d = self.domain.<=>(anOther.domain)
	p = self.path.<=>(anOther.path)
	n = self.name.<=>(anOther.name)
	return d unless d == 0
	return p unless p == 0
	return n
end

#inspectObject



62
63
64
# File 'lib/httpal/cookie.rb', line 62

def inspect
	"\#<Cookie #{name}=#{value} from #{domain}#{path} until #{expires}>"
end

#to_sObject



54
55
56
57
58
59
60
# File 'lib/httpal/cookie.rb', line 54

def to_s
	r = "#{name}=#{value}"
	@@fields.each do |f|
		r << "; #{f.to_s}=#{send(f)}" if send(f)
	end
	return r
end