Class: Google::Chrome::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Client

Create a Client object and establish the debugger connection.



19
20
21
22
# File 'lib/google/chrome/client.rb', line 19

def initialize(host, port)
  @socket = TCPSocket.open(host, port)
  handshake
end

Instance Method Details

#extension(id) ⇒ Object



80
81
82
# File 'lib/google/chrome/client.rb', line 80

def extension(id)
  ExtensionPorts.new(self, id)
end

#read_all_responseObject

Read all responses from the server.



27
28
29
30
31
32
33
34
35
# File 'lib/google/chrome/client.rb', line 27

def read_all_response
  result = []

  while IO.select([ @socket ], [], [], 0.1)
    result << read_response
  end

  result
end

#read_responseObject

Read one response from the server.



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/google/chrome/client.rb', line 40

def read_response
  header = {}

  while ln = @socket.gets
    case ln
    when "\r\n"
      break
    when /^([A-Za-z-]+):(.*)$/
      header[$1] = $2.chomp
    else
      raise
    end
  end

  body = JSON.parse(@socket.read(header['Content-Length'].to_i))
  return header, body
end

#request(header, body) ⇒ Object

Send a request.



61
62
63
64
# File 'lib/google/chrome/client.rb', line 61

def request(header, body)
  write_request(header, body)
  return read_response
end

#server_versionObject



66
67
68
69
70
# File 'lib/google/chrome/client.rb', line 66

def server_version
  header, resp = request({ 'Tool' => 'DevToolsService' },
                         { 'command' => 'version' })
  resp['data']
end

#tabsObject



72
73
74
75
76
77
78
# File 'lib/google/chrome/client.rb', line 72

def tabs
  header, resp = request({ 'Tool' => 'DevToolsService' },
                         { 'command' => 'list_tabs' })
  resp['data'].map do |ary|
    Tab.new(self, ary[0].to_i)
  end
end