Object
This class is used to manage the Cookies that have been returned from any particular website.
Add a cookie to the Jar.
# File lib/mechanize/cookie_jar.rb, line 14
14: def add(uri, cookie)
15: return unless uri.host =~ /#{CookieJar.strip_port(cookie.domain)}$/
16:
17: normal_domain = cookie.domain.downcase
18:
19: unless @jar.has_key?(normal_domain)
20: @jar[normal_domain] = Hash.new { |h,k| h[k] = {} }
21: end
22:
23: @jar[normal_domain][cookie.path] ||= {}
24: @jar[normal_domain][cookie.path][cookie.name] = cookie
25: cleanup
26: cookie
27: end
Clear the cookie jar
# File lib/mechanize/cookie_jar.rb, line 100
100: def clear!
101: @jar = {}
102: end
# File lib/mechanize/cookie_jar.rb, line 49
49: def empty?(url)
50: cookies(url).length > 0 ? false : true
51: end
Load cookie jar from a file in the format specified.
Available formats: :yaml <- YAML structure. :cookiestxt <- Mozilla’s cookies.txt format
# File lib/mechanize/cookie_jar.rb, line 86
86: def load(file, format = :yaml)
87: @jar = ::File.open(file) { |f|
88: case format
89: when :yaml then
90: YAML::load(f)
91: when :cookiestxt then
92: load_cookiestxt(f)
93: else
94: raise "Unknown cookie jar file format"
95: end
96: }
97: end
Save the cookie jar to a file in the format specified.
Available formats: :yaml <- YAML structure :cookiestxt <- Mozilla’s cookies.txt format
# File lib/mechanize/cookie_jar.rb, line 68
68: def save_as(file, format = :yaml)
69: ::File.open(file, "w") { |f|
70: case format
71: when :yaml then
72: YAML::dump(@jar, f)
73: when :cookiestxt then
74: dump_cookiestxt(f)
75: else
76: raise "Unknown cookie jar file format"
77: end
78: }
79: end
Remove expired cookies
# File lib/mechanize/cookie_jar.rb, line 170
170: def cleanup
171: @jar.each do |domain, paths|
172: paths.each do |path, names|
173: names.each do |cookie_name, cookie|
174: if cookie.expired?
175: paths[path].delete(cookie_name)
176: end
177: end
178: end
179: end
180: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.