Class: Whatsapp::Bridge

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp/bridge.rb

Overview

HTTP + WebSocket communication layer with the Node.js bridge.

Sends commands via HTTP POST and receives real-time events via WebSocket.

Examples:

bridge = Whatsapp::Bridge.new(connection)
bridge.open_websocket!
bridge.on_event { |event, data| puts "#{event}: #{data}" }
bridge.post("/messages/send", { to: "33612345678", text: "Hello" })

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, config: Whatsapp.configuration) ⇒ Bridge

Returns a new instance of Bridge.

Parameters:

  • connection (Connection)

    active connection to the Node bridge

  • config (Configuration) (defaults to: Whatsapp.configuration)

    gem configuration (for HTTP timeouts)



22
23
24
25
26
27
# File 'lib/whatsapp/bridge.rb', line 22

def initialize(connection, config: Whatsapp.configuration)
  @connection = connection
  @config = config
  @ws = nil
  @event_callbacks = []
end

Instance Attribute Details

#connectionConnection (readonly)

Returns the underlying connection to the Node bridge.

Returns:

  • (Connection)

    the underlying connection to the Node bridge



18
19
20
# File 'lib/whatsapp/bridge.rb', line 18

def connection
  @connection
end

Instance Method Details

#close!void

This method returns an undefined value.

Close the WebSocket connection and clear all callbacks.



125
126
127
128
129
# File 'lib/whatsapp/bridge.rb', line 125

def close!
  @ws&.close
  @ws = nil
  @event_callbacks.clear
end

#get(path) ⇒ Hash

Send an HTTP GET request to the bridge.

Parameters:

  • path (String)

    API path (e.g. "/status")

Returns:

  • (Hash)

    parsed response body



69
70
71
72
73
74
75
76
77
# File 'lib/whatsapp/bridge.rb', line 69

def get(path)
  uri = URI("#{connection.base_url}#{path}")
  req = Net::HTTP::Get.new(uri)

  res = http_request(uri, req)
  parsed = JSON.parse(res.body, symbolize_names: true)

  handle_response(res, parsed)
end

#on_event {|event, data| ... } ⇒ void

This method returns an undefined value.

Register a callback for bridge events.

Yields:

  • (event, data)

    called for each event received via WebSocket

Yield Parameters:

  • event (String)

    event name

  • data (Hash, nil)

    event payload



118
119
120
# File 'lib/whatsapp/bridge.rb', line 118

def on_event(&block)
  @event_callbacks << block
end

#open_websocket!void

This method returns an undefined value.

Open a WebSocket connection for receiving real-time events.



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/whatsapp/bridge.rb', line 89

def open_websocket!
  @ws = WebSocket::Client::Simple.connect(connection.ws_url)

  # websocket-client-simple uses instance_exec in its callbacks,
  # which changes self. Capture into local variables instead.
  callbacks = @event_callbacks

  @ws.on(:message) do |msg|
    next if msg.data.empty?

    data = JSON.parse(msg.data, symbolize_names: true)
    callbacks.each { |cb| cb.call(data[:event], data[:data]) }
  end

  @ws.on(:error) do |e|
    callbacks.each { |cb| cb.call("bridge.error", {error: e.message}) }
  end

  @ws.on(:close) do
    callbacks.each { |cb| cb.call("bridge.closed", nil) }
  end
end

#post(path, body = {}) ⇒ Hash

Send an HTTP POST request to the bridge.

Parameters:

  • path (String)

    API path (e.g. "/messages/send")

  • body (Hash) (defaults to: {})

    request body (automatically JSON-encoded)

Returns:

  • (Hash)

    parsed response body

Raises:



37
38
39
40
41
42
43
44
45
46
# File 'lib/whatsapp/bridge.rb', line 37

def post(path, body = {})
  uri = URI("#{connection.base_url}#{path}")
  req = Net::HTTP::Post.new(uri, "Content-Type" => "application/json")
  req.body = body.compact.to_json

  res = http_request(uri, req)
  parsed = JSON.parse(res.body, symbolize_names: true)

  handle_response(res, parsed)
end

#put(path, body = {}) ⇒ Hash

Send an HTTP PUT request to the bridge.

Parameters:

  • path (String)

    API path

  • body (Hash) (defaults to: {})

    request body (automatically JSON-encoded)

Returns:

  • (Hash)

    parsed response body

Raises:



54
55
56
57
58
59
60
61
62
63
# File 'lib/whatsapp/bridge.rb', line 54

def put(path, body = {})
  uri = URI("#{connection.base_url}#{path}")
  req = Net::HTTP::Put.new(uri, "Content-Type" => "application/json")
  req.body = body.compact.to_json

  res = http_request(uri, req)
  parsed = JSON.parse(res.body, symbolize_names: true)

  handle_response(res, parsed)
end

#statusHash

Get the bridge status.

Returns:

  • (Hash)

    bridge and WhatsApp connection status



82
83
84
# File 'lib/whatsapp/bridge.rb', line 82

def status
  get("/status")
end