Class: Mechanize::CookieJar
- Inherits:
-
Object
- Object
- Mechanize::CookieJar
- Defined in:
- lib/mechanize/cookie_jar.rb
Overview
This class is used to manage the Cookies that have been returned from any particular website.
Defined Under Namespace
Classes: FakeURI
Instance Attribute Summary collapse
-
#jar ⇒ Object
readonly
Returns the value of attribute jar.
Instance Method Summary collapse
-
#add(uri, cookie) ⇒ Object
Add a cookie to the Jar.
-
#clear! ⇒ Object
Clear the cookie jar.
-
#cookies(url) ⇒ Object
Fetch the cookies that should be used for the URI object passed in.
-
#dump_cookiestxt(io) ⇒ Object
Write cookies to Mozilla cookies.txt-style IO stream.
- #empty?(url) ⇒ Boolean
-
#initialize ⇒ CookieJar
constructor
A new instance of CookieJar.
-
#initialize_copy(other) ⇒ Object
:nodoc:.
-
#load(file, format = :yaml) ⇒ Object
Load cookie jar from a file in the format specified.
-
#load_cookiestxt(io) ⇒ Object
Read cookies from Mozilla cookies.txt-style IO stream.
-
#load_yaml ⇒ Object
:nodoc:.
-
#save_as(file, format = :yaml) ⇒ Object
Save the cookie jar to a file in the format specified.
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ CookieJar
Returns a new instance of CookieJar.
13 14 15 |
# File 'lib/mechanize/cookie_jar.rb', line 13 def initialize @jar = {} end |
Instance Attribute Details
#jar ⇒ Object (readonly)
Returns the value of attribute jar.
11 12 13 |
# File 'lib/mechanize/cookie_jar.rb', line 11 def jar @jar end |
Instance Method Details
#add(uri, cookie) ⇒ Object
Add a cookie to the Jar.
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/mechanize/cookie_jar.rb', line 22 def add(uri, ) return unless (uri, ) normal_domain = .domain.downcase @jar[normal_domain] ||= {} unless @jar.has_key?(normal_domain) @jar[normal_domain][.path] ||= {} @jar[normal_domain][.path][.name] = end |
#clear! ⇒ Object
Clear the cookie jar
133 134 135 |
# File 'lib/mechanize/cookie_jar.rb', line 133 def clear! @jar = {} end |
#cookies(url) ⇒ Object
Fetch the cookies that should be used for the URI object passed in.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mechanize/cookie_jar.rb', line 36 def (url) cleanup url.path = '/' if url.path.empty? domains = @jar.find_all { |domain, _| = self.class.strip_port(domain) if .start_with?('.') url.host =~ /#{Regexp.escape }$/i else url.host =~ /^#{Regexp.escape }$/i end } return [] unless domains.length > 0 = domains.map { |_,paths| paths.find_all { |path, _| url.path =~ /^#{Regexp.escape(path)}/ }.map { |_,| .values } }.flatten .find_all { || ! .expired? } end |
#dump_cookiestxt(io) ⇒ Object
Write cookies to Mozilla cookies.txt-style IO stream
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/mechanize/cookie_jar.rb', line 169 def (io) to_a.each do || fields = [] fields[0] = .domain if .domain =~ /^\./ fields[1] = "TRUE" else fields[1] = "FALSE" end fields[2] = .path if .secure == true fields[3] = "TRUE" else fields[3] = "FALSE" end fields[4] = .expires.to_i.to_s fields[5] = .name fields[6] = .value io.puts(fields.join("\t")) end end |
#empty?(url) ⇒ Boolean
60 61 62 |
# File 'lib/mechanize/cookie_jar.rb', line 60 def empty?(url) (url).length > 0 ? false : true end |
#initialize_copy(other) ⇒ Object
:nodoc:
17 18 19 |
# File 'lib/mechanize/cookie_jar.rb', line 17 def initialize_copy other # :nodoc: @jar = Marshal.load Marshal.dump other.jar end |
#load(file, format = :yaml) ⇒ Object
Load cookie jar from a file in the format specified.
Available formats: :yaml <- YAML structure. :cookiestxt <- Mozilla’s cookies.txt format
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/mechanize/cookie_jar.rb', line 104 def load(file, format = :yaml) @jar = open(file) { |f| case format when :yaml then load_yaml YAML.load(f) when :cookiestxt then (f) else raise ArgumentError, "Unknown cookie jar file format" end } cleanup self end |
#load_cookiestxt(io) ⇒ Object
Read cookies from Mozilla cookies.txt-style IO stream
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/mechanize/cookie_jar.rb', line 138 def (io) now = Time.now io.each_line do |line| line.chomp! line.gsub!(/#.+/, '') fields = line.split("\t") next if fields.length != 7 expires_seconds = fields[4].to_i expires = (expires_seconds == 0) ? nil : Time.at(expires_seconds) next if expires and (expires < now) c = Mechanize::Cookie.new(fields[5], fields[6]) c.domain = fields[0] # Field 1 indicates whether the cookie can be read by other machines at # the same domain. This is computed by the cookie implementation, based # on the domain value. c.path = fields[2] # Path for which the cookie is relevant c.secure = (fields[3] == "TRUE") # Requires a secure connection c.expires = expires # Time the cookie expires. c.version = 0 # Conforms to Netscape cookie spec. add(FakeURI.new(c.domain), c) end @jar end |
#load_yaml ⇒ Object
:nodoc:
123 124 125 126 127 128 129 130 |
# File 'lib/mechanize/cookie_jar.rb', line 123 def load_yaml # :nodoc: begin require 'psych' rescue LoadError end require 'yaml' end |
#save_as(file, format = :yaml) ⇒ Object
Save the cookie jar to a file in the format specified.
Available formats: :yaml <- YAML structure :cookiestxt <- Mozilla’s cookies.txt format
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/mechanize/cookie_jar.rb', line 79 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.(f) else raise ArgumentError, "Unknown cookie jar file format" end } self end |
#to_a ⇒ Object
64 65 66 67 68 69 70 71 72 |
# File 'lib/mechanize/cookie_jar.rb', line 64 def to_a cleanup @jar.map do |domain, paths| paths.map do |path, names| names.values end end.flatten end |