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.
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).
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.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/spidr/cookie_jar.rb', line 73 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.
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).
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 (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.
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.
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 = [] (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.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/spidr/cookie_jar.rb', line 99 def from_page(page) = page. unless .empty? self[page.url.host] = return true end return false end |
#inspect ⇒ String
Inspects the cookie jar.
197 198 199 |
# File 'lib/spidr/cookie_jar.rb', line 197 def inspect "#<#{self.class}: #{@params.inspect}>" end |
#size ⇒ Object
Size of the current cookie jar store.
187 188 189 |
# File 'lib/spidr/cookie_jar.rb', line 187 def size @params.size end |