Class: OpenWire::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/open_wire/client.rb

Overview

HTTP client for the Open-Wire gateway (outbound message.send).

Instance Method Summary collapse

Constructor Details

#initialize(base_url: nil, api_key: nil) ⇒ Client

Returns a new instance of Client.

Raises:



6
7
8
9
10
# File 'lib/open_wire/client.rb', line 6

def initialize(base_url: nil, api_key: nil)
  @base_url = (base_url || OpenWire.configuration.base_url).to_s.sub(%r{/+\z}, "")
  @api_key = api_key || OpenWire.configuration.api_key
  raise ConfigurationError, "OPEN_WIRE_API_KEY / api_key is required" if @api_key.to_s.empty?
end

Instance Method Details

#healthObject



39
40
41
42
43
# File 'lib/open_wire/client.rb', line 39

def health
  uri = URI("#{@base_url}/api/v1/health")
  res = Net::HTTP.get_response(uri)
  JSON.parse(res.body)
end

#send_message(installation_id:, to:, text: nil, body: nil, thread_id: nil, blocks: nil) ⇒ Object

Send a message through an installation.

client.send_message(
installation_id: "inst_…",
to: { id: "C123", kind: "channel" },
text: "Hello",
thread_id: "C123:1710000000.000100"
)


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

def send_message(installation_id:, to:, text: nil, body: nil, thread_id: nil, blocks: nil)
  payload_body = body || {}
  payload_body = payload_body.merge("text" => text) if text
  payload_body = payload_body.merge("blocks" => blocks) if blocks

  envelope = {
    "protocol" => OpenWire::PROTOCOL,
    "type" => "message.send",
    "installation_id" => installation_id,
    "to" => stringify_keys(to),
    "body" => stringify_keys(payload_body)
  }
  if thread_id
    envelope["thread"] = { "id" => thread_id }
  end

  request_json(:post, "/api/v1/messages", envelope)
end