class HTTP::CookieJar
def add(cookie)
jar.origin = origin
# HTTP::Cookie
jar.add!(cookie) # no acceptance check is performed
jar.add(origin, cookie)
# Mechanize::Cookie
and HTTP::Cookie.parse takes one via the second argument.
To be more specific, HTTP::Cookie.new takes an `:origin` option
manually by #origin=, one is typically given in its generation.
(cf. #origin). While the origin URI of a cookie can be set
In HTTP::Cookie, each cookie object can store its origin URI
### Compatibility Note for Mechanize::Cookie users
individual store classes for that matter.
with the flag off or vice versa depends on the store used. See
Whether a cookie with the `for_domain` flag on overwrites another
set, or ArgumentError is raised.
any case. A given cookie must have domain and path attributes
Adds a cookie to the jar if it is acceptable, and returns self in
def add(cookie) @store.add(cookie) if begin cookie.acceptable? rescue RuntimeError => e raise ArgumentError, e.message end self end
def cleanup(session = false)
Removes expired cookies and returns self. If `session` is true,
def cleanup(session = false) @store.cleanup session self end
def clear
def clear @store.clear self end
def const_missing(name)
def const_missing(name) case name.to_s when /\A([A-Za-z]+)Store\z/ file = 'http/cookie_jar/%s_store' % $1.downcase when /\A([A-Za-z]+)Saver\z/ file = 'http/cookie_jar/%s_saver' % $1.downcase end begin require file rescue LoadError raise NameError, 'can\'t resolve constant %s; failed to load %s' % [name, file] end if const_defined?(name) const_get(name) else raise NameError, 'can\'t resolve constant %s after loading %s' % [name, file] end end
def cookies(url = nil)
`url` is given, only ones that should be sent to the URL/URI are
Gets an array of cookies sorted by the path and creation time. If
def cookies(url = nil) each(url).sort end
def delete(cookie)
depends on the store used. See individual store classes for that
How the `for_domain` flag value affects the set of deleted cookies
given cookie from the jar and returns self.
Deletes a cookie that has the same name, domain and path as a
def delete(cookie) @store.delete(cookie) self end
def each(uri = nil, &block) # :yield: cookie
each cookie is updated to the current time.
If (and only if) the `uri` option is given, last access time of
i.e. cookie.valid_for_uri?(uri) evaluates to true.
should be good to send to the given URI,
destination of the cookies being selected. Every cookie yielded
An optional argument `uri` specifies a URI/URL indicating the
order.
Iterates over all cookies that are not expired in no particular
def each(uri = nil, &block) # :yield: cookie block_given? or return enum_for(__method__, uri) if uri uri = HTTP::Cookie::URIParser.parse(uri) return self unless URI::HTTP === uri && uri.host end @store.each(uri, &block) self end
def empty?(url = nil)
Tests if the jar is empty. If `url` is given, tests if there is
def empty?(url = nil) if url each(url) { return false } return true else @store.empty? end end
def get_impl(base, value, *args)
def get_impl(base, value, *args) case value when base value when Symbol begin base.implementation(value).new(*args) rescue IndexError => e raise ArgumentError, e.message end when Class if base >= value value.new(*args) else raise TypeError, 'not a subclass of %s: %s' % [base, value] end else raise TypeError, 'invalid object: %s' % value.inspect end end
def initialize(options = nil)
(HTTP::CookieJar::MozillaStore) store class requires a `:filename`
specified store class. For example, the `:mozilla`
Any options given are passed through to the initializer of the
to HTTP::CookieJar::MozillaStore.
classes, like `:hash` to HTTP::CookieJar::HashStore and `:mozilla`
of a store class is accepted. Symbols are mapped to store
A symbol addressing a store class, a store class, or an instance
: The store class that backs this jar. (default: `:hash`)
:store
Available option keywords are as below:
Generates a new cookie jar.
def initialize(options = nil) opthash = { :store => :hash, } opthash.update(options) if options @store = get_impl(AbstractStore, opthash[:store], opthash) end
def initialize_copy(other)
def initialize_copy(other) @store = other.instance_eval { @store.dup } end
def load(readable, *options)
All options given are passed through to the underlying cookie
saver class is accepted.
addressing a saver class, or a pre-generated instance of a
Specifies the format for loading. A saver class, a symbol
* `:format`
Available option keywords are below:
\#read it is taken as an IO, or taken as a filename otherwise.
into the jar and returns self. If a given object responds to
Loads cookies recorded in a file or an IO in the format specified
jar.load(filename_or_io, format = :yaml, **options)
jar.load(filename_or_io, **options)
call-seq:
def load(readable, *options) opthash = { :format => :yaml, :session => false, } case options.size when 0 when 1 case options = options.first when Symbol opthash[:format] = options else if hash = Hash.try_convert(options) opthash.update(hash) end end when 2 opthash[:format], options = options if hash = Hash.try_convert(options) opthash.update(hash) end else raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size) end saver = get_impl(AbstractSaver, opthash[:format], opthash) if readable.respond_to?(:write) saver.load(readable, self) else File.open(readable, 'r') { |io| saver.load(io, self) } end self end
def parse(set_cookie, origin, options = nil) # :yield: cookie
See HTTP::Cookie.parse for available options.
}
jar.add(cookie)
HTTP::Cookie.parse(set_cookie, origin) { |cookie|
`jar.parse(set_cookie, origin)` is a shorthand for this:
is added only if the block returns a true value.
If a block is given, it is called for each cookie and the cookie
of cookies that have been added.
as valid and considered acceptable to the jar. Returns an array
sent from a source URL/URI `origin`, and adds the cookies parsed
Parses a Set-Cookie field value `set_cookie` assuming that it is
def parse(set_cookie, origin, options = nil) # :yield: cookie if block_given? HTTP::Cookie.parse(set_cookie, origin, options).tap { |cookies| cookies.select! { |cookie| yield(cookie) && add(cookie) } } else HTTP::Cookie.parse(set_cookie, origin, options) { |cookie| add(cookie) } end end
def save(writable, *options)
All options given are passed through to the underlying cookie
* `:session`
saver class is accepted.
addressing a saver class, or a pre-generated instance of a
Specifies the format for saving. A saver class, a symbol
* `:format`
Available option keywords are below:
taken as an IO, or taken as a filename otherwise.
and returns self. If a given object responds to #write it is
Saves the cookie jar into a file or an IO in the format specified
jar.save(filename_or_io, format = :yaml, **options)
jar.save(filename_or_io, **options)
call-seq:
def save(writable, *options) opthash = { :format => :yaml, :session => false, } case options.size when 0 when 1 case options = options.first when Symbol opthash[:format] = options else if hash = Hash.try_convert(options) opthash.update(hash) end end when 2 opthash[:format], options = options if hash = Hash.try_convert(options) opthash.update(hash) end else raise ArgumentError, 'wrong number of arguments (%d for 1-3)' % (1 + options.size) end saver = get_impl(AbstractSaver, opthash[:format], opthash) if writable.respond_to?(:write) saver.save(writable, self) else File.open(writable, 'w') { |io| saver.save(io, self) } end self end