Parent

Included Modules

Mechanize::CookieJar

This class is used to manage the Cookies that have been returned from any particular website.

Attributes

jar[R]

add_cookie wants something resembling a URI.

Public Class Methods

new() click to toggle source
    # File lib/mechanize/cookie_jar.rb, line 12
12:   def initialize
13:     @jar = {}
14:   end

Public Instance Methods

<<(cookie) click to toggle source
Alias for: add!
add(uri, cookie) click to toggle source

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!(cookie) click to toggle source

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
Also aliased as: <<
clear!() click to toggle source

Clear the cookie jar

     # File lib/mechanize/cookie_jar.rb, line 132
132:   def clear!
133:     @jar = {}
134:   end
cookies(url) click to toggle source

Fetch the cookies that should be used for the URI object passed in.

    # File lib/mechanize/cookie_jar.rb, line 43
43:   def cookies(url)
44:     cleanup
45:     url.path = '/' if url.path.empty?
46:     now = Time.now
47: 
48:     select { |cookie|
49:       !cookie.expired? && cookie.valid_for_uri?(url) && (cookie.accessed_at = now)
50:     }.sort_by { |cookie|
51:       # RFC 6265 5.4
52:       # Precedence: 1. longer path  2. older creation
53:       [-cookie.path.length, cookie.created_at]
54:     }
55:   end
dump_cookiestxt(io) click to toggle source

Write cookies to Mozilla cookies.txt-style IO stream

     # File lib/mechanize/cookie_jar.rb, line 166
166:   def dump_cookiestxt(io)
167:     to_a.each do |cookie|
168:       io.puts([
169:         cookie.domain,
170:         cookie.for_domain? ? "TRUE" : "FALSE",
171:         cookie.path,
172:         cookie.secure ? "TRUE" : "FALSE",
173:         cookie.expires.to_i.to_s,
174:         cookie.name,
175:         cookie.value
176:       ].join("\t"))
177:     end
178:   end
each() click to toggle source
    # 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
empty?(url) click to toggle source
    # File lib/mechanize/cookie_jar.rb, line 57
57:   def empty?(url)
58:     cookies(url).length > 0 ? false : true
59:   end
load(file, format = :yaml) click to toggle source

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
load_cookiestxt(io) click to toggle source

Read cookies from Mozilla cookies.txt-style IO stream

     # File lib/mechanize/cookie_jar.rb, line 137
137:   def load_cookiestxt(io)
138:     now = Time.now
139: 
140:     io.each_line do |line|
141:       line.chomp!
142:       line.gsub!(/#.+/, '')
143:       fields = line.split("\t")
144: 
145:       next if fields.length != 7
146: 
147:       expires_seconds = fields[4].to_i
148:       expires = (expires_seconds == 0) ? nil : Time.at(expires_seconds)
149:       next if expires and (expires < now)
150: 
151:       c = Mechanize::Cookie.new(fields[5], fields[6])
152:       c.domain = fields[0]
153:       c.for_domain = (fields[1] == "TRUE") # Whether this cookie is for domain
154:       c.path = fields[2]               # Path for which the cookie is relevant
155:       c.secure = (fields[3] == "TRUE") # Requires a secure connection
156:       c.expires = expires             # Time the cookie expires.
157:       c.version = 0                   # Conforms to Netscape cookie spec.
158: 
159:       add!(c)
160:     end
161: 
162:     @jar
163:   end
save_as(file, format = :yaml) click to toggle source

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

Protected Instance Methods

cleanup(session = false) click to toggle source

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.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.