Class: Lightpanda::Network

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(browser) ⇒ Network



7
8
9
10
11
# File 'lib/lightpanda/network.rb', line 7

def initialize(browser)
  @browser = browser
  @traffic = []
  @enabled = false
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



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

def browser
  @browser
end

Instance Method Details

#clearObject



32
33
34
# File 'lib/lightpanda/network.rb', line 32

def clear
  @traffic.clear
end

#disableObject



21
22
23
24
25
26
# File 'lib/lightpanda/network.rb', line 21

def disable
  return unless @enabled

  browser.command("Network.disable")
  @enabled = false
end

#enableObject



13
14
15
16
17
18
19
# File 'lib/lightpanda/network.rb', line 13

def enable
  return if @enabled

  browser.command("Network.enable")
  subscribe
  @enabled = true
end

#trafficObject



28
29
30
# File 'lib/lightpanda/network.rb', line 28

def traffic
  @traffic.dup
end

#wait_for_idle(timeout: 5, connections: 0) ⇒ Object

rubocop:disable Naming/PredicateMethod



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lightpanda/network.rb', line 36

def wait_for_idle(timeout: 5, connections: 0) # rubocop:disable Naming/PredicateMethod
  started_at = Time.now

  while Time.now - started_at < timeout
    pending = @traffic.count { |t| t[:response].nil? }
    return true if pending <= connections

    sleep 0.1
  end

  false
end