Sha256: 0243ef2ace7e247a3e72893ca809c9cf62ca52d8f031b265ca72b98830104c6c

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

require 'rails'
require 'active_record'
require 'notifiable'
require 'twilio-ruby'

module Notifiable
  module Sms
    class Twilio < Notifiable::NotifierBase
      
      attr_accessor :account_sid, :auth_token, :sms_originator
      
			protected      
  			def enqueue(device_token, localized_notification)        				
        
          raise "Account SID missing" if account_sid.nil?
          raise "Auth token missing" if auth_token.nil?
          raise "Auth token missing" if sms_originator.nil?
        
          result = twilio_client.account.messages.create({
          	:from => sms_originator, 
          	:to => device_token.token, 
          	:body => localized_notification.message,  
          })
          
          if result.error_code == nil
            processed(device_token, 0)
          else
            processed(device_token, result.error_code.to_i)            
          end
  			end
        
      private
        def twilio_client
  				@twilio_client ||= ::Twilio::REST::Client.new(account_sid, auth_token)
        end
    end
  end
end

Notifiable.notifier_classes[:sms] = Notifiable::Sms::Twilio

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
notifiable-sms-twilio-0.1.0 lib/notifiable/sms/twilio.rb