Class: Stealth::Services::Bandwidth::Client

Inherits:
Stealth::Services::BaseClient
  • Object
show all
Defined in:
lib/stealth/services/bandwidth/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reply:) ⇒ Client

Returns a new instance of Client.



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/stealth/services/bandwidth/client.rb', line 16

def initialize(reply:)
  @reply = reply
   = Stealth.config.bandwidth.
  username = Stealth.config.bandwidth.api_username
  password = Stealth.config.bandwidth.api_password
  application_id = Stealth.config.bandwidth.application_id
  @endpoint = "https://messaging.bandwidth.com/api/v2/users/#{}/messages"
  @http_client = HTTP
                  .timeout(connect: 15, read: 30)
                  .basic_auth(user: username, pass: password)
                  .headers('Content-Type' => 'application/json')
end

Instance Attribute Details

#endpointObject (readonly)

Returns the value of attribute endpoint.



14
15
16
# File 'lib/stealth/services/bandwidth/client.rb', line 14

def endpoint
  @endpoint
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



14
15
16
# File 'lib/stealth/services/bandwidth/client.rb', line 14

def http_client
  @http_client
end

#replyObject (readonly)

Returns the value of attribute reply.



14
15
16
# File 'lib/stealth/services/bandwidth/client.rb', line 14

def reply
  @reply
end

Instance Method Details

#transmitObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stealth/services/bandwidth/client.rb', line 29

def transmit
  # Don't transmit anything for delays
  return true if reply.blank?

  json_reply = Oj.dump(reply, mode: :compat)
  response = http_client.post(endpoint, body: json_reply)

  Stealth::Logger.l(
    topic: 'bandwidth',
    message: "Transmitting. Response: #{response.status.code}: #{response.body}"
  )
end