Class: WANG::Jar

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

Instance Method Summary collapse

Constructor Details

#initializeJar

Returns a new instance of Jar.



377
378
379
# File 'lib/wang.rb', line 377

def initialize
  @jar = []
end

Instance Method Details

#add(c) ⇒ Object



386
387
388
389
390
391
392
393
# File 'lib/wang.rb', line 386

def add c
  i = index(c)
  if i.nil?
    @jar << c
  else
    @jar[i] = c
  end
end

#consume(raw_cookie, uri = nil) ⇒ Object



381
382
383
384
# File 'lib/wang.rb', line 381

def consume raw_cookie, uri = nil
  cookie = Cookie.new.parse(raw_cookie, uri)
  add(cookie)
end

#cookies_for(uri) ⇒ Object



399
400
401
402
# File 'lib/wang.rb', line 399

def cookies_for uri
  @jar -= @jar.select { |c| c.expired? }
  @jar.select { |c| c.match?(uri) }.map{ |c| "#{c.key}=#{c.value}" }.join("; ")
end

#has_cookies_for?(uri) ⇒ Boolean

Returns:

  • (Boolean)


395
396
397
# File 'lib/wang.rb', line 395

def has_cookies_for? uri
  not cookies_for(uri).empty?
end

#include?(c) ⇒ Boolean

Returns:

  • (Boolean)


412
413
414
# File 'lib/wang.rb', line 412

def include? c
  not index(c).nil?
end

#index(c) ⇒ Object



404
405
406
407
408
409
410
# File 'lib/wang.rb', line 404

def index c
  @jar.each do |cookie|
    return @jar.index(cookie) if cookie.same? c
  end

  nil
end

#load(io) ⇒ Object



421
422
423
424
425
426
427
# File 'lib/wang.rb', line 421

def load io
  saved_jar = YAML::load(io)

  saved_jar.each do |c|
    add(c)
  end
end

#save(io) ⇒ Object



416
417
418
419
# File 'lib/wang.rb', line 416

def save io
  io << @jar.to_yaml
  io.close
end