oneapi-ruby 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,13 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ if File.exists?(lib)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ end
5
+ require 'oneapi-ruby'
6
+
7
+ # In your script an HTTP POST request will be received, you should extract the body of this request...
8
+ http_body = '...'
9
+
10
+ #...and process it with:
11
+ # example:on-mo
12
+ inbound_messages = OneApi::SmsClient.unserialize_inbound_messages(http_body)
13
+ # ----------------------------------------------------------------------------------------------------
@@ -0,0 +1,50 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ if File.exists?(lib)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ end
5
+ require 'oneapi-ruby'
6
+
7
+ username = ARGV[0]
8
+ password = ARGV[1]
9
+ local_ip_address = ARGV[2]
10
+ destination_address = ARGV[3]
11
+
12
+ if OneApi::Utils.empty(username)
13
+ print "Username: "
14
+ username = gets.strip!
15
+ end
16
+
17
+ if OneApi::Utils.empty(password)
18
+ print "Password: "
19
+ password = gets.strip!
20
+ end
21
+
22
+ if OneApi::Utils.empty(local_ip_address)
23
+ print "Local IP address: "
24
+ local_ip_address = gets.strip!
25
+ end
26
+
27
+ if OneApi::Utils.empty(destination_address)
28
+ print "Destination (MSISDN): "
29
+ destination_address = gets.strip!
30
+ end
31
+
32
+ port = 9090
33
+
34
+ data_connection_client = OneApi::DataConnectionProfileClient.new(username, password)
35
+
36
+ notify_url = "http://#{local_ip_address}:#{port}"
37
+
38
+ # example:retrieve-roaming-status-with-notify-url
39
+ data_connection_client.retrieve_roaming_status(destination_address, notify_url)
40
+ # ----------------------------------------------------------------------------------------------------
41
+
42
+ dummy_web_server = OneApi::DummyWebServer.new(local_ip_address, port)
43
+ dummy_web_server.start 30
44
+
45
+ for method, url, headers, body in dummy_web_server.requests
46
+ # example:on-roaming-status
47
+ delivery_info = OneApi::DataConnectionProfileClient.unserialize_roaming_status(body)
48
+ # ----------------------------------------------------------------------------------------------------
49
+ puts delivery_info.inspect
50
+ end
@@ -0,0 +1,53 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ if File.exists?(lib)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ end
5
+ require 'oneapi-ruby'
6
+
7
+ username = ARGV[0]
8
+ password = ARGV[1]
9
+ destination_address = ARGV[2]
10
+
11
+ if OneApi::Utils.empty(username)
12
+ print "Username: "
13
+ username = gets.strip!
14
+ end
15
+
16
+ if OneApi::Utils.empty(password)
17
+ print "Password: "
18
+ password = gets.strip!
19
+ end
20
+
21
+ if OneApi::Utils.empty(destination_address)
22
+ print "Destination (MSISDN): "
23
+ destination_address = gets.strip!
24
+ end
25
+
26
+ # example:initialize-sms-client
27
+ sms_client = OneApi::SmsClient.new(username, password)
28
+ # ----------------------------------------------------------------------------------------------------
29
+
30
+ # example:prepare-message-without-notify-url
31
+ sms = OneApi::SMSRequest.new
32
+ sms.sender_address = '38598123456'
33
+ sms.address = destination_address
34
+ sms.message = 'Test message'
35
+ sms.callback_data = 'Any string'
36
+ # ----------------------------------------------------------------------------------------------------
37
+
38
+ # example:send-message
39
+ result = sms_client.send_sms(sms)
40
+
41
+ # Store the client correlator to be able to query for the delivery status later:
42
+ client_correlator = result.client_correlator
43
+ # ----------------------------------------------------------------------------------------------------
44
+
45
+ # example:query-for-delivery-status
46
+ delivery_status = sms_client.query_delivery_status(client_correlator)
47
+ # ----------------------------------------------------------------------------------------------------
48
+
49
+ sleep(10)
50
+
51
+ for delivery_info in delivery_status.delivery_info
52
+ puts "Delivery status id #{delivery_info.delivery_status}"
53
+ end
@@ -0,0 +1,56 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ if File.exists?(lib)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ end
5
+ require 'oneapi-ruby'
6
+
7
+ username = ARGV[0]
8
+ password = ARGV[1]
9
+ local_ip_address = ARGV[2]
10
+ destination_address = ARGV[3]
11
+
12
+ if OneApi::Utils.empty(username)
13
+ print "Username: "
14
+ username = gets.strip!
15
+ end
16
+
17
+ if OneApi::Utils.empty(password)
18
+ print "Password: "
19
+ password = gets.strip!
20
+ end
21
+
22
+ if OneApi::Utils.empty(local_ip_address)
23
+ print "Local IP address: "
24
+ local_ip_address = gets.strip!
25
+ end
26
+
27
+ if OneApi::Utils.empty(destination_address)
28
+ print "Destination (MSISDN): "
29
+ destination_address = gets.strip!
30
+ end
31
+
32
+ port = 9090
33
+
34
+ sms_client = OneApi::SmsClient.new(username, password)
35
+
36
+ # example:prepare-message-with-notify-url
37
+ sms = OneApi::SMSRequest.new
38
+ sms.sender_address = 'INFOSMS'
39
+ sms.address = destination_address
40
+ sms.message = 'Test message'
41
+ sms.callback_data = 'Any string'
42
+ sms.notify_url = "http://#{local_ip_address}:#{port}"
43
+ # ----------------------------------------------------------------------------------------------------
44
+
45
+ puts sms.inspect
46
+
47
+ result = sms_client.send_sms(sms)
48
+
49
+ dummy_web_server = OneApi::DummyWebServer.new(local_ip_address, port)
50
+ dummy_web_server.start 30
51
+
52
+ for method, url, headers, body in dummy_web_server.requests
53
+ # example:on-delivery-notification
54
+ delivery_info = OneApi::SmsClient.unserialize_delivery_status(body)
55
+ # ----------------------------------------------------------------------------------------------------
56
+ end
@@ -0,0 +1,8 @@
1
+ require "oneapi-ruby/version"
2
+ require "oneapi-ruby/utils"
3
+ require "oneapi-ruby/objects"
4
+ require "oneapi-ruby/models"
5
+ require "oneapi-ruby/client"
6
+
7
+ module OneApi
8
+ end
@@ -0,0 +1,309 @@
1
+ #require 'pry'
2
+ require 'net/http'
3
+ require 'net/https'
4
+ require "base64"
5
+
6
+ require_relative 'objects'
7
+ require_relative 'models'
8
+
9
+ module OneApi
10
+
11
+ class OneApiClient
12
+
13
+ # If true -- an exception will be thrown on error, otherwise, you have
14
+ # to check the is_success and exception methods on resulting objects.
15
+ attr_accessor :raise_exceptions
16
+
17
+ def initialize(username, password, base_url=nil)
18
+ @username = username
19
+ @password = password
20
+ if base_url
21
+ @base_url = base_url
22
+ else
23
+ @base_url = 'https://oneapi.infobip.com'
24
+ end
25
+
26
+ if @base_url[-1, 1] != '/'
27
+ @base_url += '/'
28
+ end
29
+
30
+ @oneapi_authentication = nil
31
+ @raise_exceptions = true
32
+ end
33
+
34
+
35
+ def login()
36
+ params = {
37
+ 'username' => @username,
38
+ 'password' => @password,
39
+ }
40
+
41
+ is_success, result = execute_POST('/1/customerProfile/login', params)
42
+
43
+ return fill_oneapi_authentication(result, is_success)
44
+ end
45
+
46
+ def get_or_create_client_correlator(client_correlator=nil)
47
+ if client_correlator
48
+ return client_correlator
49
+ end
50
+
51
+ return Utils.get_random_alphanumeric_string()
52
+ end
53
+
54
+ def prepare_headers(request)
55
+ request["User-Agent"] = "OneApi-ruby-#{OneApi::VERSION}"
56
+ if @oneapi_authentication and @oneapi_authentication.ibsso_token
57
+ request['Authorization'] = "IBSSO #{@oneapi_authentication.ibsso_token}"
58
+ else
59
+ auth_string = Base64.encode64("#{@username}:#{@password}").strip
60
+ request['Authorization'] = "Basic #{auth_string}"
61
+ end
62
+ end
63
+
64
+ def is_success(response)
65
+ http_code = response.code.to_i
66
+ is_success = 200 <= http_code && http_code < 300
67
+
68
+ is_success
69
+ end
70
+
71
+ def urlencode(params)
72
+ if Utils.empty(params)
73
+ return ''
74
+ end
75
+ if params.instance_of? String
76
+ return URI.encode(params)
77
+ end
78
+ result = ''
79
+ params.each_key do |key|
80
+ if ! Utils.empty(result)
81
+ result += '&'
82
+ end
83
+ result += URI.encode(key.to_s) + '=' + URI.encode(params[key].to_s)
84
+ end
85
+
86
+ return result
87
+ end
88
+
89
+ def execute_GET(url, params=nil)
90
+ execute_request('GET', url, params)
91
+ end
92
+
93
+ def execute_POST(url, params=nil)
94
+ execute_request('POST', url, params)
95
+ end
96
+
97
+ def execute_request(http_method, url, params)
98
+ rest_url = get_rest_url(url)
99
+ uri = URI(rest_url)
100
+
101
+ if Utils.empty(params)
102
+ params = {}
103
+ end
104
+
105
+ if http_method == 'GET'
106
+ request = Net::HTTP::Get.new("#{uri.request_uri}?#{urlencode(params)}")
107
+ elsif http_method == 'POST'
108
+ request = Net::HTTP::Post.new(uri.request_uri)
109
+ request.set_form_data(params)
110
+ end
111
+
112
+ http = Net::HTTP.new(uri.host, uri.port)
113
+
114
+ use_ssl = rest_url.start_with? "https"
115
+ if use_ssl
116
+ http.use_ssl = true
117
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
118
+ end
119
+
120
+ prepare_headers(request)
121
+ response = http.request(request)
122
+
123
+ puts "response = #{response.body}"
124
+
125
+ return is_success(response), response.body
126
+ end
127
+
128
+ def get_rest_url(rest_path)
129
+ if not rest_path
130
+ return @base_url
131
+ end
132
+
133
+ if rest_path[0, 1] == '/'
134
+ return @base_url + rest_path[1, rest_path.length]
135
+ end
136
+
137
+ @base_url + rest_path
138
+ end
139
+
140
+ def fill_oneapi_authentication(json, is_success)
141
+ @oneapi_authentication = convert_from_json(OneApiAuthentication, json, !is_success)
142
+
143
+ @oneapi_authentication.username = @username
144
+ @oneapi_authentication.password = @password
145
+
146
+ @oneapi_authentication.authenticated = @oneapi_authentication.ibsso_token ? @oneapi_authentication.ibsso_token.length > 0 : false
147
+
148
+ @oneapi_authentication
149
+ end
150
+
151
+ def convert_from_json(classs, json, is_error)
152
+ result = Conversions.from_json(classs, json, is_error)
153
+
154
+ if @raise_exceptions and !result.is_success
155
+ raise "#{result.exception.message_id}: #{result.exception.text} [#{result.exception.variables}]"
156
+ end
157
+
158
+ result
159
+ end
160
+
161
+ end
162
+
163
+ class SmsClient < OneApiClient
164
+
165
+ def initialize(username, password, base_url=nil)
166
+ super(username, password, base_url)
167
+ end
168
+
169
+ def send_sms(sms)
170
+ client_correlator = sms.client_correlator
171
+ if not client_correlator
172
+ client_correlator = Utils.get_random_alphanumeric_string()
173
+ end
174
+
175
+ params = {
176
+ 'senderAddress' => sms.sender_address,
177
+ 'address' => sms.address,
178
+ 'message' => sms.message,
179
+ 'clientCorrelator' => client_correlator,
180
+ 'senderName' => "tel:#{sms.sender_address}"
181
+ }
182
+
183
+ if sms.notify_url
184
+ params['notifyURL'] = sms.notify_url
185
+ end
186
+ if sms.callback_data
187
+ params['callbackData'] = sms.callback_data
188
+ end
189
+
190
+ is_success, result = execute_POST(
191
+ "/1/smsmessaging/outbound/#{sms.sender_address}/requests",
192
+ params
193
+ )
194
+
195
+ convert_from_json(ResourceReference, result, !is_success)
196
+ end
197
+
198
+ def query_delivery_status(client_correlator_or_resource_reference)
199
+ if defined? client_correlator_or_resource_reference.client_correlator
200
+ client_correlator = client_correlator_or_resource_reference.client_correlator
201
+ else
202
+ client_correlator = client_correlator_or_resource_reference
203
+ end
204
+
205
+ client_correlator = get_or_create_client_correlator(client_correlator)
206
+
207
+ params = {
208
+ 'clientCorrelator' => client_correlator,
209
+ }
210
+
211
+ is_success, result = execute_GET(
212
+ "/1/smsmessaging/outbound/TODO/requests/#{client_correlator}/deliveryInfos",
213
+ params
214
+ )
215
+
216
+ return convert_from_json(DeliveryInfoList, result, !is_success)
217
+ end
218
+
219
+ def retrieve_inbound_messages(max_number=nil)
220
+ if Utils.empty(max_number)
221
+ max_number = 100
222
+ end
223
+
224
+ params = {
225
+ 'maxBatchSize' => max_number
226
+ }
227
+
228
+ is_success, result = execute_GET(
229
+ '/1/smsmessaging/inbound/registrations/INBOUND/messages',
230
+ params
231
+ )
232
+
233
+ return convert_from_json(InboundSmsMessages, result, ! is_success)
234
+ end
235
+
236
+ # To be used when http push with a delivery notification comes.
237
+ def self.unserialize_delivery_status(http_body)
238
+ json = JSONUtils.get_json(http_body)
239
+ return Conversions::from_json(DeliveryInfoNotification, json, false)
240
+ end
241
+
242
+
243
+ @staticmethod
244
+ def self.unserialize_inbound_messages(http_body)
245
+ json = JSONUtils.get_json(http_body)
246
+ return Conversions::from_json(InboundSmsMessages, json, false)
247
+ end
248
+ end
249
+
250
+ class DataConnectionProfileClient < OneApiClient
251
+
252
+ def initialize(username, password, base_url=nil)
253
+ super(username, password, base_url)
254
+ end
255
+
256
+ def retrieve_roaming_status(destination_address, notify_url=nil)
257
+ # Retrieve asynchronously the customer’s roaming status for a single network-connected mobile device (HLR)
258
+
259
+ params = {
260
+ 'address' => destination_address
261
+ }
262
+ if notify_url
263
+ params['notifyURL'] = notify_url
264
+ end
265
+
266
+ is_success, result = execute_GET('/1/terminalstatus/queries/roamingStatus', params)
267
+
268
+ puts "params = #{params.inspect}"
269
+ puts "is_success = #{is_success}"
270
+ puts "result = #{result}"
271
+
272
+ if Utils.empty(notify_url)
273
+ json = JSONUtils.get_json(result)
274
+ return convert_from_json(TerminalRoamingStatus, json['roaming'], ! is_success);
275
+ else
276
+ return convert_from_json(GenericObject, {}, ! is_success);
277
+ end
278
+ end
279
+
280
+ # To be used when http push with a delivery notification comes.
281
+ def self.unserialize_roaming_status(http_body)
282
+ json = JSONUtils.get_json(http_body)
283
+ return Conversions::from_json(TerminalRoamingStatusNotification, json, false)
284
+ end
285
+
286
+ end
287
+
288
+ class CustomerProfileClient < OneApiClient
289
+
290
+ def initialize(username, password, base_url=nil)
291
+ super(username, password, base_url)
292
+ end
293
+
294
+ def get_account_balance()
295
+ is_success, result = execute_GET('/1/customerProfile/balance')
296
+
297
+ return convert_from_json(AccountBalance, result, ! is_success)
298
+ end
299
+
300
+ def get_customer_profile()
301
+ is_success, result = execute_GET('/1/customerProfile')
302
+
303
+ return convert_from_json(CustomerProfile, result, ! is_success)
304
+ end
305
+
306
+ end
307
+
308
+ end
309
+