Class WWW::Mechanize::CookieJar
In: lib/mechanize/cookie.rb
Parent: Object
Mechanize\n[lib/mechanize.rb\nlib/mechanize/cookie.rb\nlib/mechanize/errors.rb\nlib/mechanize/form.rb\nlib/mechanize/form_elements.rb\nlib/mechanize/history.rb\nlib/mechanize/list.rb\nlib/mechanize/page.rb\nlib/mechanize/page_elements.rb\nlib/mechanize/pluggable_parsers.rb] lib/mechanize.rb WWW dot/m_19_0.png

This class is used to manage the Cookies that have been returned from any particular website.

Methods

add   cleanup   clear!   cookies   empty?   load   new   save_as   to_a  

Attributes

jar  [R] 

Public Class methods

[Source]

    # File lib/mechanize/cookie.rb, line 70
70:       def initialize
71:         @jar = {}
72:       end

Public Instance methods

Add a cookie to the Jar.

[Source]

    # File lib/mechanize/cookie.rb, line 75
75:       def add(uri, cookie)
76:         return unless uri.host =~ /#{cookie.domain}$/i
77:         normal_domain = cookie.domain.downcase
78:         unless @jar.has_key?(normal_domain)
79:           @jar[normal_domain] = Hash.new
80:         end
81:     
82:         @jar[normal_domain][cookie.name] = cookie
83:         cleanup()
84:         cookie
85:       end

Clear the cookie jar

[Source]

     # File lib/mechanize/cookie.rb, line 136
136:       def clear!
137:         @jar = {}
138:       end

Fetch the cookies that should be used for the URI object passed in.

[Source]

     # File lib/mechanize/cookie.rb, line 88
 88:       def cookies(url)
 89:         cleanup
 90:         cookies = []
 91:         url.path = '/' if url.path.empty?
 92:         @jar.each_key do |domain|
 93:           if url.host =~ /#{domain}$/i
 94:             @jar[domain].each_key do |name|
 95:               if url.path =~ /^#{@jar[domain][name].path}/
 96:                 if @jar[domain][name].expires.nil?
 97:                   cookies << @jar[domain][name]
 98:                 elsif Time.now < @jar[domain][name].expires
 99:                   cookies << @jar[domain][name]
100:                 end
101:               end
102:             end
103:           end
104:         end
105:     
106:         cookies
107:       end

[Source]

     # File lib/mechanize/cookie.rb, line 109
109:       def empty?(url)
110:         cookies(url).length > 0 ? false : true
111:       end

Load cookie jar from a file stored as YAML

[Source]

     # File lib/mechanize/cookie.rb, line 131
131:       def load(file)
132:         @jar = ::File.open(file) { |yf| YAML::load( yf ) }
133:       end

Save the cookie jar to a file as YAML

[Source]

     # File lib/mechanize/cookie.rb, line 124
124:       def save_as(file)
125:         ::File.open(file, "w") { |f|
126:           YAML::dump(@jar, f)
127:         }
128:       end

[Source]

     # File lib/mechanize/cookie.rb, line 113
113:       def to_a
114:         cookies = []
115:         @jar.each_key do |domain|
116:           @jar[domain].each_key do |name|
117:             cookies << @jar[domain][name]
118:           end
119:         end
120:         cookies
121:       end

Private Instance methods

Remove expired cookies

[Source]

     # File lib/mechanize/cookie.rb, line 142
142:       def cleanup
143:         @jar.each_key do |domain|
144:           @jar[domain].each_key do |name|
145:             unless @jar[domain][name].expires.nil?
146:               if Time.now > @jar[domain][name].expires
147:                 @jar[domain].delete(name)
148:               end
149:             end
150:           end
151:         end
152:       end

[Validate]