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



18
19
20
21
22
23
# File 'lib/spidr/cookie_jar.rb', line 18

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



58
59
60
# File 'lib/spidr/cookie_jar.rb', line 58

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



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

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



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

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



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

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



41
42
43
# File 'lib/spidr/cookie_jar.rb', line 41

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



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

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



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

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.



197
198
199
# File 'lib/spidr/cookie_jar.rb', line 197

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

#sizeObject

Size of the current cookie jar store.

Since:

  • 0.2.2



187
188
189
# File 'lib/spidr/cookie_jar.rb', line 187

def size
  @params.size
end