Class: BabySMS::Adapters::NexmoAdapter

Inherits:
BabySMS::Adapter show all
Defined in:
lib/babysms/adapters/nexmo_adapter.rb

Defined Under Namespace

Classes: WebHook

Instance Attribute Summary

Attributes inherited from BabySMS::Adapter

#client, #from

Instance Method Summary collapse

Methods inherited from BabySMS::Adapter

#adapter_id, adapter_name, #adapter_name, for_adapter_id, for_number, #web_hook, #web_hook?, #web_hook_class

Constructor Details

#initialize(api_key:, api_secret:, from:) ⇒ NexmoAdapter

Returns a new instance of NexmoAdapter.



8
9
10
11
12
# File 'lib/babysms/adapters/nexmo_adapter.rb', line 8

def initialize(api_key:, api_secret:, from:)
  super(from: from)

  self.client = Nexmo::Client.new(api_key: api_key, api_secret: api_secret)
end

Instance Method Details

#deliver(message) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/babysms/adapters/nexmo_adapter.rb', line 14

def deliver(message)
  # Thanks for being weird, Nexmo.  Rejects numbers starting with "+"
  response = client.sms.send(from: from.gsub(/\A\+/, ''),
                             to: message.to,
                             text: message.contents)
  if response.messages.first.status != '0'
    raise BabySMS::FailedDelivery.new(response.messages.first.error_text,
                                      adapter: self)
  end

  response.messages.first.message_id
end