Class HTTP::CookieJar
In: lib/http/cookie_jar.rb
lib/http/cookie_jar/mozilla_store.rb
lib/http/cookie_jar/hash_store.rb
Parent: Object

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

Methods

<<   add   cleanup   clear   const_missing   cookies   delete   each   empty?   initialize_copy   load   new   parse   save  

Included Modules

Enumerable

Classes and Modules

Class HTTP::CookieJar::AbstractSaver
Class HTTP::CookieJar::AbstractStore
Class HTTP::CookieJar::CookiestxtSaver
Class HTTP::CookieJar::HashStore
Class HTTP::CookieJar::MozillaStore
Class HTTP::CookieJar::YAMLSaver

Attributes

store  [R] 

Public Class methods

Generates a new cookie jar.

Available option keywords are as below:

:store : The store class that backs this jar. (default: `:hash`) A symbol addressing a store class, a store class, or an instance of a store class is accepted. Symbols are mapped to store classes, like `:hash` to HTTP::CookieJar::HashStore and `:mozilla` to HTTP::CookieJar::MozillaStore.

Any options given are passed through to the initializer of the specified store class. For example, the `:mozilla` (HTTP::CookieJar::MozillaStore) store class requires a `:filename` option. See individual store classes for details.

Public Instance methods

<<(cookie)

Alias for add

Adds a cookie to the jar if it is acceptable, and returns self in any case. A given cookie must have domain and path attributes set, or ArgumentError is raised.

Whether a cookie with the `for_domain` flag on overwrites another with the flag off or vice versa depends on the store used. See individual store classes for that matter.

### Compatibility Note for Mechanize::Cookie users

In HTTP::Cookie, each cookie object can store its origin URI (cf. origin). While the origin URI of a cookie can be set manually by origin=, one is typically given in its generation. To be more specific, HTTP::Cookie.new takes an `:origin` option and HTTP::Cookie.parse takes one via the second argument.

      # Mechanize::Cookie
      jar.add(origin, cookie)
      jar.add!(cookie)    # no acceptance check is performed

      # HTTP::Cookie
      jar.origin = origin
      jar.add(cookie)     # acceptance check is performed

Removes expired cookies and returns self. If `session` is true, all session cookies are removed as well.

Clears the cookie jar and returns self.

Gets an array of cookies sorted by the path and creation time. If `url` is given, only ones that should be sent to the URL/URI are selected, with the access time of each of them updated.

Deletes a cookie that has the same name, domain and path as a given cookie from the jar and returns self.

How the `for_domain` flag value affects the set of deleted cookies depends on the store used. See individual store classes for that matter.

Iterates over all cookies that are not expired in no particular order.

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.

Tests if the jar is empty. If `url` is given, tests if there is no cookie for the URL.

The copy constructor. Not all backend store classes support cloning.

Loads cookies recorded in a file or an IO in the format specified into the jar and returns 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`
      Specifies the format for loading.  A saver class, a symbol
      addressing a saver class, or a pre-generated instance of a
      saver class is accepted.
    
      <dl class="rdoc-list note-list">
        <dt>:yaml</dt>
        <dd>YAML structure (default)</dd>
        <dt>:cookiestxt</dt>
        <dd>Mozilla's cookies.txt format</dd>
      </dl>
    

All options given are passed through to the underlying cookie saver module‘s constructor.

Parses a Set-Cookie field value `set_cookie` assuming that it is sent from a source URL/URI `origin`, and adds the cookies parsed as valid and considered acceptable to the jar. Returns an array of cookies that have been added.

If a block is given, it is called for each cookie and the cookie is added only if the block returns a true value.

`jar.parse(set_cookie, origin)` is a shorthand for this:

        HTTP::Cookie.parse(set_cookie, origin) { |cookie|
          jar.add(cookie)
        }

See HTTP::Cookie.parse for available options.

Saves the cookie jar into a file or an IO in the format specified and returns 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`
      Specifies the format for saving.  A saver class, a symbol
      addressing a saver class, or a pre-generated instance of a
      saver class is accepted.
    
      <dl class="rdoc-list note-list">
        <dt>:yaml</dt>
        <dd>YAML structure (default)</dd>
        <dt>:cookiestxt</dt>
        <dd>Mozilla's cookies.txt format</dd>
      </dl>
    
  • `:session`
      <dl class="rdoc-list note-list">
        <dt>true</dt>
        <dd>Save session cookies as well.</dd>
        <dt>false</dt>
        <dd>Do not save session cookies. (default)</dd>
      </dl>
    

All options given are passed through to the underlying cookie saver module‘s constructor.

[Validate]