Class: Ferrum::Cookies

Inherits:
Object
  • Object
show all
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

Methods included from Collection

#[]

Constructor Details

#initialize(client, context_id) ⇒ Cookies

Returns a new instance of Cookies.

Parameters:

  • client (Client)
  • context_id (String)


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.

Examples:

browser.cookies.clear # => true

Returns:

  • (nil)

Raises:



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.

Overloads:

Yields:

  • (cookie) —

    The given block will be passed each cookie.

Yield Parameters:

  • cookie (Cookie) —

    A cookie in the context.

Returns:

  • (Enumerator) —

    If no block is given, an Enumerator object will be returned.

Raises:



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)
  cookies = response["cookies"]

  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.

Examples:

browser.cookies.load # => true

Parameters:

  • path (String) (defaults to: "cookies.yml")

Returns:

  • (nil)

Raises:



182
183
184
185
186
# File 'lib/ferrum/cookies.rb', line 182

def load(path = "cookies.yml")
  cookies = YAML.load_file(path)
  cookies.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.

Examples:

browser.cookies.remove(name: "stealth", domain: "google.com") # => true

Parameters:

  • name (String)
  • options (Hash{Symbol => Object}) —

    Additional keyword arguments.

  • name: (String)
  • domain: (String, nil)
  • url: (String, nil)
  • path: (String, nil)

Options Hash (**options):

  • :domain (String) —

    The exact domain of the cookie, the :base_url host by default.

  • :url (String) —

    The URL the cookie is sent to.

  • :path (String) —

    The exact path of the cookie.

Returns:

  • (nil)

Raises:

  • (ArgumentError) —

    if neither :domain, :url nor :base_url is set.

  • (TimeoutError)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ferrum/cookies.rb', line 128

def remove(name:, **options)
  domain = options[:domain] || (default_domain unless options[:url])
  raise ArgumentError, "Specify :domain or :url option" if !domain && !options[:url]

  uri = URI.parse(options[:url]) if options[:url]
  cookies = select do |cookie|
    cookie.name == name &&
      (options[:path].nil? || cookie.path == options[:path]) &&
      (domain ? cookie.domain == domain : sent_to?(cookie, uri))
  end
  cookies.each { |cookie| write(cookie.attributes.merge("expires" => 1)) }

  nil
end

#set(options) ⇒ nil

Sets a cookie.

Examples:

browser.cookies.set(name: "stealth", value: "omg", domain: "google.com") # => true
nid_cookie = browser.cookies["NID"] # => <Ferrum::Cookies::Cookie:0x0000558624b67a88>
browser.cookies.set(nid_cookie) # => true

Parameters:

  • options (Hash{Symbol => Object}, Cookie)

Options Hash (options):

  • :name (String) —

    The cookie param name.

  • :value (String) —

    The cookie param value.

  • :domain (String) —

    The domain the cookie belongs to, the :base_url host by default.

  • :url (String) —

    The URL to derive the domain and path from instead.

  • :path (String) —

    The path that the cookie is bound to.

  • :expires (Integer) —

    When the cookie will expire.

  • :httponly (Boolean) —

    Specifies whether the cookie HttpOnly, also accepted as :http_only.

  • :secure (Boolean) —

    Specifies whether the cookie is marked as Secure.

  • :samesite (String) —

    Specifies whether the cookie is SameSite, also accepted as :same_site.

Returns:

  • (nil)

Raises:



93
94
95
96
97
98
99
# File 'lib/ferrum/cookies.rb', line 93

def set(options)
  cookie = cookie_params(options)
  cookie[:domain] ||= default_domain unless cookie[:url]

  write(cookie)
  nil
end

#store(path = "cookies.yml") ⇒ Integer

Stores all cookies in the context in a file.

Examples:

browser.cookies.store # => Integer

Parameters:

  • path (String) (defaults to: "cookies.yml")

Returns:

  • (Integer)

Raises:



168
169
170
# File 'lib/ferrum/cookies.rb', line 168

def store(path = "cookies.yml")
  File.write(path, map(&:to_h).to_yaml)
end