Class: WhatsAppNotifier::WebAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp_notifier/web_adapter.rb

Constant Summary collapse

DEFAULT_OPEN_TIMEOUT =
5
DEFAULT_READ_TIMEOUT =
30

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_url: self.class.default_base_url, open_timeout: DEFAULT_OPEN_TIMEOUT, read_timeout: DEFAULT_READ_TIMEOUT) ⇒ WebAdapter

Returns a new instance of WebAdapter.



14
15
16
17
18
19
20
# File 'lib/whatsapp_notifier/web_adapter.rb', line 14

def initialize(base_url: self.class.default_base_url,
               open_timeout: DEFAULT_OPEN_TIMEOUT,
               read_timeout: DEFAULT_READ_TIMEOUT)
  @base_url = base_url
  @open_timeout = open_timeout
  @read_timeout = read_timeout
end

Class Method Details

.default_base_urlObject



10
11
12
# File 'lib/whatsapp_notifier/web_adapter.rb', line 10

def self.default_base_url
  ENV["WHATSAPP_NOTIFIER_SERVICE_URL"] || ENV["WHATSAPP_SERVICE_URL"] || "http://127.0.0.1:3001"
end

Instance Method Details

#connection_status(metadata: {}) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/whatsapp_notifier/web_adapter.rb', line 45

def connection_status(metadata: {})
  user_id = user_id_from()
  response = request(:get, "/status/#{user_id}")
  {
    state: response["state"],
    authenticated: response["authenticated"],
    has_qr: response["hasQR"]
  }
end

#fetch_inbound(metadata: {}) ⇒ Object

Drains the service’s pending inbound queue for this user. The service returns the messages once, then clears them (at-least-once handoff —callers must dedupe on message_id). Accepts either a bare array or a { “messages” => […] } envelope so the wire format can evolve.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/whatsapp_notifier/web_adapter.rb', line 59

def fetch_inbound(metadata: {})
  user_id = user_id_from()
  response = request(:get, "/inbound/#{user_id}")
  raw = response.is_a?(Hash) ? response["messages"] : response
  Array(raw).map do |m|
    {
      from: m["from"],
      body: m["body"],
      message_id: m["messageId"] || m["message_id"],
      timestamp: m["timestamp"],
      type: m["type"]
    }
  end
end

#fetch_qr_code(metadata: {}) ⇒ Object



39
40
41
42
43
# File 'lib/whatsapp_notifier/web_adapter.rb', line 39

def fetch_qr_code(metadata: {})
  user_id = user_id_from()
  response = request(:get, "/qr/#{user_id}")
  response["qr"]
end

#logout(metadata: {}) ⇒ Object

Logs the user out of WhatsApp and clears their saved session on the service.



75
76
77
78
79
# File 'lib/whatsapp_notifier/web_adapter.rb', line 75

def logout(metadata: {})
  user_id = user_id_from()
  response = request(:post, "/logout/#{user_id}")
  { success: response.fetch("success", false) }
end

#send_message(payload:, session: {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/whatsapp_notifier/web_adapter.rb', line 22

def send_message(payload:, session: {})
  user_id = user_id_from(payload[:metadata] || {})
  body = {
    to: payload[:to],
    message: payload[:body],
    mediaUrl: payload.dig(:metadata, :media_url)
  }.compact

  response = request(:post, "/send/#{user_id}", body: body)
  {
    success: response.fetch("success"),
    message_id: payload[:idempotency_key] || "local-#{Time.now.to_i}",
    session: session,
    error_message: response["error"]
  }
end