Class: RubyForge::CookieManager

Inherits:
Object
  • Object
show all
Defined in:
lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cookies_file = nil) ⇒ CookieManager

Returns a new instance of CookieManager.



12
13
14
15
16
17
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb', line 12

def initialize(cookies_file = nil)
  @jar = Hash.new { |hash,domain_name|
    hash[domain_name.downcase] = {}
  }
  @cookies_file = cookies_file
end

Instance Attribute Details

#cookies_fileObject

Returns the value of attribute cookies_file.



11
12
13
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb', line 11

def cookies_file
  @cookies_file
end

Class Method Details

.load(path) ⇒ Object



4
5
6
7
8
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb', line 4

def load(path)
  cm = YAML.load_file(path) rescue CookieManager.new(path)
  cm = CookieManager.new(path) unless cm.is_a?(CookieManager)
  cm.clean_stale_cookies
end

Instance Method Details

#[](uri) ⇒ Object



19
20
21
22
23
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb', line 19

def [](uri)
  # FIXME we need to do more matching on hostname....  This is not
  # bulletproof
  @jar[uri.host.downcase]
end

#add(uri, cookie) ⇒ Object



35
36
37
38
39
40
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb', line 35

def add(uri, cookie)
  no_dot_domain = cookie.domain.gsub(/^\./, '')
  return unless uri.host =~ /#{no_dot_domain}$/i
  @jar[no_dot_domain][cookie.name] = cookie
  clean_stale_cookies
end

#clean_stale_cookiesObject



42
43
44
45
46
47
48
49
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb', line 42

def clean_stale_cookies
  @jar.each do |domain, cookies|
    cookies.each do |name, cookie|
      cookies.delete(name) if cookie.expires < Time.now
    end
  end
  self
end

#empty?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb', line 25

def empty?
  @jar.empty? || @jar.all? { |k,v| v.empty? }
end

#save!Object



29
30
31
32
33
# File 'lib/gems/rubyforge-1.0.1/lib/rubyforge/cookie_manager.rb', line 29

def save!
  File.open(@cookies_file, 'wb') { |f|
    f.write(YAML.dump(self))
  }
end