Module: SmsApi
- Defined in:
- lib/sms24x7/sms_api.rb,
lib/sms24x7/version.rb
Defined Under Namespace
Classes: AuthError, BalanceError, BaseError, EncodingError, InterfaceError, NoGateError, NoLoginError, OtherError, SpamError
Constant Summary collapse
- SMS_HOST =
'api.sms24x7.ru'
- VERSION =
"0.2.0"
Class Method Summary collapse
-
._communicate(request, cookie = nil, secure = true) ⇒ Object
Public: Sends request to API.
-
.check_and_result_push_msg(responce) ⇒ Object
Private: Check the responce to a required fields.
-
.login ⇒ Object
Public: Logs in API, producing a session ID to be sent back in session cookie.
-
.push_msg(phone, text, params = {}) ⇒ Object
Public: Sends message via API, using previously obtained cookie to authenticate.
-
.push_msg_nologin(phone, text, params = {}) ⇒ Object
Public: Sends a message via sms24x7 API, combining authenticating and sending message in one request.
- .setup {|_self| ... } ⇒ Object
Class Method Details
._communicate(request, cookie = nil, secure = true) ⇒ Object
Public: Sends request to API
request - Associative array to pass to API, :format key will be overridden cookie - Cookies string to be passed
Returns:
:error_code => error_code,
:data => data
If response was OK, data is an associative array, error_code is an error numeric code. Otherwise exception will be raised.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sms24x7/sms_api.rb', line 44 def _communicate(request, = nil, secure = true) request[:format] = 'json' protocol = secure ? 'https' : 'http' curl = CurbFu.post({ :host => SMS_HOST, :protocol => protocol }, request) do |curb| curb. = if end raise InterfaceError, 'cURL request failed' unless curl.success? json = JSON.parse(curl.body) unless (response = json['response']) && (msg = response['msg']) && (error_code = msg['err_code']) raise InterfaceError, 'Empty some necessary data fields' end error_code = error_code.to_i if error_code > 0 case error_code when 2 then raise AuthError when 29 then raise NoGateError when 35 then raise EncodingError when 36 then raise BalanceError, 'No money' when 37, 38 then raise SpamError else raise OtherError, "Communication to API failed. Error code: #{error_code}" end end { :error_code => error_code, :data => response['data'] } end |
.check_and_result_push_msg(responce) ⇒ Object
Private: Check the responce to a required fields. And formation of the resulting hash.
responce - Result of _communicate method
Returns:
:n_raw_sms => n_raw_sms, - Number of SMS parts in message
:credits => credits - Price for a single part
153 154 155 156 157 158 159 |
# File 'lib/sms24x7/sms_api.rb', line 153 def check_and_result_push_msg(responce) data = responce[:data] unless (n_raw_sms = data['n_raw_sms']) && (credits = data['credits']) raise InterfaceError, "Could not find 'n_raw_sms' or 'credits' in successful push_msg response" end data end |
.login ⇒ Object
Public: Logs in API, producing a session ID to be sent back in session cookie.
Returns: cookie - Is a string “sid=#session_id” to be passed to cURL if no block given
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/sms24x7/sms_api.rb', line 101 def login request = { :method => 'login', :email => @@email, :password => @@password } responce = _communicate(request) raise InterfaceError, "Login request OK, but no 'sid' set" unless (sid = responce[:data]['sid']) @@cookie = "sid=#{CGI::escape(sid)}" if block_given? yield @@cookie = nil end @@cookie end |
.push_msg(phone, text, params = {}) ⇒ Object
Public: Sends message via API, using previously obtained cookie to authenticate. That is, must first call the login method.
phone - Target phone text - Message text, ASCII or UTF-8 params - Dictionary of optional parameters, see API documentation of push_msg method
Returns:
:n_raw_sms => n_raw_sms, - Number of SMS parts in message
:credits => credits - Price for a single part
132 133 134 135 136 137 138 139 140 141 |
# File 'lib/sms24x7/sms_api.rb', line 132 def push_msg(phone, text, params = {}) raise NoLoginError, 'Must first call the login method' unless @@cookie request = { :method => 'push_msg', :phone => phone, :text => text }.merge(params) responce = _communicate(request, @@cookie) check_and_result_push_msg(responce) end |
.push_msg_nologin(phone, text, params = {}) ⇒ Object
Public: Sends a message via sms24x7 API, combining authenticating and sending message in one request.
phone - Recipient phone number in international format (like 7xxxyyyzzzz) text - Message text, ASCII or UTF-8. params - Additional parameters as key => value array, see API doc.
Returns:
:n_raw_sms => n_raw_sms, - Number of SMS parts in message
:credits => credits - Price for a single part
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sms24x7/sms_api.rb', line 84 def push_msg_nologin(phone, text, params = {}) request = { :method => 'push_msg', :email => @@email, :password => @@password, :phone => phone, :text => text }.merge(params) responce = _communicate(request) check_and_result_push_msg(responce) end |
.setup {|_self| ... } ⇒ Object
27 28 29 |
# File 'lib/sms24x7/sms_api.rb', line 27 def setup yield self end |