Class: Starling::Resources::BaseResource
- Inherits:
-
Object
- Object
- Starling::Resources::BaseResource
- Defined in:
- lib/starling/resources/base_resource.rb
Overview
A basic implementation of a resource representing a response from the Starling Bank API
Direct Known Subclasses
AccountBalanceResource, AccountResource, AddressResource, AddressesResource, CardResource, ContactAccountResource, ContactResource, CustomerResource, DirectDebitMandateResource, DirectDebitTransactionResource, InboundFasterPaymentsTransactionResource, MastercardTransactionResource, MeResource, MerchantLocationResource, MerchantResource, OutboundFasterPaymentsTransactionResource, PaymentResource, TransactionResource
Instance Method Summary collapse
-
#initialize(response: nil, parsed_data: nil) ⇒ BaseResource
constructor
A resource can be instantiated with either a Faraday::Response (including a #body), or with a Hash pre-parsed from JSON.
Constructor Details
#initialize(response: nil, parsed_data: nil) ⇒ BaseResource
A resource can be instantiated with either a Faraday::Response (including a #body), or with a Hash pre-parsed from JSON.
An alternative possible approach to our resources would be to parse out the attributes we care about at initialisation, and then just add ‘attr_reader`s, rather than looking into the parsed JSON hash. The current solution is probably preferable, since it defers delving into the hash until a parameter is actually wanted, and keeps instance variables to a minimum.
24 25 26 27 28 29 30 31 32 |
# File 'lib/starling/resources/base_resource.rb', line 24 def initialize(response: nil, parsed_data: nil) unless response || parsed_data raise ArgumentError, 'Either response or parsed_data must be provided to ' \ 'instantiate a resource' end @response = response @parsed_data = parsed_data || JSON.parse(response.body) end |