class HTTPX::Plugins::Cookies::Jar

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

The Cookie Jar

It stores and manages cookies for a session, such as i.e. evicting when expired, access methods, or initialization from parsing ‘Set-Cookie` HTTP header values.

It closely follows the [CookieStore API](developer.mozilla.org/en-US/docs/Web/API/CookieStore), by implementing the same methods, with a few specific conveniences for this non-browser manipulation use-case.

Methods

Public Class

  1. new

Public Instance

  1. []
  2. add
  3. delete
  4. each
  5. get
  6. get_all
  7. initialize_dup
  8. merge
  9. parse
  10. set

Included modules

  1. Enumerable

Public Class methods

new(cookies = nil)

initializes the cookie store, either empty, or with whatever is passed as cookies, which can be an array of HTTPX::Plugins::Cookies::Cookie objects or hashes-or-tuples of cookie attributes.

[show source]
   # File lib/httpx/plugins/cookies/jar.rb
26 def initialize(cookies = nil)
27   @mtx = Thread::Mutex.new
28   @cookies = []
29 
30   cookies.each do |elem|
31     cookie = case elem
32              when Cookie
33                elem
34              when Array
35                Cookie.new(*elem)
36              else
37                Cookie.new(elem)
38     end
39 
40     @cookies << cookie
41   end if cookies
42 end

Public Instance methods

[](uri)

returns the list of valid cookies which matdh the domain and path from the URI object passed to uri.

[show source]
    # File lib/httpx/plugins/cookies/jar.rb
116 def [](uri)
117   each(uri).sort
118 end
add(cookie, path = nil)

@deprecated

[show source]
   # File lib/httpx/plugins/cookies/jar.rb
93 def add(cookie, path = nil)
94   warn "DEPRECATION WARNING: calling `##{__method__}` is deprecated. Use `#set` instead."
95   c = cookie.dup
96   c.path = path if path && c.path == "/"
97   set(c)
98 end
delete(name_or_options)

deletes all cookies in the store which match either the name (when String) or all attributes (when a Hash or array of tuples) passed to name_or_options.

alternatively, of name_or_options is an instance of HTTPX::Plugins::Cookies::Cookiem, it deletes it from the store.

[show source]
    # File lib/httpx/plugins/cookies/jar.rb
104 def delete(name_or_options)
105   synchronize do
106     case name_or_options
107     when Cookie
108       @cookies.delete(name_or_options)
109     else
110       @cookies.delete_if { |ck| ck.match?(name_or_options) }
111     end
112   end
113 end
each(uri = nil, &blk)

enumerates over all stored cookies. if uri is passed, it’ll filter out expired cookies and only yield cookies which match its domain and path.

[show source]
    # File lib/httpx/plugins/cookies/jar.rb
122 def each(uri = nil, &blk)
123   return enum_for(__method__, uri) unless blk
124 
125   return synchronize { @cookies.each(&blk) } unless uri
126 
127   now = Time.now
128   tpath = uri.path
129 
130   synchronize do
131     @cookies.delete_if do |cookie|
132       if cookie.expired?(now)
133         true
134       else
135         yield cookie if cookie.valid_for_uri?(uri) && Cookie.path_match?(cookie.path, tpath)
136         false
137       end
138     end
139   end
140 end
get(name_or_options)

returns the first HTTPX::Plugins::Cookie::Cookie instance in the store which matches either the name (when String) or all attributes (when a Hash or array of tuples) passed to name_or_options

[show source]
   # File lib/httpx/plugins/cookies/jar.rb
53 def get(name_or_options)
54   each.find { |ck| ck.match?(name_or_options) }
55 end
get_all(name_or_options)

returns all HTTPX::Plugins::Cookie::Cookie instances in the store which match either the name (when String) or all attributes (when a Hash or array of tuples) passed to name_or_options

[show source]
   # File lib/httpx/plugins/cookies/jar.rb
59 def get_all(name_or_options)
60   each.select { |ck| ck.match?(name_or_options) } # rubocop:disable Style/SelectByRegexp
61 end
initialize_dup(orig)
[show source]
   # File lib/httpx/plugins/cookies/jar.rb
18 def initialize_dup(orig)
19   super
20   @mtx = orig.instance_variable_get(:@mtx).dup
21   @cookies = orig.instance_variable_get(:@cookies).dup
22 end
merge(other)
[show source]
    # File lib/httpx/plugins/cookies/jar.rb
142 def merge(other)
143   jar_dup = dup
144 
145   other.each do |elem|
146     cookie = case elem
147              when Cookie
148                elem
149              when Array
150                Cookie.new(*elem)
151              else
152                Cookie.new(elem)
153     end
154 
155     jar_dup.set(cookie)
156   end
157 
158   jar_dup
159 end
parse(set_cookie)

parses the ‘Set-Cookie` header value as set_cookie and does the corresponding updates.

[show source]
   # File lib/httpx/plugins/cookies/jar.rb
45 def parse(set_cookie)
46   SetCookieParser.call(set_cookie) do |name, value, attrs|
47     set(Cookie.new(name, value, attrs))
48   end
49 end
set(name, value_or_options = nil)

optionally, name can also be the attributes hash-or-array as long it contains a :name field).

[show source]
   # File lib/httpx/plugins/cookies/jar.rb
67 def set(name, value_or_options = nil)
68   cookie = case name
69            when Cookie
70              raise ArgumentError, "there should not be a second argument" if value_or_options
71 
72              name
73            when Array, Hash
74              raise ArgumentError, "there should not be a second argument" if value_or_options
75 
76              Cookie.new(name)
77            else
78              raise ArgumentError, "the second argument is required" unless value_or_options
79 
80              Cookie.new(name, value_or_options)
81   end
82 
83   synchronize do
84     # If the user agent receives a new cookie with the same cookie-name, domain-value, and path-value
85     # as a cookie that it has already stored, the existing cookie is evicted and replaced with the new cookie.
86     @cookies.delete_if { |ck| ck.name == cookie.name && ck.domain == cookie.domain && ck.path == cookie.path }
87 
88     @cookies << cookie
89   end
90 end