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.



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

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.



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

def handlers
  @handlers
end

#wsObject (readonly)

Returns the value of attribute ws.



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

def ws
  @ws
end

Instance Method Details

#listenObject



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

def listen
  read_until { false }
end

#listen_until(&block) ⇒ Object



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

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

#on(event_name, &block) ⇒ Object



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

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

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



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

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 }
  Hashie::Mash.new(msg["result"])
end

#wait_for(event_name = nil) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/chrome_remote/client.rb', line 34

def wait_for(event_name=nil)
  if event_name
    msg = read_until { |msg| msg["method"] == event_name }
  elsif block_given?
    msg = read_until { |msg| yield(msg["method"], msg["params"]) }
  end
  Hashie::Mash.new(msg["params"])
end