-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Merchant Report (#54)
* Add new API method, Bambora::V1::Reports::Merchants.get_all * Add new API method, Bambora::V1::Reports::Merchants.get * Add custom error classes * Raise error for unauthenticated request * Raise error for invalid request * Raise an error for .get when passing the wrong argument
- Loading branch information
1 parent
c62fa42
commit 114f49d
Showing
4 changed files
with
483 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bambora | ||
# All library-specific errors inherit from this error class. This helps make it easy for integrations to rescue | ||
# specifically from errors raised by this library. | ||
class Error < StandardError | ||
attr_reader :payload | ||
|
||
def initialize(message = nil, payload = {}) | ||
@message = message | ||
@payload = payload | ||
|
||
super( | ||
<<~ERROR | ||
#{@message} | ||
#{JSON.pretty_generate(@payload).gsub("\n", "\n ")} | ||
ERROR | ||
) | ||
end | ||
end | ||
|
||
# An error returned when the API returns an authentication failure. | ||
class InvalidAuthenticationError < Error | ||
def initialize(payload = {}) | ||
super(nil, payload) | ||
end | ||
end | ||
|
||
# An error returned when the API returns an invalid request. | ||
class InvalidRequestError < Error | ||
def initialize(payload = {}) | ||
super(nil, payload) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bambora | ||
module V1 | ||
module Reports | ||
class Merchants | ||
def self.get(merchant_id, options = {}) | ||
credentials = options[:credentials] | ||
|
||
raise ArgumentError, "#{self}.get takes an integer argument" unless merchant_id.is_a?(Integer) | ||
|
||
response = | ||
Bambora::Rest::JSONClient | ||
.new(base_url: 'https://api.na.bambora.com', merchant_id: credentials.merchant_id) | ||
.get(path: "/v1/reports/merchants/#{merchant_id}", api_key: credentials.reporting_passcode) | ||
|
||
raise InvalidAuthenticationError, response if response[:message] == 'Authentication failed' | ||
|
||
raise InvalidRequestError, response if response == { Message: 'The request is invalid.' } | ||
|
||
response | ||
end | ||
|
||
def self.get_all(options = {}) | ||
credentials = options[:credentials] | ||
|
||
response = | ||
Bambora::Rest::JSONClient | ||
.new(base_url: 'https://api.na.bambora.com', merchant_id: credentials.merchant_id) | ||
.get(path: '/v1/reports/merchants', api_key: credentials.reporting_passcode) | ||
|
||
raise InvalidAuthenticationError, response if response[:message] == 'Authentication failed' | ||
|
||
response | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.