Skip to content

Commit 022d9d4

Browse files
committed
Add validations
1 parent b9cdb07 commit 022d9d4

14 files changed

Lines changed: 165 additions & 37 deletions

File tree

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
momoapi-ruby (0.1.0)
4+
momoapi-ruby (0.1.1)
55

66
GEM
77
remote: https://rubygems.org/

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ require 'momoapi-ruby'
8383

8484
collection = Momoapi::Collection.new
8585
collection.request_to_pay(
86-
mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
86+
phone_number="256772123456", amount=600, external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
8787
```
8888
An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.
8989

@@ -123,7 +123,7 @@ require 'momoapi-ruby'
123123

124124
disbursement = Momoapi::Disbursement.new
125125
disbursement.transfer(
126-
mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
126+
phone_number="256772123456", amount=600, external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
127127
```
128128
An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.
129129

@@ -163,7 +163,7 @@ require 'momoapi-ruby'
163163

164164
remittance = Momoapi::Remittance.new
165165
remittance.transfer(
166-
mobile="256772123456", amount="600", external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
166+
phone_number="256772123456", amount=600, external_id="123456789", payee_note="dd", payer_message="dd", currency="EUR")
167167
```
168168
An extra argument, `callback_url`, can be passed to this method, denoting the URL to the server where the callback should be sent.
169169

lib/momoapi-ruby/cli.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def create_sandbox_user
3232
end
3333

3434
unless response.status == 201
35-
raise Error::APIError.new(response.body, response.status)
35+
raise Momoapi::Error.new(response.body, response.status)
3636
end
3737

3838
generate_api_key
@@ -46,7 +46,7 @@ def generate_api_key
4646
end
4747

4848
unless response.status == 201
49-
raise Error::APIError.new(response.body, response.status)
49+
raise Momoapi::Error.new(response.body, response.status)
5050
end
5151

5252
key = JSON.parse(response.body)

lib/momoapi-ruby/client.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
module Momoapi
1313
class Client
1414
def send_request(method, path, headers, body = {})
15-
auth_token = get_auth_token['access_token']
16-
conn = Faraday.new(url: Momoapi.config.base_url)
17-
conn.headers = headers
18-
conn.authorization(:Bearer, auth_token)
15+
begin
16+
auth_token = get_auth_token['access_token']
17+
conn = Faraday.new(url: Momoapi.config.base_url)
18+
conn.headers = headers
19+
conn.authorization(:Bearer, auth_token)
1920

20-
case method
21-
when 'get'
22-
response = conn.get(path)
23-
when 'post'
24-
response = conn.post(path, body.to_json)
21+
case method
22+
when 'get'
23+
response = conn.get(path)
24+
when 'post'
25+
response = conn.post(path, body.to_json)
26+
end
27+
rescue ArgumentError
28+
raise "Missing configuration key(s) for #{@product.capitalize}s"
2529
end
2630
interpret_response(response)
2731
end
@@ -35,11 +39,11 @@ def interpret_response(resp)
3539
unless resp.status >= 200 && resp.status < 300
3640
handle_error(response[:body], response[:code])
3741
end
38-
response
42+
body
3943
end
4044

4145
def handle_error(response_body, response_code)
42-
raise Error::APIError.new(response_body, response_code)
46+
raise Momoapi::Error.new(response_body, response_code)
4347
end
4448

4549
# Create an access token which can then be used to
@@ -51,8 +55,8 @@ def get_auth_token(path, subscription_key)
5155
url = Momoapi.config.base_url
5256
conn = Faraday.new(url: url)
5357
conn.headers = headers
54-
product = path.split('/')[0]
55-
get_credentials(product)
58+
@product = path.split('/')[0]
59+
get_credentials(@product)
5660
conn.basic_auth(@username, @password)
5761
response = conn.post(path)
5862
begin

lib/momoapi-ruby/collection.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
require 'momoapi-ruby/config'
88
require 'momoapi-ruby/client'
9+
require 'momoapi-ruby/validate'
910

1011
module Momoapi
1112
class Collection < Client
@@ -34,6 +35,7 @@ def get_transaction_status(transaction_id)
3435
def request_to_pay(phone_number, amount, external_id,
3536
payee_note = '', payer_message = '',
3637
currency = 'EUR', callback_url = '')
38+
Momoapi::Validate.new.validate(phone_number, amount, currency)
3739
uuid = SecureRandom.uuid
3840
headers = {
3941
"X-Target-Environment": Momoapi.config.environment || 'sandbox',

lib/momoapi-ruby/disbursement.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def get_transaction_status(transaction_id)
2929
def transfer(phone_number, amount, external_id,
3030
payee_note = '', payer_message = '',
3131
currency = 'EUR', callback_url = '')
32+
Momoapi::Validate.new.validate(phone_number, amount, currency)
3233
uuid = SecureRandom.uuid
3334
headers = {
3435
"X-Target-Environment": Momoapi.config.environment || 'sandbox',

lib/momoapi-ruby/errors.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22

33
# Error handling for unsuccessful responses from the MTN Momo API
44

5-
module Error
6-
class APIError < StandardError
5+
module Momoapi
6+
class Error < StandardError
77
def initialize(message, code)
88
@code = code
99
super("Error code #{code} #{message}")
1010
end
1111
end
12+
13+
class ValidationError < StandardError
14+
def initialize(msg = message)
15+
super(msg)
16+
end
17+
end
1218
end

lib/momoapi-ruby/remittance.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ def get_transaction_status(transaction_id)
2929
def transfer(phone_number, amount, external_id,
3030
payee_note = '', payer_message = '',
3131
currency = 'EUR', callback_url = '')
32+
Momoapi::Validate.new.validate(phone_number, amount, currency)
3233
uuid = SecureRandom.uuid
3334
headers = {
3435
"X-Target-Environment": Momoapi.config.environment || 'sandbox',

lib/momoapi-ruby/validate.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# Validations for parameters passed into client methods
4+
5+
require 'momoapi-ruby/errors'
6+
7+
module Momoapi
8+
class Validate
9+
def validate(phone_number, amount, currency)
10+
validate_string?(phone_number, 'Phone number')
11+
validate_numeric?(amount, 'Amount')
12+
validate_string?(currency, 'Currency')
13+
end
14+
15+
def validate_numeric?(num, field)
16+
return true if num.is_a? Numeric
17+
18+
raise Momoapi::ValidationError, "#{field} should be a number"
19+
end
20+
21+
def validate_string?(str, field)
22+
return true if str.is_a? String
23+
24+
raise Momoapi::ValidationError, "#{field} should be a string"
25+
end
26+
end
27+
end

lib/momoapi-ruby/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Momoapi
4-
VERSION = '0.1.0'
4+
VERSION = '0.1.1'
55
end

0 commit comments

Comments
 (0)