Sha256: 919e2d69d28e1e926e47c95a34e161d796129be057c38c39c9148345a2a047a8

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

require 'httparty'

module Cellular
  module Backends
    class Twilio < Backend
      # Documentation: https://www.twilio.com/docs/api/rest
      API_VERSION = '2010-04-01'
      BASE_URL = 'https://api.twilio.com/'
      API_URL = BASE_URL + API_VERSION

      HTTP_HEADERS = {
        'Accept' => 'application/json',
        'Accept-Charset' => 'utf-8',
        'User-Agent' => "cellular/#{Cellular::VERSION}" \
        " (#{RUBY_ENGINE}/#{RUBY_PLATFORM}" \
        " #{RUBY_VERSION}-p#{RUBY_PATCHLEVEL})"
      }

      def self.deliver(options = {})
        request_queue = {}
        recipients_batch(options).each_with_index do |recipient, index|
          options[:batch] = recipient
          request = HTTParty.post(
            sms_url,
            body: payload(options),
            basic_auth: twilio_config,
            headers: HTTP_HEADERS
          )

          request_queue[index] = {
            recipient: options[:batch],
            response: parse_response(request)
          }
        end

        # return first response for now
        request_queue[0][:response]
      end

      def self.parse_response(response)
        [
          response.code,
          response.message
        ]
      end

      def self.sms_url
        "#{API_URL}/Accounts/#{twilio_config[:username]}/Messages"
      end

      def self.twilio_config
        {
          username: Cellular.config.username,
          password: Cellular.config.password
        }
      end

      def self.payload(options)
        {
          From: options[:sender],
          To: options[:batch],
          Body: options[:message],
          MaxPrice: options[:price] || 0.50
        }
      end

      def self.recipients_batch(options)
        if options[:recipients].blank?
          [options[:recipient]]
        else
          options[:recipients]
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cellular-2.1.0 lib/cellular/backends/twilio.rb