Class: MoteSMS::TwilioTransport
- Inherits:
-
Object
- Object
- MoteSMS::TwilioTransport
- 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
-
#account_sid ⇒ Object
readonly
Returns the value of attribute account_sid.
-
#auth_token ⇒ Object
readonly
Returns the value of attribute auth_token.
-
#from_number ⇒ Object
readonly
Returns the value of attribute from_number.
Instance Method Summary collapse
-
#deliver(message, _options = {}) ⇒ Object
Public: Delivers message using mobile technics HTTP/S API.
-
#initialize(account_sid, auth_token, from_number = nil) ⇒ TwilioTransport
constructor
Public: Create a new instance using specified endpoint, api_key and password.
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(account_sid, auth_token, from_number = nil) @account_sid = account_sid @auth_token = auth_token @from_number = from_number end |
Instance Attribute Details
#account_sid ⇒ Object (readonly)
Returns the value of attribute account_sid.
26 27 28 |
# File 'lib/mote_sms/transports/twilio_transport.rb', line 26 def account_sid @account_sid end |
#auth_token ⇒ Object (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_number ⇒ Object (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.
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(, = {}) raise ArgumentError, "too many recipients, max. is #{MAX_RECIPIENT}" if .to.length > MAX_RECIPIENT from = .from.present? ? .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(account_sid, auth_token) = prepare_numbers(.to).map do |n| client..create( from: from, to: n, body: .body ) end numbers = .map do |result| result.try(:to) end NumberList.new.push numbers.compact end |