mtn_open_api 0.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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +0 -0
- data/README.md +244 -0
- data/Rakefile +4 -0
- data/generate_readme.rb +108 -0
- data/lib/mtn_open_api/version.rb +6 -0
- data/lib/mtn_open_api.rb +164 -0
- data/mtn_open_api.gemspec +42 -0
- data/readme-template +35 -0
- data/schemas/collection.yaml +2370 -0
- data/schemas/disbursement.yaml +1907 -0
- data/schemas/remittance.yaml +1452 -0
- data/schemas/sandbox-provisioning-api.yaml +187 -0
- data/sig/mtn_open_api.rbs +4 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 69c64be9e9ce6ee28ec2ddf18934a108a79ba5f366d4af89c7ceab92aa80b53b
|
4
|
+
data.tar.gz: 7d9a36636282ae54e05414c08a3bb0b43c1d0aece8af58a97eac61b00fb33f15
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 031b3b81bc9131d303e470b1509b92b8a893a344be3405a5c0a1d19410af4ee8d73b64abc77915c6fd210de0aa125e50f2bb92a6433aa33a451c8cf352ffe79b
|
7
|
+
data.tar.gz: 0c03d7452e238be59cf8804cab86c29a3a0f480cfae6b019dc69585f3ea55cb2906c8aedb20490979251cbe81ff0d49464c64cafb0d0eb339ba9fef6a0c4d55e
|
data/CHANGELOG.md
ADDED
File without changes
|
data/README.md
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
# Mtn Open Api
|
2
|
+
Explore seamless integration with MTN Open API using this Ruby gem. For in-depth information, refer to the official documentation at [https://momodeveloper.mtn.com/](https://momodeveloper.mtn.com/).
|
3
|
+
|
4
|
+
## Key Features
|
5
|
+
- **Full API Availability:** Access a comprehensive set of API endpoints, providing developers with a versatile toolkit to address a wide range of scenarios. The gem's commitment to inclusivity ensures that all necessary API endpoints are readily available for integration.
|
6
|
+
|
7
|
+
- **Robust Testing:** Every endpoint is rigorously tested to ensure reliability and stability. By adhering to best practices in testing, the `Mtn Open Api` gem delivers a dependable solution that developers can trust for their projects.
|
8
|
+
|
9
|
+
- **Intuitive Conversion:** Experience a seamless transition from JSON responses to structured `OpenStruct` objects, providing a more Ruby-like interface for effortless interaction.
|
10
|
+
|
11
|
+
- **Comprehensive Documentation:** All methods are thoroughly documented, ensuring that developers have access to clear and concise information on the functionality and usage of each feature. This commitment to documentation enhances the gem's usability and simplifies the integration process.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Install the gem and add to the application's Gemfile by executing:
|
16
|
+
|
17
|
+
$ bundle add mtn_open_api
|
18
|
+
|
19
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
20
|
+
|
21
|
+
$ gem install mtn_open_api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
## MtnOpenApi::SandboxProvisioningApi
|
26
|
+
Sandbox User Provisioning
|
27
|
+
|
28
|
+
This class provides a convenient interface for making requests to the MTN MoMo `sandbox-provisioning-api` API.
|
29
|
+
#### Initialization
|
30
|
+
You can initialize it with the following parameters:
|
31
|
+
|
32
|
+
- `base_url` (optional): The base URL for the MTN MoMo `sandbox-provisioning-api` API. If not provided, the default value is https://sandbox.momodeveloper.mtn.com.
|
33
|
+
- `headers` (optional): A hash of HTTP headers to be included in the API requests.
|
34
|
+
- `schema` (optional): A hash representing the API schema. If not provided, the gem will use the default schema, which is already included in the gem. You can also download the schema from [https://momodeveloper.mtn.com/API-collections#api=sandbox-provisioning-api](https://momodeveloper.mtn.com/API-collections#api=sandbox-provisioning-api).
|
35
|
+
|
36
|
+
#### Example
|
37
|
+
```ruby
|
38
|
+
common_headers = {
|
39
|
+
"Authorization": "string",
|
40
|
+
"X-Callback-Url": "string",
|
41
|
+
"X-Target-Environment": "string",
|
42
|
+
"Ocp-Apim-Subscription-Key": "string"
|
43
|
+
}
|
44
|
+
|
45
|
+
sandboxprovisioningapi = MtnOpenApi::SandboxProvisioningApi.new(
|
46
|
+
headers: common_headers
|
47
|
+
)
|
48
|
+
|
49
|
+
params = {
|
50
|
+
"providerCallbackHost": "string"
|
51
|
+
}
|
52
|
+
|
53
|
+
extra_headers = {
|
54
|
+
"X-Reference-Id": "string"
|
55
|
+
}
|
56
|
+
|
57
|
+
response = sandboxprovisioningapi.postV10Apiuser(params, extra_headers)
|
58
|
+
```
|
59
|
+
#### Methods
|
60
|
+
- [postV10Apiuser](https://momodeveloper.mtn.com/API-collections#api=sandbox-provisioning-api&operation=post-v1_0-apiuser) Used to create an API user in the sandbox target environment.
|
61
|
+
- [postV10ApiuserApikey](https://momodeveloper.mtn.com/API-collections#api=sandbox-provisioning-api&operation=post-v1_0-apiuser-apikey) Used to create an API key for an API user in the sandbox target environment.
|
62
|
+
- [getV10Apiuser](https://momodeveloper.mtn.com/API-collections#api=sandbox-provisioning-api&operation=get-v1_0-apiuser) Used to get API user information.
|
63
|
+
|
64
|
+
## MtnOpenApi::Collection
|
65
|
+
Enable remote collection of bills, fees or taxes
|
66
|
+
|
67
|
+
This class provides a convenient interface for making requests to the MTN MoMo `collection` API.
|
68
|
+
#### Initialization
|
69
|
+
You can initialize it with the following parameters:
|
70
|
+
|
71
|
+
- `base_url` (optional): The base URL for the MTN MoMo `collection` API. If not provided, the default value is https://sandbox.momodeveloper.mtn.com/collection.
|
72
|
+
- `headers` (optional): A hash of HTTP headers to be included in the API requests.
|
73
|
+
- `schema` (optional): A hash representing the API schema. If not provided, the gem will use the default schema, which is already included in the gem. You can also download the schema from [https://momodeveloper.mtn.com/API-collections#api=collection](https://momodeveloper.mtn.com/API-collections#api=collection).
|
74
|
+
|
75
|
+
#### Example
|
76
|
+
```ruby
|
77
|
+
common_headers = {
|
78
|
+
"Authorization": "string",
|
79
|
+
"X-Callback-Url": "string",
|
80
|
+
"X-Target-Environment": "string",
|
81
|
+
"Ocp-Apim-Subscription-Key": "string"
|
82
|
+
}
|
83
|
+
|
84
|
+
collection = MtnOpenApi::Collection.new(
|
85
|
+
headers: common_headers
|
86
|
+
)
|
87
|
+
|
88
|
+
params = {
|
89
|
+
"amount": "string",
|
90
|
+
"currency": "string",
|
91
|
+
"externalId": "string",
|
92
|
+
"payer": {
|
93
|
+
"partyIdType": "MSISDN",
|
94
|
+
"partyId": "string"
|
95
|
+
},
|
96
|
+
"payerMessage": "string",
|
97
|
+
"payeeNote": "string"
|
98
|
+
}
|
99
|
+
|
100
|
+
extra_headers = {
|
101
|
+
"X-Reference-Id": "string"
|
102
|
+
}
|
103
|
+
|
104
|
+
response = collection.requesttoPay(params, extra_headers)
|
105
|
+
```
|
106
|
+
#### Methods
|
107
|
+
- [getAccountBalance](https://momodeveloper.mtn.com/API-collections#api=collection&operation=GetAccountBalance) Get the balance of own account.
|
108
|
+
- [validateAccountHolderStatus](https://momodeveloper.mtn.com/API-collections#api=collection&operation=ValidateAccountHolderStatus) Operation is used to check if an account holder is registered and active in the system.
|
109
|
+
- [requesttoPay](https://momodeveloper.mtn.com/API-collections#api=collection&operation=RequesttoPay) This operation is used to request a payment from a consumer (Payer). The payer will be asked to authorize the payment. The transaction will be executed once the payer has authorized the payment. The requesttopay will be in status PENDING until the transaction is authorized or declined by the payer or it is timed out by the system.
|
110
|
+
Status of the transaction can be validated by using the GET /requesttopay/\<resourceId\>
|
111
|
+
- [requesttoPayTransactionStatus](https://momodeveloper.mtn.com/API-collections#api=collection&operation=RequesttoPayTransactionStatus) This operation is used to get the status of a request to pay. X-Reference-Id that was passed in the post is used as reference to the request.
|
112
|
+
- [bcAuthorize](https://momodeveloper.mtn.com/API-collections#api=collection&operation=bc-authorize) This operation is used to claim a consent by the account holder for the requested scopes.
|
113
|
+
- [getBasicUserinfo](https://momodeveloper.mtn.com/API-collections#api=collection&operation=GetBasicUserinfo) This operation returns personal information of the account holder. The operation does not need any consent by the account holder.
|
114
|
+
- [requesttoPayDeliveryNotification](https://momodeveloper.mtn.com/API-collections#api=collection&operation=RequesttoPayDeliveryNotification) This operation is used to send additional Notification to an End User.
|
115
|
+
- [getAccountBalanceInSpecificCurrency](https://momodeveloper.mtn.com/API-collections#api=collection&operation=GetAccountBalanceInSpecificCurrency) Get the balance of own account. Currency parameter passed in GET
|
116
|
+
- [requestToWithdrawV1](https://momodeveloper.mtn.com/API-collections#api=collection&operation=RequestToWithdraw-V1) This operation is used to request a withdrawal (cash-out) from a consumer (Payer). The payer will be asked to authorize the withdrawal. The transaction will be executed once the payer has authorized the withdrawal
|
117
|
+
- [requestToWithdrawV2](https://momodeveloper.mtn.com/API-collections#api=collection&operation=RequestToWithdraw-V2) This operation is used to request a withdrawal (cash-out) from a consumer (Payer). The payer will be asked to authorize the withdrawal. The transaction will be executed once the payer has authorized the withdrawal
|
118
|
+
- [requestToWithdrawTransactionStatus](https://momodeveloper.mtn.com/API-collections#api=collection&operation=RequestToWithdrawTransactionStatus) This operation is used to get the status of a request to withdraw. X-Reference-Id that was passed in the post is used as reference to the request.
|
119
|
+
- [createInvoice](https://momodeveloper.mtn.com/API-collections#api=collection&operation=CreateInvoice) A merchant may use this in order to create an invoice that can be paid by an intended payer via any channel at a later stage.
|
120
|
+
- [getInvoiceStatus](https://momodeveloper.mtn.com/API-collections#api=collection&operation=GetInvoiceStatus) This operation is used to get the status of an invoice. X-Reference-Id that was passed in the post is used as reference to the request
|
121
|
+
- [cancelInvoice](https://momodeveloper.mtn.com/API-collections#api=collection&operation=CancelInvoice) This operation is used to delete an invoice. The ReferenceId is associated with the invoice to be cancelled
|
122
|
+
- [preApproval](https://momodeveloper.mtn.com/API-collections#api=collection&operation=PreApproval) Preapproval operation is used to create a pre-approval.
|
123
|
+
- [getPreApprovalStatus](https://momodeveloper.mtn.com/API-collections#api=collection&operation=GetPreApprovalStatus) This operation is used to get the status of a pre-approval. X-Reference-Id that was passed in the post is used as reference to the request.
|
124
|
+
- [createOauth2token](https://momodeveloper.mtn.com/API-collections#api=collection&operation=CreateOauth2Token) This operation is used to claim a consent by the account holder for the requested scopes.
|
125
|
+
- [getUserInfoWithConsent](https://momodeveloper.mtn.com/API-collections#api=collection&operation=GetUserInfoWithConsent) This operation is used to claim a consent by the account holder for the requested scopes.
|
126
|
+
- [createAccessToken](https://momodeveloper.mtn.com/API-collections#api=collection&operation=CreateAccessToken) This operation is used to create an access token which can then be used to authorize and authenticate towards the other end-points of the API.
|
127
|
+
- [createPayments](https://momodeveloper.mtn.com/API-collections#api=collection&operation=CreatePayments) Making it possible to perform payments via the partner gateway. This may be used to pay for external bills or to perform air-time top-ups.
|
128
|
+
- [getPaymentStatus](https://momodeveloper.mtn.com/API-collections#api=collection&operation=GetPaymentStatus) This operation is used to get the status of a Payment. X-Reference-Id that was passed in the post is used as reference to the request
|
129
|
+
|
130
|
+
## MtnOpenApi::Disbursement
|
131
|
+
Automatically deposit funds to multiple users
|
132
|
+
|
133
|
+
This class provides a convenient interface for making requests to the MTN MoMo `disbursement` API.
|
134
|
+
#### Initialization
|
135
|
+
You can initialize it with the following parameters:
|
136
|
+
|
137
|
+
- `base_url` (optional): The base URL for the MTN MoMo `disbursement` API. If not provided, the default value is https://sandbox.momodeveloper.mtn.com/disbursement.
|
138
|
+
- `headers` (optional): A hash of HTTP headers to be included in the API requests.
|
139
|
+
- `schema` (optional): A hash representing the API schema. If not provided, the gem will use the default schema, which is already included in the gem. You can also download the schema from [https://momodeveloper.mtn.com/API-collections#api=disbursement](https://momodeveloper.mtn.com/API-collections#api=disbursement).
|
140
|
+
|
141
|
+
#### Example
|
142
|
+
```ruby
|
143
|
+
common_headers = {
|
144
|
+
"Authorization": "string",
|
145
|
+
"X-Callback-Url": "string",
|
146
|
+
"X-Target-Environment": "string",
|
147
|
+
"Ocp-Apim-Subscription-Key": "string"
|
148
|
+
}
|
149
|
+
|
150
|
+
disbursement = MtnOpenApi::Disbursement.new(
|
151
|
+
headers: common_headers
|
152
|
+
)
|
153
|
+
|
154
|
+
params = "login_hint=ID:{msisdn}/MSISDN&scope={scope}&access_type={online/offline}"
|
155
|
+
|
156
|
+
extra_headers = {}
|
157
|
+
|
158
|
+
response = disbursement.bcAuthorize(params, extra_headers)
|
159
|
+
```
|
160
|
+
#### Methods
|
161
|
+
- [getAccountBalance](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=GetAccountBalance) Get the balance of own account.
|
162
|
+
- [validateAccountHolderStatus](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=ValidateAccountHolderStatus) Operation is used to check if an account holder is registered and active in the system.
|
163
|
+
- [getTransferStatus](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=GetTransferStatus) This operation is used to get the status of a transfer. X-Reference-Id that was passed in the post is used as reference to the request.
|
164
|
+
- [getBasicUserinfo](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=GetBasicUserinfo) This operation returns personal information of the account holder. The operation does not need any consent by the account holder.
|
165
|
+
- [bcAuthorize](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=bc-authorize) This operation is used to claim a consent by the account holder for the requested scopes.
|
166
|
+
- [getAccountBalanceInSpecificCurrency](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=GetAccountBalanceInSpecificCurrency) Get the balance of own account. Currency parameter passed in GET
|
167
|
+
- [depositV1](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=Deposit-V1) deposit operation is used to deposit an amount from the owner’s account to a payee account.<br> Status of the transaction can be validated by using the GET /deposit/\{referenceId\}
|
168
|
+
- [depositV2](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=Deposit-V2) deposit operation is used to deposit an amount from the owner’s account to a payee account.<br> Status of the transaction can be validated by using the GET /deposit/\{referenceId\}
|
169
|
+
- [getDepositStatus](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=GetDepositStatus) This operation is used to get the status of a deposit. X-Reference-Id that was passed in the post is used as reference to the request.
|
170
|
+
- [refundV1](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=Refund-V1) refund operation is used to refund an amount from the owner’s account to a payee account.<br> Status of the transaction can be validated by using the GET /refund/\{referenceId\}
|
171
|
+
- [refundV2](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=Refund-V2) refund operation is used to refund an amount from the owner’s account to a payee account.<br> Status of the transaction can be validated by using the GET /refund/\{referenceId\}
|
172
|
+
- [getRefundStatus](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=GetRefundStatus) This operation is used to get the status of a refund. X-Reference-Id that was passed in the post is used as reference to the request.
|
173
|
+
- [createOauth2token](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=CreateOauth2Token) This operation is used to claim a consent by the account holder for the requested scopes.
|
174
|
+
- [getUserInfoWithConsent](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=GetUserInfoWithConsent) This operation is used to claim a consent by the account holder for the requested scopes.
|
175
|
+
- [createAccessToken](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=CreateAccessToken) This operation is used to create an access token which can then be used to authorize and authenticate towards the other end-points of the API.
|
176
|
+
- [transfer](https://momodeveloper.mtn.com/API-collections#api=disbursement&operation=Transfer) Transfer operation is used to transfer an amount from the own account to a payee account.<br> Status of the transaction can validated by using the GET /transfer/\{referenceId\}
|
177
|
+
|
178
|
+
## MtnOpenApi::Remittance
|
179
|
+
Remit funds to local recipients from the diaspora with ease
|
180
|
+
|
181
|
+
This class provides a convenient interface for making requests to the MTN MoMo `remittance` API.
|
182
|
+
#### Initialization
|
183
|
+
You can initialize it with the following parameters:
|
184
|
+
|
185
|
+
- `base_url` (optional): The base URL for the MTN MoMo `remittance` API. If not provided, the default value is https://sandbox.momodeveloper.mtn.com/remittance.
|
186
|
+
- `headers` (optional): A hash of HTTP headers to be included in the API requests.
|
187
|
+
- `schema` (optional): A hash representing the API schema. If not provided, the gem will use the default schema, which is already included in the gem. You can also download the schema from [https://momodeveloper.mtn.com/API-collections#api=remittance](https://momodeveloper.mtn.com/API-collections#api=remittance).
|
188
|
+
|
189
|
+
#### Example
|
190
|
+
```ruby
|
191
|
+
common_headers = {
|
192
|
+
"Authorization": "string",
|
193
|
+
"X-Callback-Url": "string",
|
194
|
+
"X-Target-Environment": "string",
|
195
|
+
"Ocp-Apim-Subscription-Key": "string"
|
196
|
+
}
|
197
|
+
|
198
|
+
remittance = MtnOpenApi::Remittance.new(
|
199
|
+
headers: common_headers
|
200
|
+
)
|
201
|
+
|
202
|
+
params = {
|
203
|
+
"amount": "string",
|
204
|
+
"currency": "string",
|
205
|
+
"externalId": "string",
|
206
|
+
"payee": {
|
207
|
+
"partyIdType": "MSISDN",
|
208
|
+
"partyId": "string"
|
209
|
+
},
|
210
|
+
"payerMessage": "string",
|
211
|
+
"payeeNote": "string"
|
212
|
+
}
|
213
|
+
|
214
|
+
extra_headers = {
|
215
|
+
"X-Reference-Id": "string"
|
216
|
+
}
|
217
|
+
|
218
|
+
response = remittance.transfer(params, extra_headers)
|
219
|
+
```
|
220
|
+
#### Methods
|
221
|
+
- [getAccountBalance](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=GetAccountBalance) Get the balance of own account.
|
222
|
+
- [validateAccountHolderStatus](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=ValidateAccountHolderStatus) Operation is used to check if an account holder is registered and active in the system.
|
223
|
+
- [transfer](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=Transfer) Transfer operation is used to transfer an amount from the own account to a payee account.<br> Status of the transaction can validated by using the GET /transfer/\{referenceId\}
|
224
|
+
- [getTransferStatus](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=GetTransferStatus) This operation is used to get the status of a transfer. X-Reference-Id that was passed in the post is used as reference to the request.
|
225
|
+
- [getBasicUserinfo](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=GetBasicUserinfo) This operation returns personal information of the account holder. The operation does not need any consent by the account holder.
|
226
|
+
- [bcAuthorize](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=bc-authorize) This operation is used to claim a consent by the account holder for the requested scopes.
|
227
|
+
- [getAccountBalanceInSpecificCurrency](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=GetAccountBalanceInSpecificCurrency) Get the balance of own account. Currency parameter passed in GET
|
228
|
+
- [createOauth2token](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=CreateOauth2Token) This operation is used to claim a consent by the account holder for the requested scopes.
|
229
|
+
- [getUserInfoWithConsent](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=GetUserInfoWithConsent) This operation is used to claim a consent by the account holder for the requested scopes.
|
230
|
+
- [createAccessToken](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=CreateAccessToken) This operation is used to create an access token which can then be used to authorize and authenticate towards the other end-points of the API.
|
231
|
+
- [cashTransfer](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=CashTransfer) Cash transfer operation is used to transfer an amount from the owner’s account to a payee account. Status of the transaction can be validated by using GET /cashtransfer/{referenceId}
|
232
|
+
- [getCashTransferStatus](https://momodeveloper.mtn.com/API-collections#api=remittance&operation=GetCashTransferStatus) This operation is used to get the status of a transfer. X-Reference-Id that was passed in the post is used as reference to the request.
|
233
|
+
|
234
|
+
|
235
|
+
|
236
|
+
## Development
|
237
|
+
|
238
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
239
|
+
|
240
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
241
|
+
|
242
|
+
## Contributing
|
243
|
+
|
244
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/koodeyo/mtn_open_api.
|
data/Rakefile
ADDED
data/generate_readme.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
require_relative "lib/mtn_open_api"
|
2
|
+
|
3
|
+
def get_sample_method(paths, method_name = "post")
|
4
|
+
post = nil
|
5
|
+
|
6
|
+
paths.each do |path, methods|
|
7
|
+
methods.each do |http_method, details|
|
8
|
+
if http_method === method_name
|
9
|
+
post = details
|
10
|
+
break # This breaks out of the inner loop when a method_name method is found
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
break if post # This breaks out of the outer loop when a method_name method is found
|
15
|
+
end
|
16
|
+
|
17
|
+
post
|
18
|
+
end
|
19
|
+
|
20
|
+
def arr_to_h(arr)
|
21
|
+
arr.to_h {|key| [key, "string"]}
|
22
|
+
end
|
23
|
+
|
24
|
+
def api_docs
|
25
|
+
output = ""
|
26
|
+
|
27
|
+
MtnOpenApi.schemas.each do |schema|
|
28
|
+
namespace = schema[:namespace]
|
29
|
+
content = schema[:content]
|
30
|
+
paths = content['paths']
|
31
|
+
info = content["info"]
|
32
|
+
api = schema[:api]
|
33
|
+
|
34
|
+
default_base_url = content["servers"].first['url']
|
35
|
+
instance_name = namespace.downcase
|
36
|
+
module_name = "MtnOpenApi"
|
37
|
+
|
38
|
+
# Get sample request
|
39
|
+
sample_method = get_sample_method(paths) || get_sample_method(paths, "get")
|
40
|
+
# Sample parameters
|
41
|
+
sample_parameters = sample_method["parameters"]
|
42
|
+
sample_headers = sample_parameters.select { |p| p["in"] === 'header' }.map { |p| p["name"]}
|
43
|
+
sample_body = sample_method["requestBody"]["content"]
|
44
|
+
sample_json = sample_body["application/json"]
|
45
|
+
sample_urlencoded = sample_body["application/x-www-form-urlencoded"]
|
46
|
+
# Formatting params with application/x-www-form-urlencoded as string
|
47
|
+
params_prefix_surfix = "#{!sample_json ? '"': ""}"
|
48
|
+
|
49
|
+
# Headers
|
50
|
+
sample_common_headers = ["Authorization", "X-Callback-Url", "X-Target-Environment", "Ocp-Apim-Subscription-Key"]
|
51
|
+
formatted_common_headers = JSON.pretty_generate(arr_to_h(sample_common_headers))
|
52
|
+
extra_headers = sample_headers - sample_common_headers
|
53
|
+
|
54
|
+
# Formattig document
|
55
|
+
# Class documenting
|
56
|
+
output += "## #{module_name}::#{namespace}\n"
|
57
|
+
output += "#{info["description"]}\n\n"
|
58
|
+
output += "This class provides a convenient interface for making requests to the MTN MoMo `#{api}` API.\n"
|
59
|
+
output += "#### Initialization\n"
|
60
|
+
output += "You can initialize it with the following parameters:\n"
|
61
|
+
output +="
|
62
|
+
- `base_url` (optional): The base URL for the MTN MoMo `#{api}` API. If not provided, the default value is #{default_base_url}.
|
63
|
+
- `headers` (optional): A hash of HTTP headers to be included in the API requests.
|
64
|
+
- `schema` (optional): A hash representing the API schema. If not provided, the gem will use the default schema, which is already included in the gem. You can also download the schema from [https://momodeveloper.mtn.com/API-collections#api=#{api}](https://momodeveloper.mtn.com/API-collections#api=#{api}).
|
65
|
+
"
|
66
|
+
|
67
|
+
output += "\n#### Example
|
68
|
+
```ruby
|
69
|
+
common_headers = #{formatted_common_headers}
|
70
|
+
|
71
|
+
#{instance_name} = MtnOpenApi::#{namespace}.new(
|
72
|
+
headers: common_headers
|
73
|
+
)
|
74
|
+
|
75
|
+
params = #{params_prefix_surfix}#{ sample_json ? JSON.pretty_generate(sample_json['example']) : sample_urlencoded['example'] }#{params_prefix_surfix}
|
76
|
+
|
77
|
+
extra_headers = #{JSON.send(extra_headers.size > 0 ? :pretty_generate : :dump, arr_to_h(sample_headers - sample_common_headers))}
|
78
|
+
|
79
|
+
response = #{instance_name}.#{MtnOpenApi.to_camel_case(sample_method['operationId'])}(params, extra_headers)
|
80
|
+
```"
|
81
|
+
|
82
|
+
output += "\n"
|
83
|
+
output += "#### Methods\n"
|
84
|
+
|
85
|
+
paths.each do |path, methods|
|
86
|
+
methods.each do |http_method, details|
|
87
|
+
method_name = MtnOpenApi.to_camel_case(details['operationId'])
|
88
|
+
output += "- [#{method_name}](https://momodeveloper.mtn.com/API-collections#api=#{api}&operation=#{details['operationId']})"
|
89
|
+
output += " #{details['description']}"
|
90
|
+
output += "\n"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
output += "\n"
|
95
|
+
end
|
96
|
+
|
97
|
+
output
|
98
|
+
end
|
99
|
+
|
100
|
+
readme_content = File.read('readme-template')
|
101
|
+
|
102
|
+
# Replace "TODO: Write usage instructions here"
|
103
|
+
new_content = readme_content.gsub('TODO: Write usage instructions here', api_docs)
|
104
|
+
|
105
|
+
# Write the modified content back to the README file
|
106
|
+
File.write("README.md", new_content)
|
107
|
+
|
108
|
+
puts 'README file updated successfully!'
|
data/lib/mtn_open_api.rb
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uri'
|
4
|
+
require "yaml"
|
5
|
+
require 'json'
|
6
|
+
require 'ostruct'
|
7
|
+
require 'net/http'
|
8
|
+
require 'json-schema'
|
9
|
+
require_relative "mtn_open_api/version"
|
10
|
+
|
11
|
+
module MtnOpenApi
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
def self.to_camel_case(operationId)
|
15
|
+
# AbCd to ab_cd
|
16
|
+
operationId = operationId.gsub(/\W/, '_').gsub(/([a-z])([A-Z])/, '\1_\2').downcase
|
17
|
+
# _ -
|
18
|
+
words = operationId.split(/[_-]/)
|
19
|
+
camel_case_words = words.map.with_index { |w, i| i.zero? ? w : w.capitalize }
|
20
|
+
camel_case_words.join
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.schemas
|
24
|
+
[
|
25
|
+
{
|
26
|
+
api: 'sandbox-provisioning-api',
|
27
|
+
namespace: "SandboxProvisioningApi",
|
28
|
+
content: MtnOpenApi.load_schema("sandbox-provisioning-api")
|
29
|
+
},
|
30
|
+
{
|
31
|
+
api: "collection",
|
32
|
+
namespace: 'Collection',
|
33
|
+
content: MtnOpenApi.load_schema("collection")
|
34
|
+
},
|
35
|
+
{
|
36
|
+
api: "disbursement",
|
37
|
+
namespace: "Disbursement",
|
38
|
+
content: MtnOpenApi.load_schema("disbursement")
|
39
|
+
},
|
40
|
+
{
|
41
|
+
api: 'remittance',
|
42
|
+
namespace: "Remittance",
|
43
|
+
content: MtnOpenApi.load_schema("remittance")
|
44
|
+
}
|
45
|
+
]
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.load_schema(filename)
|
49
|
+
gem_root = Gem::Specification.find_by_name(NAME).gem_dir
|
50
|
+
yaml_path = File.expand_path("schemas/#{filename}.yaml", gem_root)
|
51
|
+
YAML.load(File.read(yaml_path))
|
52
|
+
end
|
53
|
+
|
54
|
+
# Dynamically define classes based on schemas
|
55
|
+
schemas.each do |schema|
|
56
|
+
class_name = schema[:namespace]
|
57
|
+
content = schema[:content]
|
58
|
+
next if content.nil?
|
59
|
+
|
60
|
+
default_base_url = content["servers"].first['url']
|
61
|
+
|
62
|
+
class_content = Class.new do
|
63
|
+
attr_reader :base_url, :common_headers
|
64
|
+
|
65
|
+
define_method(:initialize) do |schema: content, base_url: default_base_url, headers: {}|
|
66
|
+
@base_url = URI.parse(base_url)
|
67
|
+
@common_headers = headers
|
68
|
+
|
69
|
+
# Define methods
|
70
|
+
define_methods(content)
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def define_methods(schema)
|
76
|
+
schema['paths'].each do |path, methods|
|
77
|
+
methods.each do |http_method, details|
|
78
|
+
operationId = details['operationId'] || "#{http_method} #{path}"
|
79
|
+
# Remove non-word characters (including "-") and convert to camel case
|
80
|
+
method_name = MtnOpenApi.to_camel_case(operationId)
|
81
|
+
define_singleton_method(method_name) do |params = {}, headers = {}|
|
82
|
+
headers = common_headers.merge(headers)
|
83
|
+
validate_parameters(http_method, path, headers, params, details)
|
84
|
+
make_request(http_method, path, headers, params)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def validate_parameters(http_method, path, headers, params, operation_details)
|
91
|
+
# Validate required headers
|
92
|
+
required_headers = operation_details['parameters'].select { |param| param['in'] == 'header' && param['required'] }
|
93
|
+
validate_required_parameters(headers, required_headers)
|
94
|
+
|
95
|
+
# Validate required path parameters
|
96
|
+
path_params = path.scan(/{(\w+)}/).flatten
|
97
|
+
required_path_params = operation_details['parameters'].select { |param| param['in'] == 'path' && path_params.include?(param['name']) && param['required'] }
|
98
|
+
validate_required_parameters(params, required_path_params)
|
99
|
+
|
100
|
+
# Validate required query parameters
|
101
|
+
required_query_params = operation_details['parameters'].select { |param| param['in'] == 'query' && param['required'] }
|
102
|
+
validate_required_parameters(params, required_query_params)
|
103
|
+
end
|
104
|
+
|
105
|
+
def validate_required_parameters(data, schema)
|
106
|
+
schema.each do |param_schema|
|
107
|
+
param_name = param_schema['name']
|
108
|
+
# Check if the parameter is present in the data
|
109
|
+
unless data.key?(param_name)
|
110
|
+
raise ArgumentError, "Missing required parameter: #{param_name}"
|
111
|
+
end
|
112
|
+
|
113
|
+
# Validate the parameter against the schema
|
114
|
+
JSON::Validator.validate!(param_schema['schema'], data[param_name])
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def make_request(http_method, path, headers, params)
|
119
|
+
url = "#{base_url}#{replace_path_variables(path, params)}"
|
120
|
+
uri = URI.parse(url.to_s)
|
121
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
122
|
+
http.use_ssl = (uri.scheme == 'https')
|
123
|
+
|
124
|
+
request = case http_method.downcase
|
125
|
+
when 'get'
|
126
|
+
uri.query = URI.encode_www_form(params)
|
127
|
+
Net::HTTP::Get.new(uri.request_uri)
|
128
|
+
when 'post'
|
129
|
+
Net::HTTP::Post.new(uri.request_uri)
|
130
|
+
when 'put'
|
131
|
+
Net::HTTP::Put.new(uri.request_uri)
|
132
|
+
when 'delete'
|
133
|
+
Net::HTTP::Delete.new(uri.request_uri)
|
134
|
+
# Add other HTTP methods as needed
|
135
|
+
else
|
136
|
+
raise "Unsupported HTTP method: #{http_method}"
|
137
|
+
end
|
138
|
+
|
139
|
+
# Set headers
|
140
|
+
headers.merge(
|
141
|
+
'Content-Type' => 'application/json'
|
142
|
+
).each { |key, value| request[key] = value }
|
143
|
+
|
144
|
+
# Set request body for 'POST' and 'PUT'
|
145
|
+
request.body = params.to_json if %w[post put].include?(http_method.downcase)
|
146
|
+
|
147
|
+
# Make the request
|
148
|
+
response = http.request(request)
|
149
|
+
|
150
|
+
begin
|
151
|
+
JSON.parse(response.body, object_class: OpenStruct)
|
152
|
+
rescue
|
153
|
+
OpenStruct.new(statusCode: response.code.to_i, message: response.body)
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def replace_path_variables(path, params)
|
158
|
+
path.gsub(/{([^{}]+)}/) { params[$1] }
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
const_set(class_name, class_content)
|
163
|
+
end
|
164
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/mtn_open_api/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = MtnOpenApi::NAME
|
7
|
+
spec.version = MtnOpenApi::VERSION
|
8
|
+
spec.authors = ["Paul Jeremiah Mugaya"]
|
9
|
+
spec.email = ["paulgrammer@koodeyo.com"]
|
10
|
+
|
11
|
+
spec.summary = 'MTN Open API Ruby Gem'
|
12
|
+
spec.description = 'A Ruby gem for interacting with MTN Open API.'
|
13
|
+
|
14
|
+
spec.homepage = "https://github.com/koodeyo/mtn_open_api"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/koodeyo/mtn_open_api.git"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/koodeyo/mtn_open_api/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(File.expand_path(f) == __FILE__) ||
|
26
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git appveyor Gemfile])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
spec.bindir = "exe"
|
31
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
32
|
+
spec.require_paths = ["lib"]
|
33
|
+
|
34
|
+
spec.add_dependency "yaml"
|
35
|
+
spec.add_dependency "json"
|
36
|
+
spec.add_dependency 'json-schema'
|
37
|
+
|
38
|
+
spec.add_development_dependency "rspec", "~> 3.2"
|
39
|
+
spec.add_development_dependency 'activesupport', '~> 6.0'
|
40
|
+
end
|
41
|
+
|
42
|
+
|
data/readme-template
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# Mtn Open Api
|
2
|
+
Explore seamless integration with MTN Open API using this Ruby gem. For in-depth information, refer to the official documentation at [https://momodeveloper.mtn.com/](https://momodeveloper.mtn.com/).
|
3
|
+
|
4
|
+
## Key Features
|
5
|
+
- **Full API Availability:** Access a comprehensive set of API endpoints, providing developers with a versatile toolkit to address a wide range of scenarios. The gem's commitment to inclusivity ensures that all necessary API endpoints are readily available for integration.
|
6
|
+
|
7
|
+
- **Robust Testing:** Every endpoint is rigorously tested to ensure reliability and stability. By adhering to best practices in testing, the `Mtn Open Api` gem delivers a dependable solution that developers can trust for their projects.
|
8
|
+
|
9
|
+
- **Intuitive Conversion:** Experience a seamless transition from JSON responses to structured `OpenStruct` objects, providing a more Ruby-like interface for effortless interaction.
|
10
|
+
|
11
|
+
- **Comprehensive Documentation:** All methods are thoroughly documented, ensuring that developers have access to clear and concise information on the functionality and usage of each feature. This commitment to documentation enhances the gem's usability and simplifies the integration process.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Install the gem and add to the application's Gemfile by executing:
|
16
|
+
|
17
|
+
$ bundle add mtn_open_api
|
18
|
+
|
19
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
20
|
+
|
21
|
+
$ gem install mtn_open_api
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/koodeyo/mtn_open_api.
|