Class: Spidr::CookieJar

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

Instance Method Summary

Constructor Details

- (CookieJar) initialize

Creates a new Cookie Jar object.

Since:

  • 0.2.2


15
16
17
18
19
20
# File 'lib/spidr/cookie_jar.rb', line 15

def initialize
  @params = {}

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

Instance Method Details

- (String?) [](host)

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

Parameters:

  • (String) host — 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


55
56
57
# File 'lib/spidr/cookie_jar.rb', line 55

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

- (Object) []=(host, cookies)

Add a cookie to the jar for a particular domain.

Parameters:

  • (String) host — Host or domain name to associate with the cookie.
  • (Hash{String => String}) cookies — Cookie params.

Since:

  • 0.2.2


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/spidr/cookie_jar.rb', line 70

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

- (Object) clear!

Clear out the jar, removing all stored cookies.

Since:

  • 0.2.2


138
139
140
141
142
143
144
# File 'lib/spidr/cookie_jar.rb', line 138

def clear!
  @params.clear

  @dirty.clear
  @cookies.clear
  return self
end

- (Object) each(&block) {|host, cookie| ... }

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:

  • (String) host — The host-name that the cookie is bound to.
  • (String) cookie — The cookie value.

Since:

  • 0.2.2


38
39
40
# File 'lib/spidr/cookie_jar.rb', line 38

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

- (String) for_host(host)

Returns the pre-encoded Cookie for a given host.

Parameters:

  • (String) host — The name of the host.

Returns:

  • (String) — The encoded Cookie.

Since:

  • 0.2.2


118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/spidr/cookie_jar.rb', line 118

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

    @params[host].each do |name,value|
      values << "#{name}=#{value}"
    end

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

  return @cookies[host]
end

- (Boolean) from_page(page)

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


96
97
98
99
100
101
102
103
104
105
# File 'lib/spidr/cookie_jar.rb', line 96

def from_page(page)
  cookies = page.cookie_params

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

  return false
end

- (String) inspect

Inspects the cookie jar.

Returns:

  • (String) — The inspected version of the cookie jar.


161
162
163
# File 'lib/spidr/cookie_jar.rb', line 161

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

- (Object) size

Size of the current cookie jar store.

Since:

  • 0.2.2


151
152
153
# File 'lib/spidr/cookie_jar.rb', line 151

def size
  @params.size
end