Class: Ferrum::Cookies

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ferrum/cookies.rb,
lib/ferrum/cookies/cookie.rb

Defined Under Namespace

Classes: Cookie

Instance Method Summary collapse

Constructor Details

#initialize(page) ⇒ Cookies

Returns a new instance of Cookies.



9
10
11
# File 'lib/ferrum/cookies.rb', line 9

def initialize(page)
  @page = page
end

Instance Method Details

#[](name) ⇒ Cookie?

Returns cookie.

Examples:

browser.cookies["NID"] # =>
# <Ferrum::Cookies::Cookie:0x0000558624b67a88 @attributes={
#  "name"=>"NID", "value"=>"...", "domain"=>".google.com",
#  "path"=>"/", "expires"=>1583211046.575681, "size"=>178,
#  "httpOnly"=>true, "secure"=>false, "session"=>false
# }>

Parameters:

  • name (String)

    The cookie name to fetch.

Returns:

  • (Cookie, nil)

    The cookie with the matching name.



71
72
73
# File 'lib/ferrum/cookies.rb', line 71

def [](name)
  find { |cookie| cookie.name == name }
end

#allHash{String => Cookie}

Returns cookies hash.

Examples:

browser.cookies.all # => {
#  "NID" => #<Ferrum::Cookies::Cookie:0x0000558624b37a40 @attributes={
#     "name"=>"NID", "value"=>"...", "domain"=>".google.com", "path"=>"/",
#     "expires"=>1583211046.575681, "size"=>178, "httpOnly"=>true, "secure"=>false, "session"=>false
#  }>
# }

Returns:



48
49
50
51
52
# File 'lib/ferrum/cookies.rb', line 48

def all
  each.to_h do |cookie|
    [cookie.name, cookie]
  end
end

#cleartrue

Removes all cookies for current page.

Examples:

browser.cookies.clear # => true

Returns:

  • (true)


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

def clear
  @page.command("Network.clearBrowserCookies")
  true
end

#each {|cookie| ... } ⇒ Enumerator

Enumerates over all cookies.

Yields:

  • (cookie)

    The given block will be passed each cookie.

Yield Parameters:

  • cookie (Cookie)

    A cookie in the browser.

Returns:

  • (Enumerator)

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



25
26
27
28
29
30
31
32
33
# File 'lib/ferrum/cookies.rb', line 25

def each
  return enum_for(__method__) unless block_given?

  cookies = @page.command("Network.getAllCookies")["cookies"]

  cookies.each do |c|
    yield Cookie.new(c)
  end
end

#remove(name:, **options) ⇒ Object

Removes given cookie.

Examples:

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

Parameters:

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

    Additional keyword arguments.

Options Hash (**options):

  • :domain (String)
  • :url (String)


148
149
150
151
152
153
154
155
156
157
# File 'lib/ferrum/cookies.rb', line 148

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

  options = options.merge(name: name)
  options[:domain] ||= default_domain

  @page.command("Network.deleteCookies", **options)

  true
end

#set(options) ⇒ Object

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.

  • :path (String)

    The path that the cookie is bound to.

  • :expires (Integer)

    When the cookie will expire.

  • :size (Integer)

    The size of the cookie.

  • :httponly (Boolean)

    Specifies whether the cookie ‘HttpOnly`.

  • :secure (Boolean)

    Specifies whether the cookie is marked as ‘Secure`.

  • :samesite (String)

    Specifies whether the cookie is ‘SameSite`.

  • :session (Boolean)

    Specifies whether the cookie is a session cookie.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ferrum/cookies.rb', line 117

def set(options)
  cookie = (
    options.is_a?(Cookie) ? options.attributes : options
  ).dup.transform_keys(&:to_sym)

  cookie[:domain] ||= default_domain

  cookie[:httpOnly] = cookie.delete(:httponly) if cookie.key?(:httponly)
  cookie[:sameSite] = cookie.delete(:samesite) if cookie.key?(:samesite)

  expires = cookie.delete(:expires).to_i
  cookie[:expires] = expires if expires.positive?

  @page.command("Network.setCookie", **cookie)["success"]
end