Sha256: 7331c20824fa70eadce531b45c7317d2260eceba5227f01a9dbe1dc680739fca

Contents?: true

Size: 1.47 KB

Versions: 2

Compression:

Stored size: 1.47 KB

Contents

require 'httparty'

module Cellular
  module Backends
    class CoolSMS < Backend

      # Documentation: http://www.coolsms.com/support/dokumentation/http-gateway.sms
      GATEWAY_URL = 'https://sms.coolsmsc.dk/'

      def self.deliver(options = {})
        request_queue = {}

        recipients_batch(options).each_with_index do |recipient, index|
          options[:batch] = recipient
          result = HTTParty.get(GATEWAY_URL, query: defaults_with(options) )
          request_queue[index] = {
            recipient: recipient,
            response: parse_response(result.parsed_response['smsc'])
          }
        end

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

      def self.parse_response(response)
        [
          response['status'],
          response['result'] || response['message']['result']
        ]
      end

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

      def self.defaults_with(options)
        {
          from: options[:sender],
          to: options[:batch],
          message: options[:message],
          charset: 'utf-8',
          resulttype: 'xml',
          lang: 'en'
        }.merge!(coolsms_config)
      end

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

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cellular-2.1.0 lib/cellular/backends/cool_sms.rb
cellular-2.0.0 lib/cellular/backends/cool_sms.rb