sms24x7 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ .idea/
2
+ *~
3
+ *.gem
4
+ *.rbc
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sms24x7.gemspec
4
+ gemspec
5
+
6
+ gem 'rspec'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Gleb Averchuk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # Sms24x7
2
+
3
+ Sending SMS via sms24x7 API. For more details see http://sms24x7.ru/api/
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sms24x7'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sms24x7
18
+
19
+ ## Usage
20
+
21
+ In the begining, you need to [register](http://outbox.sms24x7.ru/registration.php?pattern_id=2) for sending SMS through sms24x7 gateway.
22
+
23
+ There are two ways for sending your SMS.
24
+
25
+ The first - it's sending a SMS to one recipient. To do this, use:
26
+
27
+ ```
28
+ SmsApi.push_msg_nologin('your@email.com', 'your_password', 'recipient_phone', 'sms_text', params)
29
+ ```
30
+
31
+ here `recipient_phone` - is a phone number in format '7xxxyyyzzzz' and `params` - additional params that you can see in [documentation](http://sms24x7.ru/wp-content/uploads/2011/04/api_manual.pdf).
32
+
33
+ The second way - it's sending multiple SMS to multiple recipients.
34
+
35
+ ```
36
+ SmsApi.login('your@email.com', 'your_password')
37
+ recipient_phones_str_arr.each do |recipient_phone|
38
+ SmsApi.push_msg(recipient_phone, 'sms_text', params)
39
+ end
40
+ ```
41
+
42
+ here `recipient_phones_str_arr` - it's array that consist phone numbers as strings in format that represented above.
43
+
44
+ ## Contributing
45
+
46
+ 1. Fork it
47
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
48
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
49
+ 4. Push to the branch (`git push origin my-new-feature`)
50
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/sms24x7.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "sms24x7/sms_api"
2
+ require "sms24x7/version"
@@ -0,0 +1,147 @@
1
+ require 'curb-fu'
2
+ require 'json/pure'
3
+
4
+ module SmsApi
5
+ SMS_HOST = 'api.sms24x7.ru'
6
+
7
+ class BaseError < Exception; end
8
+ class InterfaceError < BaseError; end
9
+ class AuthError < BaseError; end
10
+ class NoLoginError < BaseError; end
11
+ class BalanceError < BaseError; end
12
+ class SpamError < BaseError; end
13
+ class EncodingError < BaseError; end
14
+ class NoGateError < BaseError; end
15
+ class OtherError < BaseError; end
16
+
17
+ module_function
18
+
19
+ # Public: Sends request to API
20
+ #
21
+ # request - Associative array to pass to API, :format key will be overridden
22
+ # cookie - Cookies string to be passed
23
+ #
24
+ # Returns:
25
+ # {
26
+ # :error_code => error_code,
27
+ # :data => data
28
+ # }
29
+ # If response was OK, data is an associative array, error_code is an error numeric code.
30
+ # Otherwise exception will be raised.
31
+ #
32
+ def _communicate(request, cookie = nil, secure = true)
33
+ request[:format] = 'json'
34
+ protocol = secure ? 'https' : 'http'
35
+ curl = CurbFu.post({ :host => SMS_HOST, :protocol => protocol }, request) do |curb|
36
+ curb.cookies = cookie if cookie
37
+ end
38
+ raise InterfaceError, 'cURL request failed' unless curl.success?
39
+
40
+ json = JSON.parse(curl.body)
41
+ unless (response = json['response']) && (msg = response['msg']) && (error_code = msg['err_code'])
42
+ raise InterfaceError, 'Empty some necessary data fields'
43
+ end
44
+
45
+ error_code = error_code.to_i
46
+ if error_code > 0
47
+ case error_code
48
+ when 2 then raise AuthError
49
+ when 29 then raise NoGateError
50
+ when 35 then raise EncodingError
51
+ when 36 then raise BalanceError, 'No money'
52
+ when 37, 38 then raise SpamError
53
+ else raise OtherError, "Communication to API failed. Error code: #{error_code}"
54
+ end
55
+ end
56
+
57
+ { :error_code => error_code, :data => response['data'] }
58
+ end
59
+
60
+ # Public: Sends a message via sms24x7 API, combining authenticating and sending message in one request.
61
+ #
62
+ # email, passwrod - Login info
63
+ # phone - Recipient phone number in international format (like 7xxxyyyzzzz)
64
+ # text - Message text, ASCII or UTF-8.
65
+ # params - Additional parameters as key => value array, see API doc.
66
+ #
67
+ # Returns:
68
+ # {
69
+ # :n_raw_sms => n_raw_sms, - Number of SMS parts in message
70
+ # :credits => credits - Price for a single part
71
+ # }
72
+ #
73
+ def push_msg_nologin(email, password, phone, text, params = {})
74
+ request = {
75
+ :method => 'push_msg',
76
+ :email => email,
77
+ :password => password,
78
+ :phone => phone,
79
+ :text => text
80
+ }.merge(params)
81
+ responce = _communicate(request)
82
+ check_and_result_push_msg(responce)
83
+ end
84
+
85
+ # Public: Logs in API, producing a session ID to be sent back in session cookie.
86
+ #
87
+ # email - User's email
88
+ # password - User's password
89
+ #
90
+ # Returns:
91
+ # cookie - Is a string "sid=#{session_id}" to be passed to cURL
92
+ #
93
+ def login(email, password)
94
+ request = {
95
+ :method => 'login',
96
+ :email => email,
97
+ :password => password
98
+ }
99
+ responce = _communicate(request)
100
+ raise InterfaceError, "Login request OK, but no 'sid' set" unless (sid = responce[:data]['sid'])
101
+ @_cookie = "sid=#{CGI::escape(sid)}"
102
+ end
103
+
104
+ # Public: Sends message via API, using previously obtained cookie to authenticate.
105
+ # That is, must first call the login method.
106
+ #
107
+ # cookie - String, returned by smsapi_login, "sid=#{session_id}"
108
+ # phone - Target phone
109
+ # text - Message text, ASCII or UTF-8
110
+ # params - Dictionary of optional parameters, see API documentation of push_msg method
111
+ #
112
+ # Returns:
113
+ # {
114
+ # :n_raw_sms => n_raw_sms, - Number of SMS parts in message
115
+ # :credits => credits - Price for a single part
116
+ # }
117
+ #
118
+ def push_msg(phone, text, params = {})
119
+ raise NoLoginError, 'Must first call the login method' unless @_cookie
120
+ request = {
121
+ :method => 'push_msg',
122
+ :phone => phone,
123
+ :text => text
124
+ }.merge(params)
125
+ responce = _communicate(request, @_cookie)
126
+ check_and_result_push_msg(responce)
127
+ end
128
+
129
+ # Private: Check the responce to a required fields. And formation of the resulting hash.
130
+ #
131
+ # responce - Result of _communicate method
132
+ #
133
+ # Returns:
134
+ # {
135
+ # :n_raw_sms => n_raw_sms, - Number of SMS parts in message
136
+ # :credits => credits - Price for a single part
137
+ # }
138
+ #
139
+ def check_and_result_push_msg(responce)
140
+ data = responce[:data]
141
+ unless (n_raw_sms = data['n_raw_sms']) && (credits = data['credits'])
142
+ raise InterfaceError, "Could not find 'n_raw_sms' or 'credits' in successful push_msg response"
143
+ end
144
+ #{ :n_raw_sms => n_raw_sms.to_i, :credits => credits.to_f }
145
+ data
146
+ end
147
+ end
@@ -0,0 +1,3 @@
1
+ module SmsApi
2
+ VERSION = "0.1.0"
3
+ end
data/sms24x7.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/sms24x7/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Gleb Averchuk"]
6
+ gem.email = ["altermn@gmail.com"]
7
+ gem.description = %q{Sending SMS via sms24x7 API}
8
+ gem.summary = %q{Uses sms24x7 gateway for sending SMS}
9
+ gem.homepage = "https://github.com/newmen/sms24x7"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "sms24x7"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = SmsApi::VERSION
17
+
18
+ gem.add_dependency 'curb-fu'
19
+ gem.add_dependency 'json'
20
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe SmsApi do
4
+ describe 'Testing sms24x7 api calls' do
5
+ it 'sending SMS' do
6
+ email = 'your@email.com'
7
+ password = 'your_password'
8
+ phone = '79991234567' # your phone
9
+
10
+ email.should match(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/), 'We need your email'
11
+ password.should_not eq(''), 'We need your password'
12
+ phone.should_not eq(''), 'We need your phone number'
13
+
14
+ sending_result = SmsApi.push_msg_nologin(email, password, phone, 'test passed',
15
+ :sender_name => 'RSpec',
16
+ :api => '1.1',
17
+ :satellite_adv => 'IF_EXISTS')
18
+
19
+ sending_result.should have_key('n_raw_sms')
20
+ sending_result.should have_key('credits')
21
+ end
22
+
23
+ it 'incorrect email and password' do
24
+ lambda {
25
+ SmsApi.push_msg_nologin('test@test.tt', 'T_T', '79991234567', 'wrong')
26
+ }.should raise_error(SmsApi::AuthError)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'sms24x7'
3
+ require 'rspec'
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sms24x7
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gleb Averchuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-05 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: curb-fu
16
+ requirement: &11599700 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *11599700
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ requirement: &11598620 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *11598620
36
+ description: Sending SMS via sms24x7 API
37
+ email:
38
+ - altermn@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - LICENSE
46
+ - README.md
47
+ - Rakefile
48
+ - lib/sms24x7.rb
49
+ - lib/sms24x7/sms_api.rb
50
+ - lib/sms24x7/version.rb
51
+ - sms24x7.gemspec
52
+ - spec/functional_spec.rb
53
+ - spec/spec_helper.rb
54
+ homepage: https://github.com/newmen/sms24x7
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.17
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Uses sms24x7 gateway for sending SMS
78
+ test_files:
79
+ - spec/functional_spec.rb
80
+ - spec/spec_helper.rb