Class: Providers::Twilio

Inherits:
SmsProvider show all
Defined in:
lib/multi_sms/providers/twilio.rb

Constant Summary collapse

API_VERSION =
"2010-04-01"
BASE_DOMAIN =
"api.twilio.com"

Instance Method Summary collapse

Methods inherited from SmsProvider

#config

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Providers::SmsProvider

Instance Method Details

#send(parameters) ⇒ Object

Send SMS Required parameters

  • :from - Either the one of the allocated numbers or arbitrary alphanumeric string of at most 11 characters

  • :to - Any phone number capable of receiving SMS

  • :message - Any UTF-8 text Splitting and joining multi-part SMS messages are automatically handled by the API



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/multi_sms/providers/twilio.rb', line 19

def send(parameters)
  base_url = "https://#{config.twilio.}:#{config.twilio.auth_token}@#{BASE_DOMAIN}/#{API_VERSION}"
  path = "/Accounts/#{config.twilio.}/SMS/Messages.json"

  response = RestClient.post base_url + path, :To => parameters[:to], :Body => parameters[:message], :From => parameters[:from]
  MultiSms.parse_json(response.body)
rescue RestClient::Unauthorized
  raise AuthError, "Authentication failed"
rescue RestClient::InternalServerError
  raise ServerError, "Server error"
rescue RestClient::Forbidden => e
  raise BadRequest, e.http_body
end

#usable?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/multi_sms/providers/twilio.rb', line 8

def usable?
  config.twilio..present? and config.twilio.auth_token.present?
end