| Class | WWW::Mechanize::CookieJar |
| In: |
lib/mechanize/cookie.rb
|
| Parent: | Object |
This class is used to manage the Cookies that have been returned from any particular website.
| jar | [R] |
Add a cookie to the Jar.
# 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
# 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.
# 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
# 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
# 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
# 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
# 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
Remove expired cookies
# 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