Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve errors handling #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,31 @@ PATH
GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
addressable (2.8.1)
public_suffix (>= 2.0.2, < 6.0)
coderay (1.1.2)
coveralls (0.8.23)
json (>= 1.8, < 3)
simplecov (~> 0.16.1)
term-ansicolor (~> 1.3)
thor (>= 0.19.4, < 2.0)
tins (~> 1.6)
crack (0.4.3)
safe_yaml (~> 1.0.0)
crack (0.4.5)
rexml
diff-lcs (1.3)
docile (1.3.2)
faraday (1.0.0)
multipart-post (>= 1.2, < 3)
hashdiff (1.0.0)
hashdiff (1.0.1)
json (2.3.0)
method_source (0.9.2)
multipart-post (2.1.1)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (4.0.3)
public_suffix (5.0.1)
rake (13.0.1)
rexml (3.2.5)
rspec (3.9.0)
rspec-core (~> 3.9.0)
rspec-expectations (~> 3.9.0)
Expand All @@ -44,7 +45,6 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.9.0)
rspec-support (3.9.2)
safe_yaml (1.0.5)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
Expand All @@ -57,10 +57,10 @@ GEM
tins (1.24.1)
sync
vcr (5.1.0)
webmock (2.3.2)
addressable (>= 2.3.6)
webmock (3.18.1)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby
Expand All @@ -73,7 +73,7 @@ DEPENDENCIES
rake (~> 13.0)
rspec (~> 3.0)
vcr (~> 5.1)
webmock (~> 2.1)
webmock (~> 3.18)

BUNDLED WITH
2.0.2
2.1.4
18 changes: 15 additions & 3 deletions lib/momoapi-ruby/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Client
def send_request(method, path, headers, body = {})
begin
auth_token = get_auth_token['access_token']
conn = Faraday.new(url: Momoapi.config.base_url)
conn = faraday_with_block(url: Momoapi.config.base_url)
conn.headers = headers
conn.authorization(:Bearer, auth_token)

Expand All @@ -24,7 +24,7 @@ def send_request(method, path, headers, body = {})
when 'post'
response = conn.post(path, body.to_json)
end
rescue ArgumentError
rescue ArgumentError => e
raise "Missing configuration key(s) for #{@product.capitalize}s"
end
interpret_response(response)
Expand Down Expand Up @@ -53,7 +53,7 @@ def get_auth_token(path, subscription_key)
"Ocp-Apim-Subscription-Key": subscription_key
}
url = Momoapi.config.base_url
conn = Faraday.new(url: url)
conn = faraday_with_block(url: url)
conn.headers = headers
@product = path.split('/')[0]
get_credentials(@product)
Expand Down Expand Up @@ -103,5 +103,17 @@ def get_transaction_status(path, subscription_key)
def is_user_active(path, subscription_key)
prepare_get_request(path, subscription_key)
end

private

def faraday_with_block(options)
Faraday.new(options)
block = Momoapi.config.faraday_block
if block
Faraday.new(options, &block)
else
Faraday.new(options)
end
end
end
end
8 changes: 8 additions & 0 deletions lib/momoapi-ruby/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ def request_to_pay(phone_number, amount, external_id,
path = '/collection/v1_0/requesttopay'
send_request('post', path, headers, body)
{ transaction_reference: uuid }
rescue Exception => error
if uuid
class << error
attr_accessor :transaction_reference
end
error.transaction_reference = uuid
end
raise error
end

def is_user_active(phone_number)
Expand Down
3 changes: 2 additions & 1 deletion lib/momoapi-ruby/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Config
:collection_user_id, :collection_api_secret,
:disbursement_primary_key, :disbursement_user_id,
:disbursement_api_secret, :remittance_primary_key,
:remittance_user_id, :remittance_api_secret
:remittance_user_id, :remittance_api_secret, :faraday_block

def initialize
@environment = nil
Expand All @@ -26,6 +26,7 @@ def initialize
@remittance_primary_key = nil
@remittance_user_id = nil
@remittance_api_secret = nil
@faraday_block = nil # pass Proxy
end

def base_url
Expand Down
8 changes: 8 additions & 0 deletions lib/momoapi-ruby/remittance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def transfer(phone_number, amount, external_id,
path = '/remittance/v1_0/transfer'
send_request('post', path, headers, body)
{ transaction_reference: uuid }
rescue Exception => error
if uuid
class << error
attr_accessor :transaction_reference
end
error.transaction_reference = uuid
end
raise error
end

def is_user_active(phone_number)
Expand Down
2 changes: 1 addition & 1 deletion momoapi-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.0'
spec.add_development_dependency 'vcr', '~> 5.1'
spec.add_development_dependency 'webmock', '~> 2.1'
spec.add_development_dependency 'webmock', '~> 3.18'
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading