Module: ActieSmsc

Defined in:
lib/actie_smsc.rb,
lib/actie_smsc/version.rb,
lib/actie_smsc/configuration.rb

Defined Under Namespace

Classes: Configuration, InvalidConfigurationError, SmscError

Constant Summary collapse

VERSION =
'0.0.1'

Class Method Summary collapse

Class Method Details

.balance(fmt: 1) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/actie_smsc.rb', line 117

def balance(fmt: 1)
  resp = request('balance', fmt: fmt)

  check_response_for_exception(resp.body)

  case fmt
  when 2,0
    resp.body
  when 3
    JSON.parse(resp.body)
  when :response
    resp
  else
    resp.body.to_f
  end
end

.configObject



13
14
15
# File 'lib/actie_smsc.rb', line 13

def config
  @config ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields:



17
18
19
# File 'lib/actie_smsc.rb', line 17

def configure
  yield(config)
end

.send_sms(phones, message, translit: 0, time: nil, id: 0, format: nil, sender: nil, fmt: 1, **query_params) ⇒ Object



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
# File 'lib/actie_smsc.rb', line 21

def send_sms(phones, message, translit: 0, time: nil, id: 0, format: nil, sender: nil, fmt: 1, **query_params)
  request_params = { cost: 3, phones: phones_string(phones), mes: message, id: id, fmt: fmt }
  request_params[:translit] = (0..2).include?(translit) ? translit : 0
  request_params[format] = format_value(format) if format_value(format)
  request_params[:sender] = sender if sender
  request_params[:time] = time_string(time) if time
  request_params.merge!(query_params)

  resp = request('send', request_params)

  check_response_for_exception(resp.body)

  case fmt
  when 2,0
    resp.body
  when 3
    JSON.parse(resp.body)
  when :response
    resp
  else
    body = resp.body.split(',')
    {
      id: body[0].to_i,
      cnt: body[1].to_i,
      cost: body[2].to_f,
      balance: body[3].to_f
    }
  end
end

.sms_cost(phones, message, translit: 0, format: nil, sender: nil, fmt: 1, **query_params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/actie_smsc.rb', line 51

def sms_cost(phones, message, translit: 0, format: nil, sender: nil, fmt: 1, **query_params)
  request_params = { cost: 1, phones: phones_string(phones), mes: message, fmt: fmt }
  request_params[:translit] = (0..2).include?(translit) ? translit : 0
  request_params[format] = format_value(format) if format_value(format)
  request_params[:sender] = sender if sender
  request_params.merge!(query_params)

  resp = request('send', request_params)

  check_response_for_exception(resp.body)

  case fmt
  when 2,0
    resp.body
  when 3
    JSON.parse(resp.body)
  when :response
    resp
  else
    body = resp.body.split(',')
    { cost: body[0].to_f, cnt: body[1].to_i }
  end
end

.status(id, phone, all: false, fmt: 1) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/actie_smsc.rb', line 75

def status(id, phone, all: false, fmt: 1)
  request_params = { phone: phone, id: id, fmt: fmt }
  request_params[:all] = (all && all != 0) ? 1 : 0

  resp = request('status', request_params)

  check_response_for_exception(resp.body)

  case fmt
  when 2,0
    resp.body
  when 3
    JSON.parse(resp.body)
  when :response
    resp
  else
    body = resp.body.split(',')
    result = {
      status: body[0].to_i,
      change_time: Time.at(body[1].to_i),
      error_code: body[2].to_i
    }
    # TODO: Implement HLR requests data:
    # для отправленного SMS (<статус>, <время изменения>, <код ошибки sms>)
    # для HLR-запроса (<статус>, <время изменения>, <код ошибки sms>, <код IMSI SIM-карты>, <номер сервис-центра>,
    # <код страны регистрации>, <код оператора абонента>, <название страны регистрации>, <название оператора абонента>,
    # <название роуминговой страны>, <название роумингового оператора>)

    if all != 0
      result.merge!(
        send_time: Time.at(body[-7].to_i),
        phone: body[-6],
        cost: body[-5].to_f,
        sender: body[-4],
        status_message: CGI.unescape(body[-3]),
        message: body[-2]
      )
    end
    result
  end
end