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: #<<)
Adds a
cookie
to the jar and return self. -
#add!(cookie) ⇒ Object
Used to exist in Mechanize::CookieJar.
-
#cleanup(session = false) ⇒ Object
Removes expired cookies and return self.
-
#clear ⇒ Object
Clears the cookie jar and return self.
-
#clear!(*args) ⇒ Object
Used to exist in Mechanize::CookieJar.
-
#cookies(url) ⇒ Object
Gets an array of cookies that should be sent 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
Generates a new cookie jar.
- #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
Generates a new cookie jar. The default store class is ‘:hash`, which maps to `HTTP::CookieJar::HashStore`. Any given options are passed through to the initializer of the specified store class.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/http/cookie_jar.rb', line 16 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: <<
Adds a cookie
to the jar and return self. If a given cookie has no domain or path attribute values and the origin is unknown, ArgumentError is raised.
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/http/cookie_jar.rb', line 36 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().
50 51 52 |
# File 'lib/http/cookie_jar.rb', line 50 def add!() raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#add!() is #add().' end |
#cleanup(session = false) ⇒ Object
Removes expired cookies and return self.
232 233 234 235 |
# File 'lib/http/cookie_jar.rb', line 232 def cleanup(session = false) @store.cleanup session self end |
#clear ⇒ Object
Clears the cookie jar and return self.
221 222 223 224 |
# File 'lib/http/cookie_jar.rb', line 221 def clear @store.clear self end |
#clear!(*args) ⇒ Object
Used to exist in Mechanize::CookieJar. Use #clear().
227 228 229 |
# File 'lib/http/cookie_jar.rb', line 227 def clear!(*args) raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#clear!() is #clear().' end |
#cookies(url) ⇒ Object
Gets an array of cookies that should be sent for the URL/URI.
55 56 57 58 59 60 |
# File 'lib/http/cookie_jar.rb', line 55 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.
An optional argument uri
specifies 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) the uri
option is given, last access time of each cookie is updated to the current time.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/http/cookie_jar.rb', line 82 def each(uri = nil, &block) block_given? or return enum_for(__method__, uri) if uri uri = URI(uri) return self unless URI::HTTP === uri && uri.host 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.
64 65 66 67 68 69 70 71 |
# File 'lib/http/cookie_jar.rb', line 64 def empty?(url = nil) if url each(url) { return false } return true else @store.empty? end end |
#initialize_copy(other) ⇒ Object
29 30 31 |
# File 'lib/http/cookie_jar.rb', line 29 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, **)
Loads cookies recorded in a file or an IO in the format specified into the jar and return self. If a 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.
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 216 217 218 |
# File 'lib/http/cookie_jar.rb', line 182 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 IndexError => 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, **)
Saves the cookie jar into a file or an IO in the format specified and return self. If a 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.
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 155 156 157 |
# File 'lib/http/cookie_jar.rb', line 121 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 IndexError => 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().
160 161 162 |
# File 'lib/http/cookie_jar.rb', line 160 def save_as(*args) raise NoMethodError, 'HTTP::Cookie equivalent for Mechanize::CookieJar#save_as() is #save().' end |