globe_connect 1.0.1

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,39 @@
1
+ class Authentication < Base
2
+
3
+ ACCESS_TOKEN_URL = 'https://developer.globelabs.com.ph/oauth/access_token'
4
+ REDIRECT_URL = 'https://developer.globelabs.com.ph/dialog/oauth?app_id=%s'
5
+
6
+ # Requests for an access token based on the given app id, app secret, and code
7
+ # that was returned after logging in via WebForm
8
+ #
9
+ # @param string app_id
10
+ # @param string app_secret
11
+ # @param string code
12
+ # @return json
13
+ def get_access_token(app_id, app_secret, code)
14
+ url = ACCESS_TOKEN_URL
15
+
16
+ # set body
17
+ query = {
18
+ "app_id" => app_id,
19
+ "app_secret" => app_secret,
20
+ "code" => code
21
+ }
22
+
23
+ # hash it
24
+ payload = URI.encode(query.map{|k,v| "#{k}=#{v}"}.join("&"))
25
+
26
+ # send post request
27
+ response = send_post_request(url, payload, 'application/x-www-form-urlencoded')
28
+
29
+ return JSON.parse(response)
30
+ end
31
+
32
+ # Generates the url where the application/user will login
33
+ #
34
+ # @param string app_id
35
+ # @return string
36
+ def get_access_url(app_id)
37
+ return sprintf(REDIRECT_URL, app_id)
38
+ end
39
+ end
@@ -0,0 +1,58 @@
1
+ class Base
2
+
3
+ # Sends request via GET method
4
+ #
5
+ # @param string url
6
+ # @param mixed payload
7
+ # @return json
8
+ def send_get_request(url, payload = nil)
9
+ # set the uri
10
+ url = URI(url)
11
+
12
+ # set http settings
13
+ http = Net::HTTP.new(url.host, url.port)
14
+ http.use_ssl = true
15
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
16
+
17
+ # set request
18
+ request = Net::HTTP::Get.new(url)
19
+ request['content-type'] = 'application/json'
20
+ request['cache-control'] = 'no-cache'
21
+
22
+ # send the request and get whatever is the response
23
+ response = http.request(request)
24
+
25
+ # return whatever the result
26
+ return response.read_body
27
+ end
28
+
29
+ # Sends request via POST method
30
+ #
31
+ # @param string url
32
+ # @param mixed payload
33
+ # @param string content_type
34
+ # @return json
35
+ def send_post_request(url, payload, content_type = 'application/json')
36
+ # set the uri
37
+ url = URI(url)
38
+
39
+ # set http settings
40
+ http = Net::HTTP.new(url.host, url.port)
41
+ http.use_ssl = true
42
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
+
44
+ # set request
45
+ request = Net::HTTP::Post.new(url)
46
+ request["content-type"] = content_type
47
+ request["cache-control"] = 'no-cache'
48
+
49
+ # set the payload
50
+ request.body = payload
51
+
52
+ # send the request and get whatever is the response
53
+ response = http.request(request)
54
+
55
+ # return the response by reading the body
56
+ return response.read_body
57
+ end
58
+ end
@@ -0,0 +1,29 @@
1
+ class LocationQuery < Base
2
+
3
+ LOCATION_URL = 'https://devapi.globelabs.com.ph/location/v1/queries/location?access_token=%s&address=%s&requestedAccuracy=%s'
4
+
5
+ attr_accessor :access_token
6
+
7
+ # Starts up whenever the class is being called.
8
+ #
9
+ # @param string access_token
10
+ # @return LocationQuery
11
+ def initialize(access_token)
12
+ @access_token = access_token
13
+ end
14
+
15
+ # Fetches the geolocation of a given address.
16
+ #
17
+ # @param string address
18
+ # @param int accuracy
19
+ # @return json
20
+ def get_location(address, accuracy = 10)
21
+ # prepare the request url
22
+ url = sprintf(LOCATION_URL, @access_token, address, accuracy)
23
+
24
+ # set get request
25
+ response = send_get_request(url)
26
+
27
+ return JSON.parse(response)
28
+ end
29
+ end
@@ -0,0 +1,65 @@
1
+ class Payment < Base
2
+
3
+ CHARGE_URL = 'https://devapi.globelabs.com.ph/payment/v1/transactions/amount?access_token=%s'
4
+ LAST_REFERENCE_URL = 'https://devapi.globelabs.com.ph/payment/v1/transactions/getLastRefCode?app_id=%s&app_secret=%s'
5
+
6
+ attr_accessor :access_token, :app_id, :app_secret
7
+
8
+ # Starts up whenever the class is being called.
9
+ #
10
+ # @param string app_id
11
+ # @param string app_secret
12
+ # @param string access_token
13
+ # @return Payment
14
+ def initialize(app_id, app_secret, access_token = nil)
15
+ @app_id = app_id
16
+ @app_secret = app_secret
17
+ @access_token = access_token
18
+ end
19
+
20
+ # Sends payment request.
21
+ #
22
+ # @param float amount
23
+ # @param string description
24
+ # @param string end_user_id
25
+ # @param string reference_code
26
+ # @param string transaction_operation_status
27
+ # @return json
28
+ def send_payment_request(amount, description, end_user_id, reference_code, transaction_operation_status)
29
+ # set the request url
30
+ url = sprintf(CHARGE_URL, @access_token)
31
+
32
+ # make sure that amount has 2 decimal places
33
+ amount = '%.2f' % amount
34
+
35
+ # set the payload
36
+ payload = {
37
+ "amount" => amount.to_s,
38
+ "description" => description,
39
+ "endUserId" => end_user_id,
40
+ "referenceCode" => reference_code,
41
+ "transactionOperationStatus" => transaction_operation_status
42
+ }
43
+
44
+ # convert to JSON
45
+ payload = JSON.generate(payload)
46
+
47
+ # send post request
48
+ response = send_post_request(url, payload)
49
+
50
+ return JSON.parse(response)
51
+ end
52
+
53
+ # Get last reference code request.
54
+ #
55
+ # @return json
56
+ def get_last_reference_code
57
+ # set the url
58
+ url = sprintf(LAST_REFERENCE_URL, @app_id, @app_secret)
59
+
60
+ # send response
61
+ response = send_get_request(url)
62
+
63
+ return JSON.parse(response)
64
+ end
65
+ end
@@ -0,0 +1,90 @@
1
+ class Sms < Base
2
+
3
+ SEND_SMS_URL = 'https://devapi.globelabs.com.ph/smsmessaging/v1/outbound/%s/requests?access_token=%s'
4
+ BINARY_SMS_URL = 'https://devapi.globelabs.com.ph/binarymessaging/v1/outbound/%s/requests?access_token=%s'
5
+
6
+ attr_accessor :access_token, :short_code
7
+
8
+ # Starts up whenever the class is being called.
9
+ #
10
+ # @param string access_token
11
+ # @param string short_code
12
+ # @return Sms
13
+ def initialize(access_token, short_code)
14
+ @access_token = access_token
15
+ @short_code = short_code
16
+ end
17
+
18
+ # Sends a binary sms based on the given address
19
+ #
20
+ # @param string address
21
+ # @param string message
22
+ # @param string header
23
+ # @param string encoding
24
+ # @return json
25
+ def send_binary_message(address, message, header, encoding = nil)
26
+ # format url
27
+ url = sprintf(BINARY_SMS_URL, @short_code, @access_token)
28
+
29
+ # prepare the payload
30
+ payload = {
31
+ "outboundBinaryMessageRequest" => {
32
+ "userDataHeader" => header,
33
+ "dataCodingScheme" => 1,
34
+ "address" => address,
35
+ "senderAddress" => @short_code,
36
+ "access_token" => @access_token,
37
+ "outboundBinaryMessage" => {
38
+ "message" => message
39
+ },
40
+ }
41
+ }
42
+
43
+ # convert to JSON
44
+ payload = JSON.generate(payload)
45
+
46
+ # send post request
47
+ response = send_post_request(url, payload)
48
+
49
+ return JSON.parse(response)
50
+ end
51
+
52
+ # Sends a sms based on the given address
53
+ #
54
+ # @param string address
55
+ # @param string message
56
+ # @param mixed client_correlator
57
+ # @return json
58
+ def send_message(address, message, client_correlator = nil)
59
+ # set the address format
60
+ addressFormat = 'tel:%s'
61
+
62
+ # format url
63
+ url = sprintf(SEND_SMS_URL, @short_code, @access_token)
64
+
65
+ # set the address
66
+ address = sprintf(addressFormat, address)
67
+
68
+ # set the sender
69
+ sender = sprintf(addressFormat, @short_code)
70
+
71
+ # prepare the payload
72
+ payload = {
73
+ 'outboundSMSMessageRequest' => {
74
+ 'senderAddress' => sender,
75
+ 'address' => [address],
76
+ 'outboundSMSTextMessage' => {
77
+ 'message' => message
78
+ },
79
+ }
80
+ }
81
+
82
+ # convert to JSON
83
+ payload = JSON.generate(payload)
84
+
85
+ # send post request
86
+ response = send_post_request(url, payload)
87
+
88
+ return JSON.parse(response)
89
+ end
90
+ end
@@ -0,0 +1,43 @@
1
+ class Subscriber < Base
2
+
3
+ SUBSCRIBER_BALANCE_URL = 'https://devapi.globelabs.com.ph/location/v1/queries/balance?access_token=%s&address=%s'
4
+ RELOAD_AMOUNT_URL = 'https://devapi.globelabs.com.ph/location/v1/queries/reload_amount?access_token=%s&address=%s'
5
+
6
+ attr_accessor :access_token
7
+
8
+ # Starts up whenever the class is being called.
9
+ #
10
+ # @param string access_token
11
+ # @return Subscriber
12
+ def initialize(access_token)
13
+ @access_token = access_token
14
+ end
15
+
16
+ # Get subscriber balance request.
17
+ #
18
+ # @param string address
19
+ # @return json
20
+ def get_subscriber_balance(address)
21
+ # set the request url
22
+ url = sprintf(SUBSCRIBER_BALANCE_URL, @access_token, address)
23
+
24
+ # send post request
25
+ response = send_get_request(url)
26
+
27
+ return JSON.parse(response)
28
+ end
29
+
30
+ # Get subscriber reload amount.
31
+ #
32
+ # @param address
33
+ # @return json
34
+ def get_subscriber_reload_amount(address)
35
+ # set the request url
36
+ url = sprintf(RELOAD_AMOUNT_URL, @access_token, address)
37
+
38
+ # send post request
39
+ response = send_get_request(url)
40
+
41
+ return JSON.parse(response)
42
+ end
43
+ end
@@ -0,0 +1,77 @@
1
+ class Ussd < Base
2
+
3
+ SEND_URL = 'https://devapi.globelabs.com.ph/ussd/v1/outbound/%s/send/requests?access_token=%s'
4
+ REPLY_URL = 'https://devapi.globelabs.com.ph/ussd/v1/outbound/%s/reply/requests?access_token=%s'
5
+
6
+ attr_accessor :access_token, :short_code
7
+
8
+ # Starts up whenever the class is being called.
9
+ #
10
+ # @param string access_token
11
+ # @param string short_code
12
+ # @return Ussd
13
+ def initialize(access_token, short_code)
14
+ @access_token = access_token
15
+ @short_code = short_code
16
+ end
17
+
18
+ # @param string address
19
+ # @param string message
20
+ # @param boolean flash
21
+ # @return json
22
+ def send_ussd_request(address, message, flash)
23
+ # set the request url
24
+ url = sprintf(SEND_URL, @short_code, @access_token)
25
+
26
+ # set the payload
27
+ payload = {
28
+ "outboundUSSDMessageRequest" => {
29
+ "outboundUSSDMessage" => {
30
+ "message" => message
31
+ },
32
+ "address" => address,
33
+ "senderAddress" => @short_code,
34
+ "flash" => flash
35
+ }
36
+ }
37
+
38
+ # convert to JSON
39
+ payload = JSON.generate(payload)
40
+
41
+ # send post request
42
+ response = send_post_request(url, payload)
43
+
44
+ return JSON.parse(response)
45
+ end
46
+
47
+ # @param string address
48
+ # @param string message
49
+ # @param string session_id
50
+ # @param boolean flash
51
+ # @return json
52
+ def reply_ussd_request(address, message, session_id, flash)
53
+ # set the request url
54
+ url = sprintf(REPLY_URL, @short_code, @access_token)
55
+
56
+ # set the payload
57
+ payload = {
58
+ "outboundUSSDMessageRequest" => {
59
+ "outboundUSSDMessage" => {
60
+ "message" => message
61
+ },
62
+ "address" => address,
63
+ "senderAddress" => @short_code,
64
+ "sessionID" => session_id,
65
+ "flash" => flash
66
+ }
67
+ }
68
+
69
+ # convert to JSON
70
+ payload = JSON.generate(payload)
71
+
72
+ # send post request
73
+ response = send_post_request(url, payload)
74
+
75
+ return JSON.parse(response)
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module GlobeConnect
2
+ VERSION = "1.0.1"
3
+ end
@@ -0,0 +1,423 @@
1
+ class Voice
2
+ def initialize(params = {}, &block)
3
+ # say can be stacked and we need to save the things here
4
+ @say = Array.new
5
+
6
+ # initialize things
7
+ @response = { :tropo => Array.new }
8
+
9
+ @building = true
10
+ end
11
+
12
+ def ask(params = {}, instance = false, &block)
13
+ hash = nil
14
+ params = set_recognizer(params)
15
+
16
+ if params.kind_of? Array
17
+ # multiple instance of ask so just return it
18
+ return params
19
+ else
20
+ # build hash
21
+ hash = build_action('ask', params)
22
+ end
23
+
24
+ if instance || params.key?(:instance)
25
+ return hash[:ask]
26
+ end
27
+
28
+ @response[:tropo] << hash
29
+ end
30
+
31
+ def call(params={}, &block)
32
+ if block_given?
33
+ create_nested_hash('call', params)
34
+ instance_exec(&block)
35
+ @response[:tropo] << @nested_hash
36
+ else
37
+ hash = build_action('call', params)
38
+ @response[:tropo] << hash
39
+ end
40
+
41
+ if @building.nil?
42
+ return render
43
+ end
44
+ end
45
+
46
+ def choices(params={}, instance = false)
47
+ hash = build_action('choices', params)
48
+
49
+ if @nested_hash
50
+ @nested_hash[@nested_name.to_sym].merge!(hash)
51
+ else
52
+ # check if this is an instance, if it is we return the builded hash
53
+ if instance
54
+ return hash[:choices]
55
+ end
56
+
57
+ @response[:tropo] << hash
58
+
59
+ if @building.nil?
60
+ return render
61
+ end
62
+ end
63
+ end
64
+
65
+ def connect(params={}, instance = false)
66
+ hash = build_action('connect', params)
67
+
68
+ if @nested_hash
69
+ @nested_hash[@nested_name.to_sym].merge!(hash)
70
+ else
71
+ # check if this is an instance, if it is we return the builded hash
72
+ if instance
73
+ return hash[:connect]
74
+ end
75
+
76
+ @response[:tropo] << hash
77
+
78
+ if @building.nil?
79
+ return render
80
+ end
81
+ end
82
+ end
83
+
84
+ def conference(params={}, &block)
85
+ if block_given?
86
+ create_nested_hash('conference', params)
87
+ instance_exec(&block)
88
+ @response[:tropo] << @nested_hash
89
+ else
90
+ hash = build_action('conference', params)
91
+ @response[:tropo] << hash
92
+ end
93
+
94
+ if @building.nil?
95
+ return render
96
+ end
97
+ end
98
+
99
+ def hangup
100
+ @response[:tropo] << { :hangup => nil }
101
+ if @building.nil?
102
+ return render
103
+ end
104
+ end
105
+
106
+ def headers(params = {}, instance = false, &block)
107
+ response = Array.new
108
+
109
+ if block_given?
110
+ create_nested_hash('headers', params)
111
+ instance_exec(&block)
112
+ response = @nested_hash
113
+ else
114
+ hash = build_action('headers', params)
115
+ response = hash
116
+ end
117
+
118
+ # are we using the headers just to generate the header?
119
+ if instance
120
+ # return the headers
121
+ return response[:headers]
122
+ end
123
+
124
+ @response[:tropo] << response
125
+ end
126
+
127
+ def join_prompt(params = {}, instance = false, &block)
128
+ hash = build_action('join_prompt', params)
129
+
130
+ if @nested_hash
131
+ @nested_hash[@nested_name.to_sym].merge!(hash)
132
+ else
133
+ # check if this is an instance, if it is we return the builded hash
134
+ if instance
135
+ return hash[:join_prompt]
136
+ end
137
+
138
+ @response[:tropo] << hash
139
+
140
+ if @building.nil?
141
+ return render
142
+ end
143
+ end
144
+ end
145
+
146
+ def leave_prompt(params = {}, instance = false, &block)
147
+ hash = build_action('leave_prompt', params)
148
+
149
+ if @nested_hash
150
+ @nested_hash[@nested_name.to_sym].merge!(hash)
151
+ else
152
+ # check if this is an instance, if it is we return the builded hash
153
+ if instance
154
+ return hash[:leave_prompt]
155
+ end
156
+
157
+ @response[:tropo] << hash
158
+
159
+ if @building.nil?
160
+ return render
161
+ end
162
+ end
163
+ end
164
+
165
+ def machine_detection(params = {}, &block)
166
+ if block_given?
167
+ create_nested_hash('machine_detection', params)
168
+ instance_exec(&block)
169
+ @response[:tropo] << @nested_hash
170
+ else
171
+ hash = build_action('machine_detection', params)
172
+ @response[:tropo] << hash
173
+ end
174
+
175
+ if @building.nil?
176
+ return render
177
+ end
178
+ end
179
+
180
+ def message(params={}, &block)
181
+ if block_given?
182
+ create_nested_hash('message', params)
183
+ instance_exec(&block)
184
+ @response[:tropo] << @nested_hash
185
+ else
186
+ hash = build_action('message', params)
187
+ @response[:tropo] << hash
188
+ end
189
+
190
+ if @building.nil?
191
+ return render
192
+ end
193
+ end
194
+
195
+ def on(params={}, instance = false, &block)
196
+ if params.kind_of? Array
197
+ # multiple instance, just return it
198
+ return params
199
+ end
200
+
201
+ # build it
202
+ hash = build_action('on', params)
203
+
204
+ if instance
205
+ return hash
206
+ end
207
+
208
+ # push to the main array
209
+ @response[:tropo] << { :on => hash }
210
+ end
211
+
212
+ def record(params={}, &block)
213
+ if block_given?
214
+ create_nested_hash('record', params)
215
+ instance_exec(&block)
216
+ @response[:tropo] << @nested_hash
217
+ else
218
+ hash = build_action('record', params)
219
+ @response[:tropo] << hash
220
+ end
221
+
222
+ if @building.nil?
223
+ return render
224
+ end
225
+ end
226
+
227
+ def render
228
+ # check for stacked says
229
+ if @say.any?
230
+ # check how many we have
231
+ if @say.count > 1
232
+ @response[:tropo] << { :say => @say }
233
+ else
234
+ @response[:tropo] << { :say => @say[0] }
235
+ end
236
+ end
237
+
238
+ return JSON.pretty_generate(@response)
239
+ end
240
+
241
+ def redirect(params={})
242
+ hash = build_action('redirect', params)
243
+ @response[:tropo] << hash
244
+
245
+ if @building.nil?
246
+ return render
247
+ end
248
+ end
249
+
250
+ def reject
251
+ @response[:tropo] << { :reject => nil }
252
+
253
+ if @building.nil?
254
+ return render
255
+ end
256
+ end
257
+
258
+ def say(value = nil, params = {}, instance = false)
259
+ if value.kind_of? String
260
+ params[:value] = value
261
+ elsif value.kind_of? Hash
262
+ params = value
263
+ elsif value.kind_of? Array
264
+ params = value
265
+ else
266
+ # we'll stop here, there's nothing to do.
267
+ return
268
+ end
269
+
270
+ if params.kind_of? Array
271
+ hash = Array.new
272
+ params.each do |param|
273
+ param = set_recognizer(param)
274
+ hash.push(build_action('say', param)[:say])
275
+ end
276
+ elsif params.kind_of? Hash
277
+ param = set_recognizer(params)
278
+ hash = build_action('say', param)
279
+ else
280
+ params = set_recognizer(params)
281
+ hash = build_action('say', params)
282
+ end
283
+
284
+ # check if this is an instance, if it is we return the builded hash
285
+ if instance
286
+ return hash[:say]
287
+ end
288
+
289
+ # save everything in the say attribute
290
+ # check first if the hash is an array
291
+ if hash.kind_of? Array
292
+ @say = hash
293
+ else
294
+ @say << hash[:say]
295
+ end
296
+
297
+ # if @nested_hash && @nested_on_hash.nil?
298
+ # @nested_hash[@nested_name.to_sym].merge!(response)
299
+ # elsif @nested_on_hash
300
+ # @nested_on_hash[:on][@nested_on_hash_cnt].merge!(response)
301
+ # @nested_on_hash_cnt += 1
302
+ # else
303
+ if @building.nil?
304
+ return render
305
+ end
306
+ # end
307
+ end
308
+
309
+ def start_recording(params={})
310
+ if block_given?
311
+ create_nested_hash('start_recording', params)
312
+ instance_exec(&block)
313
+ @response[:tropo] << @nested_hash
314
+ else
315
+ hash = build_action('start_recording', params)
316
+ @response[:tropo] << hash
317
+ end
318
+
319
+ if @building.nil?
320
+ return render
321
+ end
322
+ end
323
+
324
+ def stop_recording
325
+ @response[:tropo] << { :stopRecording => nil }
326
+ render_response if @building.nil?
327
+ end
328
+
329
+ def transcription(params = {}, instance = false, &block)
330
+ hash = build_action('transcription', params)
331
+
332
+ if @nested_hash
333
+ @nested_hash[@nested_name.to_sym].merge!(hash)
334
+ else
335
+ # check if this is an instance, if it is we return the builded hash
336
+ if instance
337
+ return hash[:transcription]
338
+ end
339
+
340
+ @response[:tropo] << hash
341
+
342
+ if @building.nil?
343
+ return render
344
+ end
345
+ end
346
+ end
347
+
348
+ def transfer(params={}, &block)
349
+ if block_given?
350
+ create_nested_hash('transfer', params)
351
+ instance_exec(&block)
352
+ @response[:tropo] << @nested_hash
353
+ else
354
+ hash = build_action('transfer', params)
355
+ @response[:tropo] << hash
356
+ end
357
+
358
+ if @building.nil?
359
+ return render
360
+ end
361
+ end
362
+
363
+ def wait(params={}, &block)
364
+ if block_given?
365
+ create_nested_hash('wait', params)
366
+ instance_exec(&block)
367
+ @response[:tropo] << @nested_hash
368
+ else
369
+ hash = build_action('wait', params)
370
+ @response[:tropo] << hash
371
+ end
372
+
373
+ if @building.nil?
374
+ return render
375
+ end
376
+ end
377
+
378
+ # Here goes our private functions
379
+ private
380
+ def build_action(action, params)
381
+ if action == 'on'
382
+ return build_elements(params)
383
+ else
384
+ return { action.to_sym => build_elements(params) }
385
+ end
386
+ end
387
+
388
+ def build_elements(params)
389
+ hash = Hash.new
390
+
391
+ params.each_pair do |k,v|
392
+ if k.to_s.include? "_"
393
+ k = camelize(k.to_s)
394
+ k = k.to_sym if k
395
+ end
396
+ hash.merge!({ k => v })
397
+ end
398
+
399
+ return hash
400
+ end
401
+
402
+ def camelize(string)
403
+ split_string = string.split('_')
404
+ return_string = split_string[0] + split_string[1].capitalize
405
+ return_string = return_string + split_string[2].capitalize if split_string[2]
406
+
407
+ return return_string
408
+ end
409
+
410
+ def create_nested_hash(name, params)
411
+ @nested_hash = build_action(name, params)
412
+ @nested_name = name
413
+ end
414
+
415
+ def create_on_hash
416
+ @on_hash ||= { :on => Array.new }
417
+ end
418
+
419
+ def set_recognizer(params)
420
+ params.merge!({ :recognizer => @recognizer }) if params[:recognizer].nil? && @recognizer
421
+ return params
422
+ end
423
+ end