class HTTPX::Plugins::Cookies::Jar

  1. lib/httpx/plugins/cookies/jar.rb
Superclass: Object

The Cookie Jar

It holds a bunch of cookies.

Methods

Public Class

  1. new

Public Instance

  1. []
  2. add
  3. each
  4. initialize_dup
  5. merge
  6. parse

Included modules

  1. Enumerable

Public Class methods

new(cookies = nil)
[show source]
   # File lib/httpx/plugins/cookies/jar.rb
18 def initialize(cookies = nil)
19   @cookies = []
20 
21   cookies.each do |elem|
22     cookie = case elem
23              when Cookie
24                elem
25              when Array
26                Cookie.new(*elem)
27              else
28                Cookie.new(elem)
29     end
30 
31     @cookies << cookie
32   end if cookies
33 end

Public Instance methods

[](uri)
[show source]
   # File lib/httpx/plugins/cookies/jar.rb
53 def [](uri)
54   each(uri).sort
55 end
add(cookie, path = nil)
[show source]
   # File lib/httpx/plugins/cookies/jar.rb
41 def add(cookie, path = nil)
42   c = cookie.dup
43 
44   c.path = path if path && c.path == "/"
45 
46   # If the user agent receives a new cookie with the same cookie-name, domain-value, and path-value
47   # as a cookie that it has already stored, the existing cookie is evicted and replaced with the new cookie.
48   @cookies.delete_if { |ck| ck.name == c.name && ck.domain == c.domain && ck.path == c.path }
49 
50   @cookies << c
51 end
each(uri = nil, &blk)
[show source]
   # File lib/httpx/plugins/cookies/jar.rb
57 def each(uri = nil, &blk)
58   return enum_for(__method__, uri) unless blk
59 
60   return @cookies.each(&blk) unless uri
61 
62   now = Time.now
63   tpath = uri.path
64 
65   @cookies.delete_if do |cookie|
66     if cookie.expired?(now)
67       true
68     else
69       yield cookie if cookie.valid_for_uri?(uri) && Cookie.path_match?(cookie.path, tpath)
70       false
71     end
72   end
73 end
initialize_dup(orig)
[show source]
   # File lib/httpx/plugins/cookies/jar.rb
13 def initialize_dup(orig)
14   super
15   @cookies = orig.instance_variable_get(:@cookies).dup
16 end
merge(other)
[show source]
   # File lib/httpx/plugins/cookies/jar.rb
75 def merge(other)
76   cookies_dup = dup
77 
78   other.each do |elem|
79     cookie = case elem
80              when Cookie
81                elem
82              when Array
83                Cookie.new(*elem)
84              else
85                Cookie.new(elem)
86     end
87 
88     cookies_dup.add(cookie)
89   end
90 
91   cookies_dup
92 end
parse(set_cookie)
[show source]
   # File lib/httpx/plugins/cookies/jar.rb
35 def parse(set_cookie)
36   SetCookieParser.call(set_cookie) do |name, value, attrs|
37     add(Cookie.new(name, value, attrs))
38   end
39 end