Class: Ncba::Resource
- Inherits:
-
Object
- Object
- Ncba::Resource
- Defined in:
- lib/ncba/resource.rb
Overview
Resource
Direct Known Subclasses
AccountOpening, CreditDetails, CreditTransfer, MpesaPhoneNumberValidation, TransactionQuery
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #get_request(url:, params: {}, headers: {}) ⇒ Object
- #handle_response(response) ⇒ Object
-
#initialize(client, args = {}) ⇒ Resource
constructor
A new instance of Resource.
- #post_request(url:, body: {}, headers: {}) ⇒ Object
Constructor Details
#initialize(client, args = {}) ⇒ Resource
Returns a new instance of Resource.
8 9 10 11 12 |
# File 'lib/ncba/resource.rb', line 8 def initialize(client, args = {}) @client = client @args = args @headers = { 'ApiKey': client.api_key, 'APIUser': client.api_user } end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
6 7 8 |
# File 'lib/ncba/resource.rb', line 6 def args @args end |
#client ⇒ Object (readonly)
Returns the value of attribute client.
6 7 8 |
# File 'lib/ncba/resource.rb', line 6 def client @client end |
Instance Method Details
#get_request(url:, params: {}, headers: {}) ⇒ Object
14 15 16 |
# File 'lib/ncba/resource.rb', line 14 def get_request(url:, params: {}, headers: {}) handle_response client.connection.get(url, params, @headers) end |
#handle_response(response) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ncba/resource.rb', line 22 def handle_response(response) # case response.status # when 400 # raise Error, "Your request was malformed. #{response.body["errorMessage"]}" # when 401 # raise Error, "You did not supply valid authentication credentials. #{response.body["errorMessage"]}" # when 403 # raise Error, "You are not allowed to perform that action. #{response.body["errorMessage"]}" # when 404 # raise Error, "No results were found for your request. #{response.body["errorMessage"]}" # when 500 # raise Error, "Something wrong happened. #{response.body["errorMessage"]}" # end response end |
#post_request(url:, body: {}, headers: {}) ⇒ Object
18 19 20 |
# File 'lib/ncba/resource.rb', line 18 def post_request(url:, body: {}, headers: {}) handle_response client.connection.post(url, body, @headers) end |