Object
This class is used to manage the Cookies that have been returned from any particular website.
Add a cookie to the jar if it is considered acceptable from uri. Return nil if the cookie was not added, otherwise return the cookie added.
# File lib/mechanize/cookie_jar.rb, line 23 def add(uri, cookie) return nil unless cookie.acceptable_from_uri?(uri) add!(cookie) cookie end
Add a cookie to the jar and return self.
# File lib/mechanize/cookie_jar.rb, line 30 def add!(cookie) normal_domain = cookie.domain.downcase @jar[normal_domain] ||= {} unless @jar.has_key?(normal_domain) @jar[normal_domain][cookie.path] ||= {} @jar[normal_domain][cookie.path][cookie.name] = cookie self end
Clear the cookie jar
# File lib/mechanize/cookie_jar.rb, line 132 def clear! @jar = {} end
# File lib/mechanize/cookie_jar.rb, line 61 def each block_given? or return enum_for(__method__) cleanup @jar.each { |domain, paths| paths.each { |path, hash| hash.each_value { |cookie| yield cookie } } } end
# File lib/mechanize/cookie_jar.rb, line 57 def empty?(url) cookies(url).length > 0 ? false : true 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 103 def load(file, format = :yaml) @jar = open(file) { |f| case format when :yaml then load_yaml YAML.load(f) when :cookiestxt then load_cookiestxt(f) else raise ArgumentError, "Unknown cookie jar file format" end } cleanup self 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 78 def save_as(file, format = :yaml) jar = dup jar.cleanup true open(file, 'w') { |f| case format when :yaml then load_yaml YAML.dump(jar.jar, f) when :cookiestxt then jar.dump_cookiestxt(f) else raise ArgumentError, "Unknown cookie jar file format" end } self end
Remove expired cookies
# File lib/mechanize/cookie_jar.rb, line 183 def cleanup session = false @jar.each do |domain, paths| paths.each do |path, names| names.each do |cookie_name, cookie| paths[path].delete(cookie_name) if cookie.expired? or (session and cookie.session) end end end end
Generated with the Darkfish Rdoc Generator 2.