Class: Ferrum::Cookies
- Inherits:
-
Object
- Object
- Ferrum::Cookies
- Includes:
- Cookies::Collection, Collection
- Defined in:
- lib/ferrum/cookies.rb,
lib/ferrum/cookies/cookie.rb,
lib/ferrum/cookies/collection.rb,
sig/ferrum/cookies.rbs,
sig/ferrum/cookies/cookie.rbs,
sig/ferrum/cookies/collection.rbs
Overview
Manages the cookies of a browser Context, which every page in it shares,
via the CDP Storage domain. Enumerable over all of them whatever their
domain, and provides methods to read, set, remove and clear them, as well
as persist them to/from a YAML file. Page::Cookies narrows them down to
what one page sees.
Defined Under Namespace
Modules: Collection Classes: Cookie
Instance Method Summary collapse
-
#clear ⇒ nil
Removes all cookies in the context.
-
#each {|cookie| ... } ⇒ Enumerator
Enumerates over all cookies in the context.
-
#initialize(client, context_id) ⇒ Cookies
constructor
A new instance of Cookies.
-
#load(path = "cookies.yml") ⇒ nil
Loads all cookies from the file and sets them in the context.
-
#remove(name:, **options) ⇒ nil
Removes the cookies with the given name, on the given domain or the ones sent to the given URL.
-
#set(options) ⇒ nil
Sets a cookie.
-
#store(path = "cookies.yml") ⇒ Integer
Stores all cookies in the context in a file.
Methods included from Collection
Constructor Details
#initialize(client, context_id) ⇒ Cookies
Returns a new instance of Cookies.
18 19 20 21 |
# File 'lib/ferrum/cookies.rb', line 18 def initialize(client, context_id) @client = client @context_id = context_id end |
Instance Method Details
#clear ⇒ nil
Removes all cookies in the context.
153 154 155 156 |
# File 'lib/ferrum/cookies.rb', line 153 def clear @client.command("Storage.clearCookies", timeout: @client.timeout, browserContextId: @context_id) nil end |
#each ⇒ void #each ⇒ Enumerator[Cookies::Cookie, void]
Enumerates over all cookies in the context.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ferrum/cookies.rb', line 37 def each return enum_for(__method__) unless block_given? response = @client.command("Storage.getCookies", timeout: @client.timeout, browserContextId: @context_id) = response["cookies"] .each do |c| yield Cookie.new(c) end end |
#load(path = "cookies.yml") ⇒ nil
Loads all cookies from the file and sets them in the context.
182 183 184 185 186 |
# File 'lib/ferrum/cookies.rb', line 182 def load(path = "cookies.yml") = YAML.load_file(path) .each { |c| set(c) } nil end |
#remove(name:, **options) ⇒ nil
Removes the cookies with the given name, on the given domain or the ones sent to the given URL.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/ferrum/cookies.rb', line 128 def remove(name:, **) domain = [:domain] || (default_domain unless [:url]) raise ArgumentError, "Specify :domain or :url option" if !domain && ![:url] uri = URI.parse([:url]) if [:url] = select do || .name == name && ([:path].nil? || .path == [:path]) && (domain ? .domain == domain : sent_to?(, uri)) end .each { || write(.attributes.merge("expires" => 1)) } nil end |
#set(options) ⇒ nil
Sets a cookie.
93 94 95 96 97 98 99 |
# File 'lib/ferrum/cookies.rb', line 93 def set() = () [:domain] ||= default_domain unless [:url] write() nil end |
#store(path = "cookies.yml") ⇒ Integer
Stores all cookies in the context in a file.
168 169 170 |
# File 'lib/ferrum/cookies.rb', line 168 def store(path = "cookies.yml") File.write(path, map(&:to_h).to_yaml) end |