Class: Cellular::Backends::CoolSMS

Inherits:
Backend
  • Object
show all
Defined in:
lib/cellular/backends/cool_sms.rb

Overview

Cool SMS backend (www.coolsms.com)

Constant Summary collapse

GATEWAY_URL =
'https://sms.coolsmsc.dk/'.freeze

Class Method Summary collapse

Methods inherited from Backend

receive

Class Method Details

.coolsms_configObject



33
34
35
36
37
38
# File 'lib/cellular/backends/cool_sms.rb', line 33

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

.defaults_with(options) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/cellular/backends/cool_sms.rb', line 40

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

.deliver(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/cellular/backends/cool_sms.rb', line 10

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

.parse_response(response) ⇒ Object



26
27
28
29
30
31
# File 'lib/cellular/backends/cool_sms.rb', line 26

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

.recipients_batch(options) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/cellular/backends/cool_sms.rb', line 51

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