Class: Ferrum::Cookies
- Inherits:
-
Object
- Object
- Ferrum::Cookies
- Includes:
- Enumerable
- Defined in:
- lib/ferrum/cookies.rb,
lib/ferrum/cookies/cookie.rb
Overview
Manages a page's cookies via the CDP Network domain. Enumerable over
the page's current Cookies, and provides methods to read,
set, remove and clear them, as well as persist them to/from a YAML file.
Defined Under Namespace
Classes: Cookie
Instance Method Summary collapse
-
#[](name) ⇒ Cookie?
Returns cookie.
-
#all ⇒ Hash{String => Cookie}
Returns cookies hash.
-
#clear ⇒ true
Removes all cookies for current page.
-
#each {|cookie| ... } ⇒ Enumerator
Enumerates over all cookies.
-
#initialize(page) ⇒ Cookies
constructor
A new instance of Cookies.
-
#load(path = "cookies.yml") ⇒ true
Loads all cookies from the file and sets them for current page.
-
#remove(name:, **options) ⇒ Object
Removes given cookie.
-
#set(options) ⇒ Object
Sets a cookie.
-
#store(path = "cookies.yml") ⇒ Integer
Stores all cookies of current page in a file.
Constructor Details
#initialize(page) ⇒ Cookies
Returns a new instance of Cookies.
15 16 17 |
# File 'lib/ferrum/cookies.rb', line 15 def initialize(page) @page = page end |
Instance Method Details
#[](name) ⇒ Cookie?
Returns cookie.
77 78 79 |
# File 'lib/ferrum/cookies.rb', line 77 def [](name) find { || .name == name } end |
#all ⇒ Hash{String => Cookie}
Returns cookies hash.
54 55 56 57 58 |
# File 'lib/ferrum/cookies.rb', line 54 def all each.to_h do || [.name, ] end end |
#clear ⇒ true
Removes all cookies for current page.
173 174 175 176 |
# File 'lib/ferrum/cookies.rb', line 173 def clear @page.command("Network.clearBrowserCookies") true end |
#each {|cookie| ... } ⇒ Enumerator
Enumerates over all cookies.
31 32 33 34 35 36 37 38 39 |
# File 'lib/ferrum/cookies.rb', line 31 def each return enum_for(__method__) unless block_given? = @page.command("Network.getAllCookies")["cookies"] .each do |c| yield Cookie.new(c) end end |
#load(path = "cookies.yml") ⇒ true
Loads all cookies from the file and sets them for current page.
198 199 200 201 202 |
# File 'lib/ferrum/cookies.rb', line 198 def load(path = "cookies.yml") = YAML.load_file(path) .each { |c| set(c) } true end |
#remove(name:, **options) ⇒ Object
Removes given cookie.
154 155 156 157 158 159 160 161 162 163 |
# File 'lib/ferrum/cookies.rb', line 154 def remove(name:, **) raise "Specify :domain or :url option" if ![:domain] && ![:url] && !default_domain = .merge(name: name) [:domain] ||= default_domain @page.command("Network.deleteCookies", **) true end |
#set(options) ⇒ Object
Sets a cookie.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ferrum/cookies.rb', line 123 def set() = ( .is_a?(Cookie) ? .attributes : ).dup.transform_keys(&:to_sym) [:domain] ||= default_domain [:httpOnly] = .delete(:httponly) if .key?(:httponly) [:sameSite] = .delete(:samesite) if .key?(:samesite) expires = .delete(:expires).to_i [:expires] = expires if expires.positive? @page.command("Network.setCookie", **)["success"] end |
#store(path = "cookies.yml") ⇒ Integer
Stores all cookies of current page in a file.
186 187 188 |
# File 'lib/ferrum/cookies.rb', line 186 def store(path = "cookies.yml") File.write(path, map(&:to_h).to_yaml) end |