Class: Cellular::Backends::Sendega

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

Overview

Sendega backend (sendega.com)

Constant Summary collapse

GATEWAY_URL =
'https://smsc.sendega.com/Content.asmx?WSDL'.freeze

Class Method Summary collapse

Class Method Details

.defaults_with(options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cellular/backends/sendega.rb', line 69

def self.defaults_with(options)
  {
    sender: options[:sender],
    destination: options[:batch],
    pricegroup: options[:price] || 0, # default price to 0
    contentTypeID: 1,
    contentHeader: '',
    content: options[:message],
    ageLimit: 0,
    extID: '',
    sendDate: '',
    refID: '',
    priority: 0,
    gwID: 0,
    pid: 0,
    dcs: 0
  }.merge!(savon_config)
end

.deliver(options = {}, savon_options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/cellular/backends/sendega.rb', line 9

def self.deliver(options = {}, savon_options = {})
  # Send an SMS and return its initial delivery status code.
  #
  # Delivery status codes:
  #    0 -- Message is received and is being processed.
  # 1001 -- Not validated
  # 1003 -- Wrong format: pid/dcs
  # 1004 -- Erroneous typeid
  # 1020 -- Fromalpha too long
  # 1021 -- Fromnumber too long
  # 1022 -- Erroneous recipient, integer overflow
  # 1023 -- No message content submitted
  # 1024 -- Premium sms must have abbreviated number as sender
  # 1025 -- The message sender is not allowed
  # 1026 -- Balance to low
  # 1027 -- Message too long
  # 1028 -- Alphanumeric sender is not valid
  # 1099 -- Internal error
  # 9001 -- Username and password does not match
  # 9002 -- Account is closed
  # 9004 -- Http not enabled
  # 9005 -- Smpp not enabled
  # 9006 -- Ip not allowed
  # 9007 -- Demo account empty
  #
  # See Gate API Documentation:
  # http://controlpanel.sendega.no/Content/Sendega%20-%20API%20documentation%20v2.3.pdf

  savon_options[:wsdl] = GATEWAY_URL
  request_queue = {}
  client = Savon.client savon_options

  recipients_batch(options).each_with_index do |batch, index|
    options[:batch] = batch
    result = client.call(:send, message: defaults_with(options))

    request_queue[index] = {
      batch: batch,
      result: result,
      body: result.body[:send_response][:send_result],
      response: map_response(result.body[:send_response][:send_result])
    }
  end

  # for now just resturn first response
  request_queue[0][:response]
end

.map_response(body) ⇒ Object



88
89
90
91
# File 'lib/cellular/backends/sendega.rb', line 88

def self.map_response(body)
  msg = body[:success] ? success_message : body[:error_message]
  [body[:error_number].to_i, msg]
end

.receive(_data) ⇒ Object

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/cellular/backends/sendega.rb', line 57

def self.receive(_data)
  raise NotImplementedError
end

.recipients_batch(options) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/cellular/backends/sendega.rb', line 97

def self.recipients_batch(options)
  if options[:recipients].blank?
    [options[:recipient]]
  else
    options[:recipients].each_slice(100).to_a.map { |x| x.join(',') }
  end
end

.savon_configObject



61
62
63
64
65
66
67
# File 'lib/cellular/backends/sendega.rb', line 61

def self.savon_config
  {
    username: Cellular.config.username,
    password: Cellular.config.password,
    dlrUrl: Cellular.config.delivery_url
  }
end

.success_messageObject



93
94
95
# File 'lib/cellular/backends/sendega.rb', line 93

def self.success_message
  'Message is received and is being processed.'
end