Class: Philiprehberger::HttpClient::CookieJar
- Inherits:
-
Object
- Object
- Philiprehberger::HttpClient::CookieJar
- Defined in:
- lib/philiprehberger/http_client/cookie_jar.rb
Overview
Simple cookie jar that stores cookies across requests within a session. Parses Set-Cookie headers and sends matching cookies back on subsequent requests.
Defined Under Namespace
Classes: Cookie
Instance Method Summary collapse
-
#clear ⇒ Object
Remove all stored cookies.
-
#cookie_header(uri) ⇒ String?
Return the Cookie header value for a given URI.
-
#initialize ⇒ CookieJar
constructor
A new instance of CookieJar.
-
#size ⇒ Integer
Return the number of stored cookies.
-
#store(set_cookie_header, request_uri) ⇒ Object
Store cookies from a Set-Cookie response header.
-
#to_a ⇒ Array<Cookie>
Return all stored cookies.
Constructor Details
#initialize ⇒ CookieJar
Returns a new instance of CookieJar.
12 13 14 |
# File 'lib/philiprehberger/http_client/cookie_jar.rb', line 12 def initialize @cookies = [] end |
Instance Method Details
#clear ⇒ Object
Remove all stored cookies.
51 52 53 |
# File 'lib/philiprehberger/http_client/cookie_jar.rb', line 51 def clear @cookies.clear end |
#cookie_header(uri) ⇒ String?
Return the Cookie header value for a given URI.
34 35 36 37 38 39 40 |
# File 'lib/philiprehberger/http_client/cookie_jar.rb', line 34 def (uri) purge_expired matching = @cookies.select { |c| matches?(c, uri) } return nil if matching.empty? matching.map { |c| "#{c.name}=#{c.value}" }.join('; ') end |
#size ⇒ Integer
Return the number of stored cookies.
58 59 60 61 |
# File 'lib/philiprehberger/http_client/cookie_jar.rb', line 58 def size purge_expired @cookies.size end |
#store(set_cookie_header, request_uri) ⇒ Object
Store cookies from a Set-Cookie response header.
20 21 22 23 24 25 26 27 28 |
# File 'lib/philiprehberger/http_client/cookie_jar.rb', line 20 def store(, request_uri) return unless = (, request_uri) return unless @cookies.reject! { |c| c.name == .name && c.domain == .domain && c.path == .path } @cookies << end |
#to_a ⇒ Array<Cookie>
Return all stored cookies.
45 46 47 48 |
# File 'lib/philiprehberger/http_client/cookie_jar.rb', line 45 def to_a purge_expired @cookies.dup end |