This class is used to manage the Cookies that have been returned from any particular website.
# File lib/mechanize/cookie_jar.rb, line 9 def initialize @jar = {} end
Add a cookie to the Jar.
# File lib/mechanize/cookie_jar.rb, line 14 def add(uri, cookie) return unless uri.host =~ %r#{CookieJar.strip_port(cookie.domain)}$/ normal_domain = cookie.domain.downcase unless @jar.has_key?(normal_domain) @jar[normal_domain] = Hash.new { |h,k| h[k] = {} } end @jar[normal_domain][cookie.path] ||= {} @jar[normal_domain][cookie.path][cookie.name] = cookie cleanup cookie end
Clear the cookie jar
# File lib/mechanize/cookie_jar.rb, line 100 def clear! @jar = {} end
# File lib/mechanize/cookie_jar.rb, line 49 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 86 def load(file, format = :yaml) @jar = ::File.open(file) { |f| case format when :yaml then YAML::load(f) when :cookiestxt then load_cookiestxt(f) else raise "Unknown cookie jar file format" end } 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 def save_as(file, format = :yaml) ::File.open(file, "w") { |f| case format when :yaml then YAML::dump(@jar, f) when :cookiestxt then dump_cookiestxt(f) else raise "Unknown cookie jar file format" end } end
# File lib/mechanize/cookie_jar.rb, line 53 def to_a cookies = [] @jar.each do |domain, paths| paths.each do |path, names| cookies << names.values end end cookies.flatten end