Class: HTTP::CookieJar
- Inherits:
-
Object
- Object
- HTTP::CookieJar
- Includes:
- Enumerable
- Defined in:
- lib/http/cookie_jar.rb,
lib/http/cookie_jar/hash_store.rb
Overview
This class is used to manage the Cookies that have been returned from any particular website.
Defined Under Namespace
Classes: AbstractSaver, AbstractStore, CookiestxtSaver, HashStore, YAMLSaver
Instance Attribute Summary collapse
-
#store ⇒ Object
readonly
Returns the value of attribute store.
Instance Method Summary collapse
-
#add(cookie, *_) ⇒ Object
(also: #<<)
Add a
cookie
to the jar and return self. -
#add!(cookie) ⇒ Object
Used to exist in Mechanize::CookieJar.
-
#cleanup(session = false) ⇒ Object
Remove expired cookies and return self.
-
#clear ⇒ Object
Clear the cookie jar and return self.
-
#clear!(*args) ⇒ Object
Used to exist in Mechanize::CookieJar.
-
#cookies(url) ⇒ Object
Fetch the cookies that should be used for the URL/URI.
-
#each(uri = nil, &block) ⇒ Object
Iterates over all cookies that are not expired.
-
#empty?(url = nil) ⇒ Boolean
Tests if the jar is empty.
-
#initialize(store = :hash, options = nil) ⇒ CookieJar
constructor
A new instance of CookieJar.
- #initialize_copy(other) ⇒ Object
-
#load(readable, *options) ⇒ Object
call-seq: jar.load(filename_or_io, **options) jar.load(filename_or_io, format = :yaml, **options).
-
#save(writable, *options) ⇒ Object
call-seq: jar.save(filename_or_io, **options) jar.save(filename_or_io, format = :yaml, **options).
-
#save_as(*args) ⇒ Object
Used to exist in Mechanize::CookieJar.
Constructor Details
#initialize(store = :hash, options = nil) ⇒ CookieJar
Returns a new instance of CookieJar.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/http/cookie_jar.rb', line 13 def initialize(store = :hash, = nil) case store when Symbol @store = AbstractStore.implementation(store).new() when AbstractStore .empty? or raise ArgumentError, 'wrong number of arguments (%d for 1)' % (1 + .size) @store = store else raise TypeError, 'wrong object given as cookie store: %s' % store.inspect end end |
Instance Attribute Details
#store ⇒ Object (readonly)
Returns the value of attribute store.
11 12 13 |
# File 'lib/http/cookie_jar.rb', line 11 def store @store end |
Instance Method Details
#add(cookie, *_) ⇒ Object Also known as: <<
Add a cookie
to the jar and return self.
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/http/cookie_jar.rb', line 31 def add(, *_) _.empty? or raise ArgumentError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#add(uri, cookie) is #add(cookie) after setting cookie.origin = uri.' if .domain.nil? || .path.nil? raise ArgumentError, "a cookie with unknown domain or path cannot be added" end @store.add() self end |
#add!(cookie) ⇒ Object
Used to exist in Mechanize::CookieJar. Use #add().
45 46 47 |
# File 'lib/http/cookie_jar.rb', line 45 def add!() raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#add!() is #add().' end |
#cleanup(session = false) ⇒ Object
Remove expired cookies and return self.
229 230 231 232 |
# File 'lib/http/cookie_jar.rb', line 229 def cleanup(session = false) @store.cleanup session self end |
#clear ⇒ Object
Clear the cookie jar and return self.
218 219 220 221 |
# File 'lib/http/cookie_jar.rb', line 218 def clear @store.clear self end |
#clear!(*args) ⇒ Object
Used to exist in Mechanize::CookieJar. Use #clear().
224 225 226 |
# File 'lib/http/cookie_jar.rb', line 224 def clear!(*args) raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#clear!() is #clear().' end |
#cookies(url) ⇒ Object
Fetch the cookies that should be used for the URL/URI.
50 51 52 53 54 55 |
# File 'lib/http/cookie_jar.rb', line 50 def (url) now = Time.now each(url).select { || !.expired? && (.accessed_at = now) }.sort end |
#each(uri = nil, &block) ⇒ Object
Iterates over all cookies that are not expired.
Available option keywords are below:
-
uri
Specify a URI/URL indicating the destination of the cookies being selected. Every cookie yielded should be good to send to the given URI, i.e. cookie.valid_for_uri?(uri) evaluates to true.
If (and only if) this option is given, last access time of each cookie is updated to the current time.
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/http/cookie_jar.rb', line 81 def each(uri = nil, &block) block_given? or return enum_for(__method__, uri) if uri block = proc { || yield if .valid_for_uri?(uri) } end @store.each(uri, &block) self end |
#empty?(url = nil) ⇒ Boolean
Tests if the jar is empty. If url is given, tests if there is no cookie for the URL.
59 60 61 62 63 64 65 66 |
# File 'lib/http/cookie_jar.rb', line 59 def empty?(url = nil) if url each(url) { return false } return true else @store.empty? end end |
#initialize_copy(other) ⇒ Object
26 27 28 |
# File 'lib/http/cookie_jar.rb', line 26 def initialize_copy(other) @store = other.instance_eval { @store.dup } end |
#load(readable, *options) ⇒ Object
call-seq:
jar.load(filename_or_io, **)
jar.load(filename_or_io, format = :yaml, **)
Load cookies recorded in a file or an IO in the format specified into the jar and return self. If the given object responds to #read it is taken as an IO, or taken as a filename otherwise.
Available option keywords are below:
-
format
:yaml
-
YAML structure (default)
:cookiestxt
-
Mozilla’s cookies.txt format
All options given are passed through to the underlying cookie saver module.
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/http/cookie_jar.rb', line 179 def load(readable, *) opthash = { :format => :yaml, :session => false, } case .size when 0 when 1 case = .first when Symbol opthash[:format] = else opthash.update() if end when 2 opthash[:format], = opthash.update() if else raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + .size) end begin saver = AbstractSaver.implementation(opthash[:format]).new(opthash) rescue KeyError => e raise ArgumentError, e. end if readable.respond_to?(:write) saver.load(readable, self) else File.open(readable, 'r') { |io| saver.load(io, self) } end self end |
#save(writable, *options) ⇒ Object
call-seq:
jar.save(filename_or_io, **)
jar.save(filename_or_io, format = :yaml, **)
Save the cookie jar into a file or an IO in the format specified and return self. If the given object responds to #write it is taken as an IO, or taken as a filename otherwise.
Available option keywords are below:
-
format
:yaml
-
YAML structure (default)
:cookiestxt
-
Mozilla’s cookies.txt format
-
session
true
-
Save session cookies as well.
false
-
Do not save session cookies. (default)
All options given are passed through to the underlying cookie saver module.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/http/cookie_jar.rb', line 118 def save(writable, *) opthash = { :format => :yaml, :session => false, } case .size when 0 when 1 case = .first when Symbol opthash[:format] = else opthash.update() if end when 2 opthash[:format], = opthash.update() if else raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + .size) end begin saver = AbstractSaver.implementation(opthash[:format]).new(opthash) rescue KeyError => e raise ArgumentError, e. end if writable.respond_to?(:write) saver.save(writable, self) else File.open(writable, 'w') { |io| saver.save(io, self) } end self end |
#save_as(*args) ⇒ Object
Used to exist in Mechanize::CookieJar. Use #save().
157 158 159 |
# File 'lib/http/cookie_jar.rb', line 157 def save_as(*args) raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#save_as() is #save().' end |