Class: Capybara::Lightpanda::Cookies

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

Defined Under Namespace

Classes: Cookie

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ Cookies

Returns a new instance of Cookies.



58
59
60
# File 'lib/capybara/lightpanda/cookies.rb', line 58

def initialize(browser)
  @browser = browser
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



56
57
58
# File 'lib/capybara/lightpanda/cookies.rb', line 56

def browser
  @browser
end

Instance Method Details

#allObject



62
63
64
65
# File 'lib/capybara/lightpanda/cookies.rb', line 62

def all
  result = browser.command("Network.getAllCookies")
  (result["cookies"] || []).map { |c| Cookie.new(c) }
end

#clearObject



102
103
104
# File 'lib/capybara/lightpanda/cookies.rb', line 102

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

#each(&block) ⇒ Object

Yields each Cookie. Powers ‘Enumerable` (so callers can do `cookies.find { … }`, `cookies.select { … }`, `cookies.to_a`, …without going through `all` first).



70
71
72
73
74
# File 'lib/capybara/lightpanda/cookies.rb', line 70

def each(&block)
  return enum_for(:each) unless block

  all.each(&block)
end

#get(name) ⇒ Object



76
77
78
# File 'lib/capybara/lightpanda/cookies.rb', line 76

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

#load(path = "cookies.yml") ⇒ Object

Load cookies from a YAML file produced by ‘store` and re-set them. CDP requires either domain or url for each cookie; entries from `store` already include domain, so they round-trip cleanly. Returns true on success (intentionally not a predicate — mirrors ferrum’s API).



116
117
118
119
120
# File 'lib/capybara/lightpanda/cookies.rb', line 116

def load(path = "cookies.yml") # rubocop:disable Naming/PredicateMethod
  cookies = YAML.load_file(path)
  cookies.each { |c| restore_cookie(c) }
  true
end

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



95
96
97
98
99
100
# File 'lib/capybara/lightpanda/cookies.rb', line 95

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/capybara/lightpanda/cookies.rb', line 80

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

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

Persist all current cookies to a YAML file (ferrum parity). Returns the number of bytes written.



108
109
110
# File 'lib/capybara/lightpanda/cookies.rb', line 108

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