Class: WANG::Cookie

Inherits:
Object
  • Object
show all
Defined in:
lib/wang.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key = nil, value = nil) ⇒ Cookie

Returns a new instance of Cookie.



317
318
319
320
# File 'lib/wang.rb', line 317

def initialize key = nil, value = nil
	@key, @value = key, value
	@domain, @path, @expires = nil
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



315
316
317
# File 'lib/wang.rb', line 315

def domain
  @domain
end

#expiresObject

Returns the value of attribute expires.



315
316
317
# File 'lib/wang.rb', line 315

def expires
  @expires
end

#keyObject

Returns the value of attribute key.



315
316
317
# File 'lib/wang.rb', line 315

def key
  @key
end

#pathObject

Returns the value of attribute path.



315
316
317
# File 'lib/wang.rb', line 315

def path
  @path
end

#valueObject

Returns the value of attribute value.



315
316
317
# File 'lib/wang.rb', line 315

def value
  @value
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


356
357
358
# File 'lib/wang.rb', line 356

def expired?
	@expires.is_a?(Time) ? @expires < Time.now : false
end

#match?(uri) ⇒ Boolean

Returns:

  • (Boolean)


352
353
354
# File 'lib/wang.rb', line 352

def match? uri
	match_domain?(uri.host) and match_path?(uri.path)
end

#match_domain?(domain) ⇒ Boolean

TODO check if this fully follows the spec

Returns:

  • (Boolean)


360
361
362
363
364
365
366
367
368
369
# File 'lib/wang.rb', line 360

def match_domain? domain # TODO check if this fully follows the spec
	case @domain
	when /^\d+\.\d+\.\d+\.\d+$/ # ip address
		domain.eql?(@domain)
	when /^\./ # so domain = site.com and subdomains could match @domain to .site.com
		domain =~ /#{Regexp.escape(@domain)}$/i
	else
		domain.downcase.eql?(@domain.downcase)
	end
end

#match_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


371
372
373
# File 'lib/wang.rb', line 371

def match_path? path
	path =~ /^#{Regexp.escape(@path)}(?:\/.*)?$/
end

#parse(raw_cookie, uri = nil) ⇒ Object



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/wang.rb', line 322

def parse raw_cookie, uri = nil
	keyval, *attributes = raw_cookie.split(/;\s*/)
	@key, @value = keyval.split("=", 2)

	attributes.each do |at|
		case at
		when /domain=(.*)/i
			@domain = $1
		when /expires=(.*)/i
			@expires = begin
			   Time.parse($1)
		   	rescue
			   nil
			end
		when /path=(.*)/i
			@path = $1
		end
	end

	@domain = uri.host if @domain.nil? and uri
	@path = "/" if @path.nil? and uri
	@path.sub!(/\/$/, "") if @path #remove the trailing /, because path matching automatically adds it

	self
end

#same?(c) ⇒ Boolean

Returns:

  • (Boolean)


348
349
350
# File 'lib/wang.rb', line 348

def same? c
	self.key.eql? c.key and self.domain.eql? c.domain and self.path.eql? c.path
end