Class: Spidr::CookieJar
- Inherits:
-
Object
- Object
- Spidr::CookieJar
- Includes:
- Enumerable
- Defined in:
- lib/spidr/cookie_jar.rb
Overview
Stores HTTP Cookies organized by host-name.
Instance Method Summary collapse
-
#[](host) ⇒ String?
Return all relevant cookies in a single string for the named host or domain (in browser request format).
-
#[]=(host, cookies) ⇒ Object
Add a cookie to the jar for a particular domain.
-
#clear! ⇒ Object
Clear out the jar, removing all stored cookies.
-
#cookies_for_host(host) ⇒ Hash{String => String}
Returns raw cookie value pairs for a given host.
-
#each {|host, cookie| ... } ⇒ Object
Enumerates over the host-name and cookie value pairs in the cookie jar.
-
#for_host(host) ⇒ String
Returns the pre-encoded Cookie for a given host.
-
#from_page(page) ⇒ Boolean
Retrieve cookies for a domain from a page response header.
-
#initialize ⇒ CookieJar
constructor
Creates a new Cookie Jar object.
-
#inspect ⇒ String
Inspects the cookie jar.
-
#size ⇒ Object
Size of the current cookie jar store.
Constructor Details
#initialize ⇒ CookieJar
Creates a new Cookie Jar object.
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).
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.
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/spidr/cookie_jar.rb', line 75 def []=(host,) collected = self[host] .each do |key,value| if collected[key] != value collected.merge!() @dirty << host break end end return end |
#clear! ⇒ Object
Clear out the jar, removing all stored cookies.
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).
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 (host) = (@params[host] || {}) sub_domains = host.split('.') while sub_domains.length > 2 sub_domains.shift if ( = @params[sub_domains.join('.')]) .each do |name,value| # copy in the parent cookies, only if they haven't been # overridden yet. unless .has_key?(name) [name] = value end end end end return end |
#each {|host, cookie| ... } ⇒ Object
Enumerates over the host-name and cookie value pairs in the cookie jar.
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.
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 = [] (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.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/spidr/cookie_jar.rb', line 101 def from_page(page) = page. unless .empty? self[page.url.host] = return true end return false end |
#inspect ⇒ String
Inspects the cookie jar.
199 200 201 |
# File 'lib/spidr/cookie_jar.rb', line 199 def inspect "#<#{self.class}: #{@params.inspect}>" end |
#size ⇒ Object
Size of the current cookie jar store.
189 190 191 |
# File 'lib/spidr/cookie_jar.rb', line 189 def size @params.size end |