africastalking-ruby 2.1.8 → 2.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +35 -23
- data/lib/AfricasTalking/Mobiledata.rb +159 -0
- data/lib/AfricasTalking/version.rb +1 -1
- data/lib/AfricasTalking.rb +24 -12
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d1caf1f74d9b3372cbc660fa10382e4fdc5b9c0bf6a56b5bbc3db624efa68fe8
|
4
|
+
data.tar.gz: 7225710bdc096661538b16c80ac95c9131d85602f3e7defdc078a9c2104e796d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e5b1b59ee256c9d6f10b37ebcb0e1fe903766fc51e879c3c18f51ce7948e30731c8de594206d3de9ef82ebfafec82c60c5c08060dc899a72be16f564cbdcac5e
|
7
|
+
data.tar.gz: d6bceffcbede1d93df1872eb84749db148659fe6d6a3f2f91dacd48bc537f633d7fafb30b01e1d9353c8086a748dbb86aba85bebbc4d81d8de5eb334b6b8f97f
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -180,6 +180,41 @@ account = @AT.account
|
|
180
180
|
account.fetchUserData
|
181
181
|
```
|
182
182
|
|
183
|
+
### Mobile Data
|
184
|
+
|
185
|
+
Send mobile data to phone numbers
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
mobiledata = @AT.mobiledata
|
189
|
+
|
190
|
+
mobiledata.send options
|
191
|
+
```
|
192
|
+
- `options`
|
193
|
+
- `productName`: This is the application's product name. `REQUIRED`
|
194
|
+
- `recipients`: An array of objects containing the following keys:
|
195
|
+
|
196
|
+
- `phoneNumber`: Recipient of the mobile data. `REQUIRED`
|
197
|
+
- `quantity`: a numeric value for the amount of mobile data. It is based on the available mobile data package[(see "Bundle Package" column of mobile data pricing)](https://africastalking.com/pricing). `REQUIRED`
|
198
|
+
- `unit`: The units for the specified data quantity, the format is: ``MB`` or ``GB``. It is based on the available mobile data package[(see "Bundle Package" column of mobile data pricing)](https://africastalking.com/pricing). `REQUIRED`
|
199
|
+
- `validity`: The period of the data bundle’s validity this can be `Day`, `Week`, `BiWeek`, `Month`, or `Quarterly`. It is based on the available mobile data package [(see "Validity" column of mobile data pricing)](https://africastalking.com/pricing). `REQUIRED`
|
200
|
+
- `metadata`: A JSON object of any metadata that you would like us to associate with the request. `OPTIONAL`
|
201
|
+
|
202
|
+
#### Find Transaction
|
203
|
+
```ruby
|
204
|
+
mobiledata.findTransaction options
|
205
|
+
```
|
206
|
+
- Find a mobile data transaction
|
207
|
+
- `options`
|
208
|
+
- `transactionId`: ID of trancation to find `REQUIRED`
|
209
|
+
|
210
|
+
#### Fetch Wallet Balance
|
211
|
+
```ruby
|
212
|
+
mobiledata.fetchWalletBalance
|
213
|
+
```
|
214
|
+
- Fetch a mobile data product balance
|
215
|
+
|
216
|
+
For more information, please read the [https://developers.africastalking.com/docs/data/overview](https://developers.africastalking.com/docs/data/overview)
|
217
|
+
|
183
218
|
### Payments
|
184
219
|
```ruby
|
185
220
|
payments = @AT.payments
|
@@ -329,19 +364,7 @@ payments.mobileB2B options
|
|
329
364
|
- `requester`: PhoneNumber through which KPLC will send tokens when using B2B to buy electricity tokens.
|
330
365
|
- `metadata`: Some optional data to associate with transaction.`REQUIRED`
|
331
366
|
|
332
|
-
#### Mobile Data
|
333
|
-
```ruby
|
334
|
-
payments.mobileData options
|
335
|
-
```
|
336
|
-
- `options`
|
337
|
-
- `productName`: Your Payment Product. `REQUIRED`
|
338
|
-
- `recipients`: A list of recipients. Each recipient has:
|
339
367
|
|
340
|
-
- `phoneNumber`: Customer phone number (in international format). `REQUIRED`
|
341
|
-
- `quantity`: Mobile data amount. `REQUIRED`
|
342
|
-
- `unit`: Mobile data unit. Can either be `MB` or `GB`. `REQUIRED`
|
343
|
-
- `validity`: How long the mobile data is valid for. Must be one of `Day`, `Week` and `Month`. `REQUIRED`
|
344
|
-
- `metadata`: Additional data to associate with the transaction. `REQUIRED`
|
345
368
|
|
346
369
|
#### Wallet Transfer
|
347
370
|
```ruby
|
@@ -394,18 +417,7 @@ payments.fetchWalletTransactions options
|
|
394
417
|
- `endDate`: End Date to consider when fetching
|
395
418
|
- `categories`: Comma delimited list of categories to consider when fetching
|
396
419
|
|
397
|
-
#### Find Transaction
|
398
|
-
```ruby
|
399
|
-
payments.findTransaction options
|
400
|
-
```
|
401
|
-
- `options`
|
402
|
-
- `transactionId`: ID of trancation to find `REQUIRED`
|
403
420
|
|
404
|
-
#### Fetch Wallet Balance
|
405
|
-
```ruby
|
406
|
-
payments.fetchWalletBalance
|
407
|
-
```
|
408
|
-
- Fetch your payment wallet balance
|
409
421
|
|
410
422
|
|
411
423
|
## Development
|
@@ -0,0 +1,159 @@
|
|
1
|
+
class Mobiledata
|
2
|
+
include AfricasTalking
|
3
|
+
HTTP_CREATED = 201
|
4
|
+
HTTP_OK = 200
|
5
|
+
|
6
|
+
def initialize username, apikey
|
7
|
+
@username = username
|
8
|
+
@apikey = apikey
|
9
|
+
end
|
10
|
+
|
11
|
+
def send options
|
12
|
+
url = getMobileDataUrl()
|
13
|
+
|
14
|
+
validateParamsPresence? options, %w(productName)
|
15
|
+
recipients = options['recipients'].map do |item|
|
16
|
+
required_params = %w(phoneNumber quantity unit validity)
|
17
|
+
if item['metadata'].is_a?(Hash)
|
18
|
+
required_params << 'metadata'
|
19
|
+
end
|
20
|
+
|
21
|
+
validateParamsPresence?(item, required_params)
|
22
|
+
item
|
23
|
+
end
|
24
|
+
|
25
|
+
post_body = {
|
26
|
+
'username' => @username,
|
27
|
+
'productName' => options['productName'],
|
28
|
+
'recipients' => recipients,
|
29
|
+
}
|
30
|
+
|
31
|
+
idempotency_key = options['idempotencyKey'].to_s if options['idempotencyKey']
|
32
|
+
|
33
|
+
post_body['idempotencyKey'] = idempotency_key if idempotency_key
|
34
|
+
|
35
|
+
response = sendJSONRequest(url, post_body, idempotency_key)
|
36
|
+
|
37
|
+
if (@response_code == HTTP_CREATED)
|
38
|
+
responses = JSON.parse(response, :quirky_mode =>true)
|
39
|
+
|
40
|
+
if (responses['entries'].length > 0)
|
41
|
+
|
42
|
+
results = responses['entries'].collect{ |data|
|
43
|
+
|
44
|
+
MobileDataResponse.new data['phoneNumber'], data['provider'], data['status'], data['transactionId'], data['value']
|
45
|
+
}
|
46
|
+
|
47
|
+
return results
|
48
|
+
else
|
49
|
+
raise AfricasTalkingException, responses['errorMessage']
|
50
|
+
end
|
51
|
+
raise AfricasTalkingException, response
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def findTransaction options
|
56
|
+
validateParamsPresence? options, ['transactionId']
|
57
|
+
parameters = {
|
58
|
+
'username' => @username,
|
59
|
+
'transactionId' => options['transactionId']
|
60
|
+
}
|
61
|
+
url = getFindTransactionUrl()
|
62
|
+
response = sendJSONRequest(url, parameters, true, false)
|
63
|
+
if (@response_code == HTTP_OK)
|
64
|
+
resultObj = JSON.parse(response, :quirky_mode =>true)
|
65
|
+
transactionData = nil
|
66
|
+
if resultObj['status'] === 'Success'
|
67
|
+
transactionData = TransactionData.new resultObj['data']['requestMetadata'], resultObj['data']['sourceType'],resultObj['data']['source'], resultObj['data']['provider'], resultObj['data']['destinationType'],resultObj['data']['description'],
|
68
|
+
resultObj['data']['providerChannel'], resultObj['data']['providerRefId'], resultObj['data']['providerMetadata'],resultObj['data']['status'], resultObj['data']['productName'], resultObj['data']['category'],
|
69
|
+
resultObj['data']['transactionDate'], resultObj['data']['destination'], resultObj['data']['value'], resultObj['data']['transactionId'], resultObj['data']['creationTime']
|
70
|
+
end
|
71
|
+
return FindTransactionResponse.new resultObj['status'], transactionData
|
72
|
+
end
|
73
|
+
raise AfricasTalkingException, response
|
74
|
+
end
|
75
|
+
|
76
|
+
def fetchWalletBalance
|
77
|
+
parameters = { 'username' => @username }
|
78
|
+
url = getFetchWalletBalanceUrl()
|
79
|
+
response = sendJSONRequest(url, parameters, true, false)
|
80
|
+
if (@response_code == HTTP_OK)
|
81
|
+
resultObj = JSON.parse(response, :quirky_mode =>true)
|
82
|
+
|
83
|
+
return FetchWalletBalanceResponse.new resultObj['status'], resultObj['balance']
|
84
|
+
end
|
85
|
+
raise AfricasTalkingException, response
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
def getMobileDataUrl()
|
90
|
+
return getApiHost() + "/mobile/data/request"
|
91
|
+
end
|
92
|
+
|
93
|
+
def getFindTransactionUrl()
|
94
|
+
return getApiHost() + "/query/transaction/find"
|
95
|
+
end
|
96
|
+
|
97
|
+
def getFetchWalletBalanceUrl()
|
98
|
+
return getApiHost() + "/query/wallet/balance"
|
99
|
+
end
|
100
|
+
|
101
|
+
def getApiHost()
|
102
|
+
if(@username == "sandbox")
|
103
|
+
return "https://bundles.sandbox.africastalking.com"
|
104
|
+
else
|
105
|
+
return "https://bundles.africastalking.com"
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
class MobileDataResponse
|
111
|
+
attr_reader :phoneNumber, :provider, :status, :transactionId, :value
|
112
|
+
|
113
|
+
def initialize phoneNumber_, provider_, status_, transactionId_, value_
|
114
|
+
@phoneNumber = phoneNumber_
|
115
|
+
@provider = provider_
|
116
|
+
@status = status_
|
117
|
+
@transactionId = transactionId_
|
118
|
+
@value = value_
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
class FetchWalletBalanceResponse
|
123
|
+
attr_reader :status, :balance
|
124
|
+
def initialize status_, balance_
|
125
|
+
@status = status_
|
126
|
+
@balance = balance_
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
class FindTransactionResponse
|
131
|
+
attr_reader :status, :transactionData
|
132
|
+
def initialize status_, transactionData_
|
133
|
+
@status = status_
|
134
|
+
@transactionData = transactionData_
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
class TransactionData
|
139
|
+
attr_reader :requestMetadata, :sourceType, :source, :provider, :destinationType, :description, :providerChannel, :providerRefId, :providerMetadata, :status, :productName, :category, :transactionDate, :destination, :value, :transactionId, :creationTime
|
140
|
+
def initialize requestMetadata_, sourceType_, source_, provider_, destinationType_, description_, providerChannel_, providerRefId_, providerMetadata_, status_, productName_, category_, transactionDate_, destination_, value_, transactionId_, creationTime_
|
141
|
+
@requestMetadata =requestMetadata_
|
142
|
+
@sourceType = sourceType_
|
143
|
+
@source = source_
|
144
|
+
@provider = provider_
|
145
|
+
@destinationType = destinationType_
|
146
|
+
@description = description_
|
147
|
+
@providerChannel = providerChannel_
|
148
|
+
@providerRefId = providerRefId_
|
149
|
+
@providerMetadata = providerMetadata_
|
150
|
+
@status = status_
|
151
|
+
@productName = productName_
|
152
|
+
@category = category_
|
153
|
+
@transactionDate = transactionDate_
|
154
|
+
@destination = destination_
|
155
|
+
@value = value_
|
156
|
+
@transactionId = transactionId_
|
157
|
+
@creationTime = creationTime_
|
158
|
+
end
|
159
|
+
end
|
data/lib/AfricasTalking.rb
CHANGED
@@ -3,6 +3,7 @@ require 'httparty'
|
|
3
3
|
require "AfricasTalking/Sms"
|
4
4
|
require "AfricasTalking/Airtime"
|
5
5
|
require "AfricasTalking/Payments"
|
6
|
+
require "AfricasTalking/Mobiledata"
|
6
7
|
require "AfricasTalking/Voice"
|
7
8
|
require "AfricasTalking/Token"
|
8
9
|
require "AfricasTalking/Application"
|
@@ -17,29 +18,33 @@ module AfricasTalking
|
|
17
18
|
attr_accessor :username, :apikey
|
18
19
|
def initialize username, apikey
|
19
20
|
@username = username
|
20
|
-
@apikey = apikey
|
21
|
+
@apikey = apikey
|
21
22
|
end
|
22
|
-
|
23
|
+
|
23
24
|
def sms
|
24
|
-
return Sms.new @username, @apikey
|
25
|
+
return Sms.new @username, @apikey
|
25
26
|
end
|
26
|
-
|
27
|
+
|
27
28
|
def payments
|
28
29
|
return Payments.new @username, @apikey
|
29
30
|
end
|
30
|
-
|
31
|
+
|
32
|
+
def mobiledata
|
33
|
+
return Mobiledata.new @username, @apikey
|
34
|
+
end
|
35
|
+
|
31
36
|
def airtime
|
32
37
|
return Airtime.new @username, @apikey
|
33
38
|
end
|
34
|
-
|
39
|
+
|
35
40
|
def voice
|
36
41
|
return Voice.new @username, @apikey
|
37
42
|
end
|
38
|
-
|
43
|
+
|
39
44
|
def token
|
40
45
|
return Token.new @username, @apikey
|
41
46
|
end
|
42
|
-
|
47
|
+
|
43
48
|
def application
|
44
49
|
return Application.new @username, @apikey
|
45
50
|
end
|
@@ -54,8 +59,8 @@ module AfricasTalking
|
|
54
59
|
end
|
55
60
|
|
56
61
|
|
57
|
-
private
|
58
|
-
|
62
|
+
private
|
63
|
+
|
59
64
|
def sendNormalRequest url_, data_ = nil
|
60
65
|
uri = URI.parse(url_)
|
61
66
|
http = Net::HTTP.new(uri.host, uri.port)
|
@@ -78,7 +83,7 @@ module AfricasTalking
|
|
78
83
|
return response.body
|
79
84
|
end
|
80
85
|
|
81
|
-
def sendJSONRequest url_, data_, get_request = false
|
86
|
+
def sendJSONRequest url_, data_, get_request = false, idempotency_key
|
82
87
|
uri = URI.parse(url_)
|
83
88
|
http = Net::HTTP.new(uri.host, uri.port)
|
84
89
|
http.use_ssl = true
|
@@ -88,11 +93,18 @@ module AfricasTalking
|
|
88
93
|
else
|
89
94
|
req = Net::HTTP::Post.new(uri.request_uri, 'Content-Type'=>"application/json")
|
90
95
|
req.body = data_.to_json
|
96
|
+
|
91
97
|
end
|
92
|
-
|
98
|
+
|
93
99
|
req["apikey"] = @apikey
|
94
100
|
req["Accept"] = "application/json"
|
101
|
+
|
102
|
+
if idempotency_key
|
103
|
+
req['Idempotency-Key'] = idempotency_key
|
104
|
+
end
|
105
|
+
|
95
106
|
response = http.request(req)
|
107
|
+
|
96
108
|
if (DEBUG)
|
97
109
|
puts "Full response #{response.body}"
|
98
110
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: africastalking-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
4
|
+
version: 2.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michael Mwirigi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -135,6 +135,7 @@ files:
|
|
135
135
|
- lib/AfricasTalking.rb
|
136
136
|
- lib/AfricasTalking/Airtime.rb
|
137
137
|
- lib/AfricasTalking/Application.rb
|
138
|
+
- lib/AfricasTalking/Mobiledata.rb
|
138
139
|
- lib/AfricasTalking/Payments.rb
|
139
140
|
- lib/AfricasTalking/Sms.rb
|
140
141
|
- lib/AfricasTalking/Token.rb
|