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.



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

def initialize
  @jar = []
end

Instance Method Details

#add(c) ⇒ Object



380
381
382
383
384
385
386
387
# File 'lib/wang.rb', line 380

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

#consume(raw_cookie, uri = nil) ⇒ Object



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

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

#cookies_for(uri) ⇒ Object



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

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)


389
390
391
# File 'lib/wang.rb', line 389

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

#include?(c) ⇒ Boolean

Returns:

  • (Boolean)


406
407
408
# File 'lib/wang.rb', line 406

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

#index(c) ⇒ Object



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

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

  nil
end

#load(io) ⇒ Object



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

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

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

#save(io) ⇒ Object



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

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