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.



69
70
71
72
73
74
75
76
77
78
# File 'lib/ferrum/client.rb', line 69

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.



67
68
69
# File 'lib/ferrum/client.rb', line 67

def options
  @options
end

#subscriberObject (readonly)

Returns the value of attribute subscriber.



67
68
69
# File 'lib/ferrum/client.rb', line 67

def subscriber
  @subscriber
end

#ws_urlObject (readonly)

Returns the value of attribute ws_url.



67
68
69
# File 'lib/ferrum/client.rb', line 67

def ws_url
  @ws_url
end

Instance Method Details

#build_message(method, params) ⇒ Object



136
137
138
# File 'lib/ferrum/client.rb', line 136

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

#closeObject



121
122
123
124
125
126
127
# File 'lib/ferrum/client.rb', line 121

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



80
81
82
83
# File 'lib/ferrum/client.rb', line 80

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

#inspectObject



129
130
131
132
133
134
# File 'lib/ferrum/client.rb', line 129

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

#off(event, id) ⇒ Object



109
110
111
# File 'lib/ferrum/client.rb', line 109

def off(event, id)
  @subscriber.off(event, id)
end

#on(event) ⇒ Object



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

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

#send_message(message, async:) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ferrum/client.rb', line 85

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



117
118
119
# File 'lib/ferrum/client.rb', line 117

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

#subscribed?(event) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/ferrum/client.rb', line 113

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