Class: Starling::Resources::BaseResource

Inherits:
Object
  • Object
show all
Defined in:
lib/starling/resources/base_resource.rb

Overview

A basic implementation of a resource representing a response from the Starling Bank API

Instance Method Summary collapse

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.

Parameters:

  • response (Faraday::Response) (defaults to: nil)

    The complete HTTP response, returned by Starling::Request#make_request

  • parsed_data (Hash) (defaults to: nil)

    The pre-parsed data for the resource, useful for building resources from list resources we've already parsed


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