Class: ChromeRemote::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/chrome_remote/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws_url) ⇒ Client

Returns a new instance of Client.



7
8
9
10
# File 'lib/chrome_remote/client.rb', line 7

def initialize(ws_url)
  @ws = WebSocketClient.new(ws_url)
  @handlers = Hash.new {|hash, key| hash[key] = [] }
end

Instance Attribute Details

#handlersObject (readonly)

Returns the value of attribute handlers.



5
6
7
# File 'lib/chrome_remote/client.rb', line 5

def handlers
  @handlers
end

#wsObject (readonly)

Returns the value of attribute ws.



5
6
7
# File 'lib/chrome_remote/client.rb', line 5

def ws
  @ws
end

Instance Method Details

#listenObject



29
30
31
# File 'lib/chrome_remote/client.rb', line 29

def listen
  read_until { false }
end

#listen_until(&block) ⇒ Object



25
26
27
# File 'lib/chrome_remote/client.rb', line 25

def listen_until(&block)
  read_until { block.call }
end

#on(event_name, &block) ⇒ Object



21
22
23
# File 'lib/chrome_remote/client.rb', line 21

def on(event_name, &block)
  handlers[event_name] << block
end

#send_cmd(command, params = {}) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/chrome_remote/client.rb', line 12

def send_cmd(command, params = {})  
  msg_id = generate_unique_id
  
  ws.send_msg({method: command, params: params, id: msg_id}.to_json)
  
  msg = read_until { |msg| msg["id"] == msg_id }
  msg["result"]
end

#wait_for(event_name) ⇒ Object



33
34
35
36
# File 'lib/chrome_remote/client.rb', line 33

def wait_for(event_name)
  msg = read_until { |msg| msg["method"] == event_name }
  msg["params"]
end