Class: Ferrum::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ferrum/client.rb,
lib/ferrum/client/subscriber.rb,
lib/ferrum/client/web_socket.rb

Defined Under Namespace

Classes: Subscriber, WebSocket

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws_url, options) ⇒ Client

Returns a new instance of Client.



62
63
64
65
66
67
68
69
70
71
# File 'lib/ferrum/client.rb', line 62

def initialize(ws_url, options)
  @command_id = 0
  @ws_url = ws_url
  @options = options
  @pendings = Concurrent::Hash.new
  @ws = WebSocket.new(ws_url, options.ws_max_receive_size, options.logger)
  @subscriber = Subscriber.new

  start
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



60
61
62
# File 'lib/ferrum/client.rb', line 60

def options
  @options
end

#subscriberObject (readonly)

Returns the value of attribute subscriber.



60
61
62
# File 'lib/ferrum/client.rb', line 60

def subscriber
  @subscriber
end

#ws_urlObject (readonly)

Returns the value of attribute ws_url.



60
61
62
# File 'lib/ferrum/client.rb', line 60

def ws_url
  @ws_url
end

Instance Method Details

#build_message(method, params) ⇒ Object



125
126
127
# File 'lib/ferrum/client.rb', line 125

def build_message(method, params)
  { method: method, params: params }.merge(id: next_command_id)
end

#closeObject



110
111
112
113
114
115
116
# File 'lib/ferrum/client.rb', line 110

def close
  @ws.close
  # Give a thread some time to handle a tail of messages
  @pendings.clear
  @thread.kill unless @thread.join(1)
  @subscriber.close
end

#command(method, async: false, **params) ⇒ Object



73
74
75
76
# File 'lib/ferrum/client.rb', line 73

def command(method, async: false, **params)
  message = build_message(method, params)
  send_message(message, async: async)
end

#inspectObject



118
119
120
121
122
123
# File 'lib/ferrum/client.rb', line 118

def inspect
  "#<#{self.class} " \
    "@command_id=#{@command_id.inspect} " \
    "@pendings=#{@pendings.inspect} " \
    "@ws=#{@ws.inspect}>"
end

#on(event, &block) ⇒ Object



98
99
100
# File 'lib/ferrum/client.rb', line 98

def on(event, &block)
  @subscriber.on(event, &block)
end

#send_message(message, async:) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ferrum/client.rb', line 78

def send_message(message, async:)
  if async
    @ws.send_message(message)
    true
  else
    pending = Concurrent::IVar.new
    @pendings[message[:id]] = pending
    @ws.send_message(message)
    data = pending.value!(timeout)
    @pendings.delete(message[:id])

    raise DeadBrowserError if data.nil? && @ws.messages.closed?
    raise TimeoutError unless data

    error, response = data.values_at("error", "result")
    raise_browser_error(error) if error
    response
  end
end

#session(session_id) ⇒ Object



106
107
108
# File 'lib/ferrum/client.rb', line 106

def session(session_id)
  SessionClient.new(self, session_id)
end

#subscribed?(event) ⇒ Boolean

Returns:

  • (Boolean)


102
103
104
# File 'lib/ferrum/client.rb', line 102

def subscribed?(event)
  @subscriber.subscribed?(event)
end