Class: Lightpanda::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/lightpanda/client.rb,
lib/lightpanda/client/subscriber.rb,
lib/lightpanda/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.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lightpanda/client.rb', line 13

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

  start_message_thread
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/lightpanda/client.rb', line 11

def options
  @options
end

#ws_urlObject (readonly)

Returns the value of attribute ws_url.



11
12
13
# File 'lib/lightpanda/client.rb', line 11

def ws_url
  @ws_url
end

Instance Method Details

#closeObject



56
57
58
59
60
61
62
# File 'lib/lightpanda/client.rb', line 56

def close
  @running = false
  @message_thread&.kill
  @ws&.close
  @subscriber.clear
  @pendings.clear
end

#closed?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/lightpanda/client.rb', line 64

def closed?
  @ws.closed?
end

#command(method, params = {}, async: false, session_id: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/lightpanda/client.rb', line 25

def command(method, params = {}, async: false, session_id: nil)
  message = build_message(method, params, session_id: session_id)

  if async
    @ws.send_message(JSON.generate(message))
    return true
  end

  pending = Concurrent::IVar.new
  @pendings[message[:id]] = pending

  @ws.send_message(JSON.generate(message))

  response = pending.value!(@options.timeout)
  raise TimeoutError, "Command #{method} timed out after #{@options.timeout}s" if response.nil?

  handle_error(response) if response["error"]

  response["result"]
ensure
  @pendings.delete(message[:id]) if message
end

#off(event, block = nil) ⇒ Object



52
53
54
# File 'lib/lightpanda/client.rb', line 52

def off(event, block = nil)
  @subscriber.unsubscribe(event, block)
end

#on(event) ⇒ Object



48
49
50
# File 'lib/lightpanda/client.rb', line 48

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