Class: Lightpanda::Cookies

Inherits:
Object
  • Object
show all
Defined in:
lib/lightpanda/cookies.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ Cookies

Returns a new instance of Cookies.



7
8
9
# File 'lib/lightpanda/cookies.rb', line 7

def initialize(browser)
  @browser = browser
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



5
6
7
# File 'lib/lightpanda/cookies.rb', line 5

def browser
  @browser
end

Instance Method Details

#allObject



11
12
13
14
15
# File 'lib/lightpanda/cookies.rb', line 11

def all
  result = browser.command("Network.getAllCookies")

  result["cookies"] || []
end

#clearObject



43
44
45
# File 'lib/lightpanda/cookies.rb', line 43

def clear
  browser.command("Network.clearBrowserCookies")
end

#get(name) ⇒ Object



17
18
19
# File 'lib/lightpanda/cookies.rb', line 17

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

#remove(name:, domain: nil, path: "/") ⇒ Object



36
37
38
39
40
41
# File 'lib/lightpanda/cookies.rb', line 36

def remove(name:, domain: nil, path: "/")
  params = { name: name, path: path }
  params[:domain] = domain if domain

  browser.command("Network.deleteCookies", **params)
end

#set(name:, value:, domain: nil, path: "/", secure: false, http_only: false, expires: nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/lightpanda/cookies.rb', line 21

def set(name:, value:, domain: nil, path: "/", secure: false, http_only: false, expires: nil)
  params = {
    name: name,
    value: value,
    path: path,
    secure: secure,
    httpOnly: http_only
  }

  params[:domain] = domain if domain
  params[:expires] = expires.to_i if expires

  browser.command("Network.setCookie", **params)
end