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



311
312
313
314
# File 'lib/wang.rb', line 311

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.



309
310
311
# File 'lib/wang.rb', line 309

def domain
  @domain
end

#expiresObject

Returns the value of attribute expires.



309
310
311
# File 'lib/wang.rb', line 309

def expires
  @expires
end

#keyObject

Returns the value of attribute key.



309
310
311
# File 'lib/wang.rb', line 309

def key
  @key
end

#pathObject

Returns the value of attribute path.



309
310
311
# File 'lib/wang.rb', line 309

def path
  @path
end

#valueObject

Returns the value of attribute value.



309
310
311
# File 'lib/wang.rb', line 309

def value
  @value
end

Instance Method Details

#expired?Boolean



350
351
352
# File 'lib/wang.rb', line 350

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

#match?(uri) ⇒ Boolean



346
347
348
# File 'lib/wang.rb', line 346

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



354
355
356
357
358
359
360
361
362
363
# File 'lib/wang.rb', line 354

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



365
366
367
# File 'lib/wang.rb', line 365

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

#parse(raw_cookie, uri = nil) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/wang.rb', line 316

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



342
343
344
# File 'lib/wang.rb', line 342

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