Class: Spidr::CookieJar

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/spidr/cookie_jar.rb

Overview

Stores HTTP Cookies organized by host-name.

Instance Method Summary collapse

Constructor Details

#initializeCookieJar

Creates a new Cookie Jar object.

Since:

  • 0.2.2



20
21
22
23
24
25
# File 'lib/spidr/cookie_jar.rb', line 20

def initialize
  @params = {}

  @dirty   = Set[]
  @cookies = {}
end

Instance Method Details

#[](host) ⇒ String?

Return all relevant cookies in a single string for the named host or domain (in browser request format).

Parameters:

  • host (String)

    Host or domain name for cookies.

Returns:

  • (String, nil)

    The cookie values or nil if the host does not have a cookie in the jar.

Since:

  • 0.2.2



60
61
62
# File 'lib/spidr/cookie_jar.rb', line 60

def [](host)
  @params[host] ||= {}
end

#[]=(host, cookies) ⇒ Object

Add a cookie to the jar for a particular domain.

Parameters:

  • host (String)

    Host or domain name to associate with the cookie.

  • cookies (Hash{String => String})

    Cookie params.

Since:

  • 0.2.2



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/spidr/cookie_jar.rb', line 75

def []=(host,cookies)
  collected = self[host]

  cookies.each do |key,value|
    if collected[key] != value
      collected.merge!(cookies)
      @dirty << host

      break
    end
  end

  return cookies
end

#clear!Object

Clear out the jar, removing all stored cookies.

Since:

  • 0.2.2



176
177
178
179
180
181
182
# File 'lib/spidr/cookie_jar.rb', line 176

def clear!
  @params.clear

  @dirty.clear
  @cookies.clear
  return self
end

#cookies_for_host(host) ⇒ Hash{String => String}

Returns raw cookie value pairs for a given host. Includes cookies set on parent domain(s).

Parameters:

  • host (String)

    The name of the host.

Returns:

  • (Hash{String => String})

    Cookie params.

Since:

  • 0.2.7



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/spidr/cookie_jar.rb', line 150

def cookies_for_host(host)
  host_cookies = (@params[host] || {})
  sub_domains  = host.split('.')

  while sub_domains.length > 2
    sub_domains.shift

    if (parent_cookies = @params[sub_domains.join('.')])
      parent_cookies.each do |name,value|
        # copy in the parent cookies, only if they haven't been
        # overridden yet.
        unless host_cookies.has_key?(name)
          host_cookies[name] = value
        end
      end
    end
  end

  return host_cookies
end

#each {|host, cookie| ... } ⇒ Object

Enumerates over the host-name and cookie value pairs in the cookie jar.

Yields:

  • (host, cookie)

    If a block is given, it will be passed each host-name and cookie value pair.

Yield Parameters:

  • host (String)

    The host-name that the cookie is bound to.

  • cookie (String)

    The cookie value.

Since:

  • 0.2.2



43
44
45
# File 'lib/spidr/cookie_jar.rb', line 43

def each(&block)
  @params.each(&block)
end

#for_host(host) ⇒ String

Returns the pre-encoded Cookie for a given host.

Parameters:

  • host (String)

    The name of the host.

Returns:

  • (String)

    The encoded Cookie.

Since:

  • 0.2.2



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/spidr/cookie_jar.rb', line 123

def for_host(host)
  if @dirty.include?(host)
    values = []

    cookies_for_host(host).each do |name,value|
      values << "#{name}=#{value}"
    end

    @cookies[host] = values.join('; ')
    @dirty.delete(host)
  end

  return @cookies[host]
end

#from_page(page) ⇒ Boolean

Retrieve cookies for a domain from a page response header.

Parameters:

  • page (Page)

    The response page from which to extract cookie data.

Returns:

  • (Boolean)

    Specifies whether cookies were added from the page.

Since:

  • 0.2.2



101
102
103
104
105
106
107
108
109
110
# File 'lib/spidr/cookie_jar.rb', line 101

def from_page(page)
  cookies = page.cookie_params

  unless cookies.empty?
    self[page.url.host] = cookies
    return true
  end

  return false
end

#inspectString

Inspects the cookie jar.

Returns:

  • (String)

    The inspected version of the cookie jar.



199
200
201
# File 'lib/spidr/cookie_jar.rb', line 199

def inspect
  "#<#{self.class}: #{@params.inspect}>"
end

#sizeObject

Size of the current cookie jar store.

Since:

  • 0.2.2



189
190
191
# File 'lib/spidr/cookie_jar.rb', line 189

def size
  @params.size
end