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   uri = URI(uri)
63 
64   now = Time.now
65   tpath = uri.path
66 
67   @cookies.delete_if do |cookie|
68     if cookie.expired?(now)
69       true
70     else
71       yield cookie if cookie.valid_for_uri?(uri) && Cookie.path_match?(cookie.path, tpath)
72       false
73     end
74   end
75 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
77 def merge(other)
78   cookies_dup = dup
79 
80   other.each do |elem|
81     cookie = case elem
82              when Cookie
83                elem
84              when Array
85                Cookie.new(*elem)
86              else
87                Cookie.new(elem)
88     end
89 
90     cookies_dup.add(cookie)
91   end
92 
93   cookies_dup
94 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