Class: CorePro::Resource
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- CorePro::Resource
- Extended by:
- HTTP::RestClient::DSL
- Defined in:
- lib/core_pro.rb
Overview
Base endpoint resources class
Direct Known Subclasses
Account, Customer, ExternalAccount, Program, Transaction, Transfer
Class Method Summary collapse
-
.all(filters = [], params = {}) ⇒ Array
Resource collection finder, uses the default limit.
-
.create(params = {}) ⇒ Object
Resource creation helper.
-
.error_response?(response, parsed_response) ⇒ TrueClass
Validate error response.
-
.extract_error(response, parsed_response) ⇒ String
Extracts the error message from the response.
-
.find(*id_or_ids) ⇒ Object
Resource finder.
-
.objectify(payload) ⇒ Object
Resource constructor wrapper.
-
.update(params = {}) ⇒ Object
Resource update helper.
Instance Method Summary collapse
-
#reload ⇒ CorePro::Resource
Helper to reload a resource.
-
#resource_id ⇒ String
Returns the ID name based on the resource type.
Class Method Details
.all(filters = [], params = {}) ⇒ Array
Resource collection finder, uses the default limit
27 28 29 |
# File 'lib/core_pro.rb', line 27 def self.all(filters = [], params = {}) objectify(request(:get, uri(:list, *filters), params: params)) end |
.create(params = {}) ⇒ Object
Resource creation helper
44 45 46 |
# File 'lib/core_pro.rb', line 44 def self.create(params = {}) objectify(request(:post, uri(:create), json: params)) end |
.error_response?(response, parsed_response) ⇒ TrueClass
Validate error response
Looks at the response code by default.
105 106 107 108 |
# File 'lib/core_pro.rb', line 105 def self.error_response?(response, parsed_response) errors = parsed_response&.fetch('errors', nil) || [] !(200..299).cover?(response.code) || errors.any? end |
.extract_error(response, parsed_response) ⇒ String
Extracts the error message from the response
93 94 95 |
# File 'lib/core_pro.rb', line 93 def self.extract_error(response, parsed_response) parsed_response&.fetch('errors', nil) || super(response, parsed_response) end |
.find(*id_or_ids) ⇒ Object
Resource finder
36 37 38 |
# File 'lib/core_pro.rb', line 36 def self.find(*id_or_ids) objectify(request(:get, uri(:get, *id_or_ids))) end |
.objectify(payload) ⇒ Object
Resource constructor wrapper
61 62 63 64 65 66 67 68 69 |
# File 'lib/core_pro.rb', line 61 def self.objectify(payload) if payload.is_a?(Hash) && payload['data'].is_a?(Array) return payload['data'].map { |data| new(data) } end return new(payload['data']) if payload&.fetch('data', nil).is_a?(Hash) payload end |
.update(params = {}) ⇒ Object
Resource update helper
53 54 55 |
# File 'lib/core_pro.rb', line 53 def self.update(params = {}) objectify(request(:post, uri(:update), json: params)) end |
Instance Method Details
#reload ⇒ CorePro::Resource
Helper to reload a resource
83 84 85 |
# File 'lib/core_pro.rb', line 83 def reload self.class.find(send(resource_id)) end |
#resource_id ⇒ String
Returns the ID name based on the resource type
74 75 76 77 78 |
# File 'lib/core_pro.rb', line 74 def resource_id resource_name = self.class.name.to_s.split('::').last resource_name[0] = resource_name[0].downcase "#{resource_name}Id" end |