class CookieJar::Jar

A cookie store for client side usage.

Public Class Methods

from_a(cookies) click to toggle source

Create a new Jar from an array of Cookie objects. Expired cookies will still be added to the archive, and conflicting cookies will be overwritten by the last cookie in the array.

@param [Array<Cookie>] cookies array of cookie objects @return [CookieJar] a new CookieJar instance

# File lib/cookiejar/jar.rb, line 178
def self.from_a(cookies)
  jar = new
  cookies.each do |cookie|
    jar.add_cookie cookie
  end
  jar
end
json_create(o) click to toggle source

Create a new Jar from a JSON-backed hash

@param o [Hash] the expanded JSON object @return [CookieJar] a new CookieJar instance

# File lib/cookiejar/jar.rb, line 163
def self.json_create(o)
  o = JSON.parse(o) if o.is_a? String
  o = o['cookies'] if o.is_a? Hash
  cookies = o.inject([]) do |result, cookie_json|
    result << (Cookie.json_create cookie_json)
  end
  from_a cookies
end
new() click to toggle source

Create a new empty Jar

# File lib/cookiejar/jar.rb, line 48
def initialize
  @domains = {}
end

Public Instance Methods

expire_cookies(session = false) click to toggle source

Look through the jar for any cookies which have passed their expiration date, or session cookies from a previous session

@param session [Boolean] whether session cookies should be expired,

or just cookies past their expiration date.
# File lib/cookiejar/jar.rb, line 191
def expire_cookies(session = false)
  @domains.delete_if do |_domain, paths|
    paths.delete_if do |_path, cookies|
      cookies.delete_if do |_cookie_name, cookie|
        cookie.expired? || (session && cookie.session?)
      end
      cookies.empty?
    end
    paths.empty?
  end
end
get_cookies(request_uri, opts = {}) click to toggle source

Given a request URI, return a sorted list of Cookie objects. Cookies will be in order per RFC 2965 - sorted by longest path length, but otherwise unordered.

@param [String, URI] request_uri the address the HTTP request will be

sent to. This must be a full URI, i.e. must include the protocol,
if you pass digi.ninja it will fail to find the domain, you must pass
http://digi.ninja

@param [Hash] opts options controlling returned cookies @option opts [Boolean] :script (false) Cookies marked HTTP-only will be

ignored if true

@return [Array<Cookie>] cookies which should be sent in the HTTP request

# File lib/cookiejar/jar.rb, line 215
def get_cookies(request_uri, opts = {})
  uri = to_uri request_uri
  hosts = Cookie.compute_search_domains uri

  return [] if hosts.nil?

  path = if uri.path == ''
           '/'
         else
           uri.path
  end

  results = []
  hosts.each do |host|
    domain = find_domain host
    domain.each do |apath, cookies|
      next unless path.start_with? apath
      results += cookies.values.select do |cookie|
        cookie.should_send? uri, opts[:script]
      end
    end
  end
  # Sort by path length, longest first
  results.sort do |lhs, rhs|
    rhs.path.length <=> lhs.path.length
  end
end
set_cookie2(request_uri, cookie_header_value) click to toggle source

Given a request URI and a literal Set-Cookie2 header value, attempt to add the cookie to the cookie store.

@param [String, URI] request_uri the resource returning the header @param [String] cookie_header_value the contents of the Set-Cookie2 @return [Cookie] which was created and stored @raise [InvalidCookieError] if the cookie header did not validate

# File lib/cookiejar/jar.rb, line 73
def set_cookie2(request_uri, cookie_header_value)
  cookie = Cookie.from_set_cookie2 request_uri, cookie_header_value
  add_cookie cookie
end
to_a() click to toggle source

Return an array of all cookie objects in the jar

@return [Array<Cookie>] all cookies. Includes any expired cookies which have not yet been removed with expire_cookies

# File lib/cookiejar/jar.rb, line 136
def to_a
  result = []
  @domains.values.each do |paths|
    paths.values.each do |cookies|
      cookies.values.inject result, :<<
    end
  end
  result
end
to_json(*a) click to toggle source

Return a JSON ‘object’ for the various data values. Allows for persistence of the cookie information

@param [Array] a options controlling output JSON text

(usually a State and a depth)

@return [String] JSON representation of object data

# File lib/cookiejar/jar.rb, line 152
def to_json(*a)
  {
    'json_class' => self.class.name,
    'cookies' => to_a.to_json(*a)
  }.to_json(*a)
end

Protected Instance Methods

find_domain(host) click to toggle source
# File lib/cookiejar/jar.rb, line 301
def find_domain(host)
  @domains[host] || {}
end
gather_header_values(http_header_value) { |value| ... } click to toggle source
# File lib/cookiejar/jar.rb, line 285
def gather_header_values(http_header_value, &_block)
  result = []
  if http_header_value.is_a? Array
    http_header_value.each do |value|
      result << yield(value)
    end
  elsif http_header_value.is_a? String
    result << yield(http_header_value)
  end
  result.compact
end
to_uri(request_uri) click to toggle source
# File lib/cookiejar/jar.rb, line 297
def to_uri(request_uri)
  (request_uri.is_a? URI) ? request_uri : (URI.parse request_uri)
end