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 23: def add(uri, cookie) 24: return nil unless cookie.acceptable_from_uri?(uri) 25: add!(cookie) 26: cookie 27: end
Add a cookie to the jar and return self.
# File lib/mechanize/cookie_jar.rb, line 30 30: def add!(cookie) 31: normal_domain = cookie.domain.downcase 32: 33: @jar[normal_domain] ||= {} unless @jar.has_key?(normal_domain) 34: 35: @jar[normal_domain][cookie.path] ||= {} 36: @jar[normal_domain][cookie.path][cookie.name] = cookie 37: 38: self 39: end
Clear the cookie jar
# File lib/mechanize/cookie_jar.rb, line 132 132: def clear! 133: @jar = {} 134: end
# File lib/mechanize/cookie_jar.rb, line 61 61: def each 62: block_given? or return enum_for(__method__) 63: cleanup 64: @jar.each { |domain, paths| 65: paths.each { |path, hash| 66: hash.each_value { |cookie| 67: yield cookie 68: } 69: } 70: } 71: end
# File lib/mechanize/cookie_jar.rb, line 57 57: def empty?(url) 58: cookies(url).length > 0 ? false : true 59: 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 103: def load(file, format = :yaml) 104: @jar = open(file) { |f| 105: case format 106: when :yaml then 107: load_yaml 108: 109: YAML.load(f) 110: when :cookiestxt then 111: load_cookiestxt(f) 112: else 113: raise ArgumentError, "Unknown cookie jar file format" 114: end 115: } 116: 117: cleanup 118: 119: self 120: 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 78: def save_as(file, format = :yaml) 79: jar = dup 80: jar.cleanup true 81: 82: open(file, 'w') { |f| 83: case format 84: when :yaml then 85: load_yaml 86: 87: YAML.dump(jar.jar, f) 88: when :cookiestxt then 89: jar.dump_cookiestxt(f) 90: else 91: raise ArgumentError, "Unknown cookie jar file format" 92: end 93: } 94: 95: self 96: end
Remove expired cookies
# File lib/mechanize/cookie_jar.rb, line 183 183: def cleanup session = false 184: @jar.each do |domain, paths| 185: paths.each do |path, names| 186: names.each do |cookie_name, cookie| 187: paths[path].delete(cookie_name) if 188: cookie.expired? or (session and cookie.session) 189: end 190: end 191: end 192: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.