octopush-ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79587279ddd4490aa739dec956b749a592deed5b
4
+ data.tar.gz: 45b2941fa22fbb20bc53e6d62a2d304f21a9159f
5
+ SHA512:
6
+ metadata.gz: ec1590872b28b5c517ae56e173ee326c022c680c63288412f5c521dd19841be8f15494c84cdabc363388dbae43565595d3813de9115a13b0f338bb66e2d4e019
7
+ data.tar.gz: 364bdb07e2a0cf77f2f0cd1aeab69bd9b1be913edd4cfbcfd3ddebcc7bb756cae953fd19741375303b69295c89ae790021d27905ba965e87f81fed2018a80027
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,7 @@
1
+ .PHONY: test
2
+
3
+ console:
4
+ irb -Ilib -roctopush-ruby
5
+
6
+ test:
7
+ cutest test/*.rb
@@ -0,0 +1,74 @@
1
+ # octopush-ruby
2
+
3
+ A ruby library for use [Octopush API](http://www.octopush.com/en/sms-api).
4
+
5
+ ## Installation
6
+
7
+ You can install via
8
+ ```bash
9
+ gem install octopush-ruby
10
+ ```
11
+ or if you are using bundler, by adding to your Gemfile
12
+ ```ruby
13
+ gem 'octopush-ruby'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ ### Setup user
19
+
20
+ First you need to setup an user login and api key to use with octopush.
21
+ You can register at [octopush.com](http://www.octopush.com/en/registration) to get one
22
+
23
+ ```ruby
24
+ require 'octopush-ruby'
25
+
26
+ # setup your own credentials
27
+ Octopush.configure do |config|
28
+ config.user_login = 'your_user_login'
29
+ config.api_key = 'your_api_key'
30
+ end
31
+
32
+ # then you can initiate a new client to use
33
+
34
+ client = Octopush::Client.new
35
+ ```
36
+
37
+ ### Send SMS
38
+
39
+ ```ruby
40
+ # first needs to create a sms instance
41
+ sms = Octopush::SMS.new
42
+
43
+ # set your desired attributes
44
+ sms.sms_text = 'some text'
45
+ sms.sms_recipients = '+33600000000'
46
+ sms.sms_type = 'FR'
47
+
48
+ # then just send the sms with the client you created before
49
+ client.send_sms(sms)
50
+ ```
51
+
52
+ ### Check Balance
53
+
54
+ ```ruby
55
+ client.get_balance
56
+ ```
57
+
58
+ ### Update user options
59
+
60
+ ```ruby
61
+ client.edit_options({answer_email: 'some@email.com', sms_alert_type: 'FR'})
62
+ ```
63
+
64
+ ### Create sub account
65
+
66
+ ```ruby
67
+ client.create_sub_account('Name', 'LastName', 'Raison', '234', 'FR')
68
+ ```
69
+
70
+ ### Credit sub account
71
+
72
+ ```ruby
73
+ client.credit_sub_account('sub_account@email.com', '500', 'FR')
74
+ ```
@@ -0,0 +1,14 @@
1
+ require 'octopush-ruby/configuration'
2
+ require 'octopush-ruby/constants'
3
+ require 'octopush-ruby/client'
4
+
5
+ module Octopush
6
+ class << self
7
+ attr_accessor :configuration
8
+ end
9
+
10
+ def self.configure
11
+ self.configuration ||= Configuration.new
12
+ yield(configuration)
13
+ end
14
+ end
@@ -0,0 +1,154 @@
1
+ require 'net/http'
2
+ require 'nori'
3
+ require 'digest/sha1'
4
+ require 'octopush-ruby/sms'
5
+
6
+ module Octopush
7
+ class Client
8
+ def initialize
9
+ raise "Should set user configuration before use" if Octopush.configuration.nil?
10
+
11
+ @constants = Octopush::Constants
12
+ @domain = @constants::DOMAIN
13
+ end
14
+
15
+ # update user options
16
+ # args should be a hash with the options that you want to update
17
+ # could be answer_email, sms_alert_bound, sms_alert_type
18
+ # @example
19
+ # {answer_email: 'an_email@domain.com'}
20
+ def edit_options *args
21
+ path = @constants::PATH_EDIT_OPTIONS
22
+ data = user_hash.merge args[0]
23
+ res = request @domain, path, data
24
+ end
25
+
26
+ # returns current user's balance
27
+ # return a hash in the form: {balance_type => balance}
28
+ def get_balance
29
+ path = @constants::PATH_BALANCE
30
+ data = user_hash
31
+ response = request @domain, path, data
32
+
33
+ h = {}
34
+ response["balance"].each do |balance|
35
+ h = h.merge(balance.attributes["type"] => balance)
36
+ end
37
+
38
+ h
39
+ end
40
+
41
+ # send a sms
42
+ # sms - a Octopush::SMS instance
43
+ # sending_date - a date to send sms, required if sms_mode is DIFFERE,
44
+ # check Octopush::Constants::SMS_MODES for modes allowed
45
+ # request_keys - Lists the key fields of the application you want to add
46
+ # in the sha1 hash. Check Octopush::Constants::REQUEST_KEYS
47
+ def send_sms sms, sending_date=nil, request_keys=nil
48
+ raise 'require a sms object' if sms.class != Octopush::SMS
49
+
50
+ path = @constants::PATH_SMS
51
+ data = user_hash.merge(sms.variables_hash)
52
+
53
+ if data[:sms_mode] == @constants::SMS_MODES['DIFFERE']
54
+ raise 'Need specify sending_date for DIFFERE mode' if sending_date.nil?
55
+ data = data.merge(sending_date: sending_date)
56
+ end
57
+
58
+ if !request_keys.nil?
59
+ sha1 = get_request_sha1_string(request_keys)
60
+ data = data.merge(request_keys: request_keys, request_sha1: sha1)
61
+ end
62
+
63
+ res = request @domain, path, data
64
+ end
65
+
66
+ # create sub account
67
+ # first_name
68
+ # last_name
69
+ # raison_sociable
70
+ # alert_bound
71
+ # alert_sms_type - check Octopush::Constants::SMS_TYPES
72
+ def create_sub_account first_name, last_name, raison_sociable, alert_bound,
73
+ alert_sms_type
74
+ path = @constants::PATH_SUB_ACCOUNT
75
+ data = user_hash.merge( first_name: first_name,
76
+ last_name: last_name,
77
+ raison_sociable: raison_sociable,
78
+ alert_bound: alert_bound,
79
+ alert_sms_type: alert_sms_type
80
+ )
81
+ res = request @domain, path, data
82
+ end
83
+
84
+ # credit sub account
85
+ # sub_account - sub account email
86
+ # sms_amount - number of credits
87
+ # sms_type - a sms type, check Octopush::Constants::SMS_TYPES
88
+ def credit_sub_account sub_account_email, sms_amount, sms_type
89
+ path = @constants::PATH_CREDIT_SUB_ACCOUNT_TOKEN
90
+ data = user_hash.merge(sub_account_email: sub_account_email)
91
+ res = request @domain, path, data
92
+ token_res = parse_response res.body
93
+
94
+ token = token_res['token']
95
+ path = @constants::PATH_CREDIT_SUB_ACCOUNT
96
+ if sms_type != 'FR' and sms_type != 'XXX'
97
+ sms_type = 'FR'
98
+ end
99
+
100
+ data = data.merge(sms_number: sms_amount,
101
+ sms_type: sms_type,
102
+ token: token)
103
+
104
+ res = request @domain, path, data
105
+ end
106
+
107
+ private
108
+
109
+ def user_hash
110
+ {
111
+ user_login: Octopush.configuration.user_login,
112
+ api_key: Octopush.configuration.api_key
113
+ }
114
+ end
115
+
116
+ def request domain, path, data, ssl=false
117
+ prefix = ssl ? 'https://' : 'http://'
118
+ url = prefix + domain + path
119
+ uri = URI url
120
+ req = Net::HTTP::Post.new uri.path
121
+ req.set_form_data data
122
+ res = Net::HTTP.start(uri.host, uri.port) do |http|
123
+ http.request(req)
124
+ end
125
+ parse_response res.body
126
+ end
127
+
128
+ # octopush api returns a xml after each request.
129
+ # We parse the xml to hash for more easy use
130
+ def parse_response response
131
+ parser = Nori.new
132
+ res_hash = parser.parse response
133
+ code = res_hash["octopush"]["error_code"]
134
+ if code != "000"
135
+ raise Octopush::Constants::ERRORS[code]
136
+ else
137
+ res_hash["octopush"]
138
+ end
139
+ end
140
+
141
+ # get a sha1 string in base to request_keys
142
+ def get_request_sha1_string request_keys, data
143
+ char_to_field = @constants::REQUEST_KEYS
144
+ request_string = ''
145
+ request_keys.split('').each do |key|
146
+ if !char_to_field[key].nil? and !data[char_to_field[key].to_sym].nil?
147
+ request_string += data[char_to_field[key].to_sym]
148
+ end
149
+ end
150
+
151
+ Digest::SHA1.hexdigest request_string
152
+ end
153
+ end
154
+ end
@@ -0,0 +1,5 @@
1
+ module Octopush
2
+ class Configuration
3
+ attr_accessor :user_login, :api_key
4
+ end
5
+ end
@@ -0,0 +1,106 @@
1
+ module Octopush
2
+ class Constants
3
+ VERSION = '1.0.0'
4
+ DOMAIN = 'www.octopush-dm.com'
5
+ PORT = 80;
6
+ API_PATH = '/api'
7
+ API_SUB_PATH = '/api_sub'
8
+ PATH_SMS = API_PATH + '/sms'
9
+ PATH_BALANCE = API_PATH + '/balance'
10
+ PATH_SUB_ACCOUNT = API_SUB_PATH + '/add_sub_account'
11
+ PATH_CREDIT_SUB_ACCOUNT_TOKEN = API_SUB_PATH + '/credit_sub_account_get_session'
12
+ PATH_CREDIT_SUB_ACCOUNT = API_SUB_PATH + '/credit_sub_account'
13
+ PATH_OSTP = API_PATH + '/open_single_temp_session'
14
+ PATH_EDIT_OPTIONS = API_PATH + '/edit_options'
15
+ PATH_GET_USER_INFO = API_SUB_PATH + '/get_user_info'
16
+ SMS_TYPES = {
17
+ 'SMS_STANDARD' => 'XXX',
18
+ 'SMS_WORLD' => 'WWW',
19
+ 'SMS_PREMIUM' => 'FR'
20
+ }
21
+ SMS_MODES = {
22
+ 'INSTANTANE' => 1,
23
+ 'DIFFERE' => 2
24
+ }
25
+ REQUEST_MODES = {
26
+ 'SIMULATION' => 'simu',
27
+ 'REEL' => 'real'
28
+ }
29
+ ERRORS = {
30
+ '100'=> 'POST request missing.',
31
+ '101'=> 'Incorrect login details.',
32
+ '102'=> 'Your SMS exceeds 160 characters',
33
+ '103'=> 'Your message has no recipients',
34
+ '104'=> 'You have run out of credit.',
35
+ '105'=> 'You don\'t have enough credit on your balance, but your last order is waiting for being validated',
36
+ '106'=> 'You have entered the Sender incorrectly. 3 to 11 characters, chosen from 0 to 9, a to z, A to Z. No accent, space or punctuation.',
37
+ '107'=> 'The text of your message is missing.',
38
+ '108'=> 'You have not entered your login details.',
39
+ '109'=> 'You have not entered your password.',
40
+ '110'=> 'You have not entered the list of recipient.',
41
+ '111'=> 'You have not chosen a way to enter your recipients.',
42
+ '112'=> 'You have not defined the quality of your message.',
43
+ '113'=> 'Your account is not validated. Log in Octopush and go to the "User interface" section.',
44
+ '114'=> 'You are under investigation for the fraudulent use of our services.',
45
+ '115'=> 'The recipient number is different from the number of one of the parameters that you have related it to.',
46
+ '116'=> 'The mailing option only works by using a contact list.',
47
+ '117'=> 'Your recipient list contains no correct numbers. Have you formatted your numbers by including the international dialling code? Contact us if you have any problems.',
48
+ '118'=> 'You must tick one of the two boxes to indicate if you do not wish to send test SMS or if you have correctly received and validated it.',
49
+ '119'=> 'You cannot send SMS with more than 160 characters for this type of SMS',
50
+ '120'=> 'A SMS with the same request_id has already been sent.',
51
+ '121'=> 'In Premium SMS, the mention "STOP au XXXXX" is mandatory and must belong to your text (respect the case).',
52
+ '122'=> 'In Standard SMS, the mention "no PUB=STOP" is mandatory and must belong to your text (respect the case).',
53
+ '123'=> 'The field request_sha1 is missing.',
54
+ '124'=> 'The field request_sha1 does not match. The data is wrong, or the query string contains an error or the frame contains an error : the request is rejected.',
55
+ '125'=> 'An undefined error has occurred. Please contact support.',
56
+ '126'=> 'An SMS campaign is already waiting for approval to send. You must validate or cancel it in order to start another.',
57
+ '127'=> 'An SMS campaign is already being processed. You must wait for processing to be completed in order to start another.',
58
+ '128'=> 'Too many attempts have been made. You need to start a new campaign.',
59
+ '129'=> 'Campaign is being built.',
60
+ '130'=> 'Campagne has not been set as finished.',
61
+ '131'=> 'Campaign not found.',
62
+ '132'=> 'Campaign sent.',
63
+ '133'=> 'The user_batch_id has already been used',
64
+ '150'=> 'No country was found for this prefix.',
65
+ '151'=> 'The recipient country is not part of the countries serviced by Octopush.',
66
+ '152'=> 'You cannot send low cost SMS to this country. Choose Premium SMS',
67
+ '153'=> 'The route is congested. This type of SMS cannot be dispatched immediately. If your order is urgent, please use another type of SMS.',
68
+ '201'=> 'This option is only available on request. Do not hesitate to request access if you need it.',
69
+ '202'=> 'The email account you wish to credit is incorrect.',
70
+ '203'=> 'You already have tokens in use. You can only have one session open at a time.',
71
+ '204'=> 'You specified a wrong token.',
72
+ '205'=> 'The number of text messages you want to transfer is too low.',
73
+ '206'=> 'You may not run campaigns during a credit transfer.',
74
+ '207'=> 'You do not have access to this feature.',
75
+ '208'=> 'Wrong type of SMS.',
76
+ '209'=> 'You are not allowed to send SMS messages to this user.',
77
+ '210'=> 'This email is not specified in any of your sub accounts or affiliate users.',
78
+ '300'=> 'You are not authorized to manage your lists by API.',
79
+ '301'=> 'You have reached the maximum number of lists.',
80
+ '302'=> 'A list with the same name already exists.',
81
+ '303'=> 'The specified list does not exist.',
82
+ '304'=> 'The list is already full.',
83
+ '305'=> 'There are too many contacts in the query.',
84
+ '306'=> 'The requested action is unknown.',
85
+ '308'=> 'Error of file.',
86
+ '500'=> 'Impossible to process the requested action',
87
+ '501'=> 'Connection error. Please contact our customer support'
88
+ }
89
+ REQUEST_KEYS = {
90
+ 'T' => 'sms_text',
91
+ 'R' => 'sms_recipients',
92
+ 'M' => 'sms_mode',
93
+ 'Y' => 'sms_type',
94
+ 'S' => 'sms_sender',
95
+ 'D' => 'sms_date',
96
+ 'a' => 'recipients_first_names',
97
+ 'b' => 'recipients_last_names',
98
+ 'c' => 'sms_fields_1',
99
+ 'd' => 'sms_fields_2',
100
+ 'e' => 'sms_fields_3',
101
+ 'W' => 'with_replies',
102
+ 'N' => 'transactional',
103
+ 'Q' => 'request_id'
104
+ }
105
+ end
106
+ end
@@ -0,0 +1,21 @@
1
+ module Octopush
2
+ class SMS
3
+ attr_accessor :sms_text, :sms_recipients, :recipients_first_names,
4
+ :recipients_last_names, :sms_fields1, :sms_fields2,
5
+ :sms_fields3, :sms_mode, :sms_type, :sms_sender,
6
+ :request_mode, :request_id, :with_replies, :transactional,
7
+ :msisdn_sender
8
+
9
+ def variables_hash
10
+ hash = {}
11
+ self.instance_variables.each do |variable|
12
+ var = variable.to_s.sub('@', '')
13
+ key = var.to_sym
14
+ value = self.send(var)
15
+ hash = hash.merge(key => value)
16
+ end
17
+
18
+ hash
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "octopush-ruby"
3
+ s.version = "0.0.1"
4
+ s.summary = "A ruby library for use Octopush API"
5
+ s.description = s.summary
6
+ s.authors = ["César Carruitero"]
7
+ s.email = ["cesar@mozilla.pe"]
8
+ s.homepage = "https://github.com/ccarruitero/octopush-ruby"
9
+ s.license = "MIT"
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+
13
+ s.add_runtime_dependency "nori"
14
+ s.add_development_dependency "cutest"
15
+ end
@@ -0,0 +1,2 @@
1
+ require 'cutest'
2
+ require_relative '../lib/octopush-ruby'
@@ -0,0 +1,22 @@
1
+ require_relative 'helper'
2
+ include Octopush
3
+
4
+ scope do
5
+ setup do
6
+ Octopush.configure do |c|
7
+ c.user_login = 'mymail@example.com'
8
+ end
9
+ end
10
+
11
+ test "sha1 string" do
12
+ data = {sms_text: 'fasgsagasg'}
13
+ cli = Octopush::Client.new
14
+ sha1 = cli.send(:get_request_sha1_string, 'T', data)
15
+ assert_equal "4ba873f39c4ca45c67ab281e75ca23779d72bf2a", sha1
16
+
17
+ data = {sms_text: 'fasgsagasg', sms_recipients: '+51948372820'}
18
+ cli = Octopush::Client.new
19
+ sha1 = cli.send(:get_request_sha1_string, 'TR', data)
20
+ assert_equal "53dfcf057931579e382e76a96f79e32d3d01c014", sha1
21
+ end
22
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: octopush-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - César Carruitero
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nori
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cutest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A ruby library for use Octopush API
42
+ email:
43
+ - cesar@mozilla.pe
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - Makefile
51
+ - README.mkd
52
+ - lib/octopush-ruby.rb
53
+ - lib/octopush-ruby/client.rb
54
+ - lib/octopush-ruby/configuration.rb
55
+ - lib/octopush-ruby/constants.rb
56
+ - lib/octopush-ruby/sms.rb
57
+ - octopush-ruby.gemspec
58
+ - test/helper.rb
59
+ - test/octopush_test.rb
60
+ homepage: https://github.com/ccarruitero/octopush-ruby
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A ruby library for use Octopush API
84
+ test_files: []