Class: MoteSMS::TwilioTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/mote_sms/transports/twilio_transport.rb

Overview

MoteSMS::TwilioTransport provides the implementation to send messages using the Twilio Api github.com/twilio/twilio-ruby

Examples:

MoteSMS.transport = MoteSMS::TwilioTransport.new 'sid', 'token', 'from_number'
sms = MoteSMS::Message.new do
  to 'to_number'
  body 'my cool text'
end
sms.deliver_now
# => <Twilio::REST::Message>

Constant Summary collapse

MAX_RECIPIENT =

Maximum recipients allowed by API

1
ServiceError =

Custom exception subclass.

Class.new(::Exception)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_sid, auth_token, from_number = nil) ⇒ TwilioTransport

Public: Create a new instance using specified endpoint, api_key and password.

account_sid - The twilio account sid auth_token - The twilio api token from_number - The phone number to send from (mandatory on initialize or send message)

Returns a new instance.



36
37
38
39
40
# File 'lib/mote_sms/transports/twilio_transport.rb', line 36

def initialize(, auth_token, from_number = nil)
  @account_sid = 
  @auth_token = auth_token
  @from_number = from_number
end

Instance Attribute Details

#account_sidObject (readonly)

Returns the value of attribute account_sid.



26
27
28
# File 'lib/mote_sms/transports/twilio_transport.rb', line 26

def 
  @account_sid
end

#auth_tokenObject (readonly)

Returns the value of attribute auth_token.



26
27
28
# File 'lib/mote_sms/transports/twilio_transport.rb', line 26

def auth_token
  @auth_token
end

#from_numberObject (readonly)

Returns the value of attribute from_number.



26
27
28
# File 'lib/mote_sms/transports/twilio_transport.rb', line 26

def from_number
  @from_number
end

Instance Method Details

#deliver(message, _options = {}) ⇒ Object

Public: Delivers message using mobile technics HTTP/S API.

message - The MoteSMS::Message instance to send. options - The Hash with service specific options.

Returns Array with sender ids.

Raises:

  • (ArgumentError)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mote_sms/transports/twilio_transport.rb', line 48

def deliver(message, _options = {})
  raise ArgumentError, "too many recipients, max. is #{MAX_RECIPIENT}" if message.to.length > MAX_RECIPIENT

  from = message.from.present? ? message.from.to_s : from_number.to_s

  raise ArgumentError, 'no from number given on new message or the transport given' if from.empty?

  client = Twilio::REST::Client.new(, auth_token)
  messages = prepare_numbers(message.to).map do |n|
    client.messages.create(
      from: from,
      to: n,
      body: message.body
    )
  end
  numbers = messages.map do |result|
    result.try(:to)
  end
  NumberList.new.push numbers.compact
end