Class: Whatsapp::Bridge
- Inherits:
-
Object
- Object
- Whatsapp::Bridge
- 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.
Instance Attribute Summary collapse
-
#connection ⇒ Connection
readonly
The underlying connection to the Node bridge.
Instance Method Summary collapse
-
#close! ⇒ void
Close the WebSocket connection and clear all callbacks.
-
#get(path) ⇒ Hash
Send an HTTP GET request to the bridge.
-
#initialize(connection, config: Whatsapp.configuration) ⇒ Bridge
constructor
A new instance of Bridge.
-
#on_event {|event, data| ... } ⇒ void
Register a callback for bridge events.
-
#open_websocket! ⇒ void
Open a WebSocket connection for receiving real-time events.
-
#post(path, body = {}) ⇒ Hash
Send an HTTP POST request to the bridge.
-
#put(path, body = {}) ⇒ Hash
Send an HTTP PUT request to the bridge.
-
#status ⇒ Hash
Get the bridge status.
Constructor Details
#initialize(connection, config: Whatsapp.configuration) ⇒ Bridge
Returns a new instance of Bridge.
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
#connection ⇒ Connection (readonly)
Returns 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.
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.
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.}) } 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.
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.
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 |
#status ⇒ Hash
Get the bridge status.
82 83 84 |
# File 'lib/whatsapp/bridge.rb', line 82 def status get("/status") end |