Class: Tweakphoeus::CookieJar
- Inherits:
-
Object
- Object
- Tweakphoeus::CookieJar
- Includes:
- Helpers
- Defined in:
- lib/tweakphoeus/cookie_jar.rb
Overview
Cookie manager
Instance Method Summary collapse
- #add_cookies(host, key, value) ⇒ Object
- #cookie_string(url, headers = {}) ⇒ Object
-
#initialize ⇒ CookieJar
constructor
A new instance of CookieJar.
- #obtain_cookies(response) ⇒ Object
- #purge_bad_cookies(cookies) ⇒ Object
Methods included from Helpers
Constructor Details
#initialize ⇒ CookieJar
Returns a new instance of CookieJar.
10 11 12 |
# File 'lib/tweakphoeus/cookie_jar.rb', line 10 def initialize @cookie_jar = {} end |
Instance Method Details
#add_cookies(host, key, value) ⇒ Object
45 46 47 48 49 |
# File 'lib/tweakphoeus/cookie_jar.rb', line 45 def (host, key, value) domain = get_domain(host) @cookie_jar[domain] ||= {} @cookie_jar[domain][key] = value end |
#cookie_string(url, headers = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/tweakphoeus/cookie_jar.rb', line 18 def (url, headers = {}) domain = get_domain(url) headers ||= {} = (headers['Cookie']) while domain != '' @cookie_jar[domain]&.each do |key, value| [key] ||= value end domain = domain.gsub(/^([^.]+\.?)/, '') end .map { |key, value| "#{key}=#{value}" }.join('; ') end |
#obtain_cookies(response) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/tweakphoeus/cookie_jar.rb', line 33 def (response) = [response.headers['Set-Cookie']].compact .each do || key, value = .match(/^([^=]+)=(.+)/).to_a[1..] domain = .match(/Domain=([^;]+)/)&.at(1)&.gsub(/^\./, '') domain = get_domain(response.request.url) if domain.nil (domain, key, value) end end |
#purge_bad_cookies(cookies) ⇒ Object
14 15 16 |
# File 'lib/tweakphoeus/cookie_jar.rb', line 14 def () .reject { || .first.last == '""' } end |