# frozen_string_literal: true module Bankster class Client include HTTParty attr_reader :credentials def self.configuration Configuration.instance end def self.configure yield Configuration.instance end def initialize(credentials) @credentials = credentials @options = { base_uri: self.class.configuration.api_url, headers: { 'Api-Key': self.class.configuration.api_key, 'Bank-Credentials': encoded_credentials } } end def transactions(account, from, to) response = self.class.get('/transactions', @options.merge(query: { account: account, start: from, end: to })) raise(response.body) unless response.ok? JSON.parse(response.body) end # FIXME : Parameter lists longer # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength def transfer(sending_account, receiving_account, amount_cents, purpose, reference, identification = nil) attributes = @options.merge( body: { sending_account: sending_account.to_h, receiving_account: receiving_account.to_h, purpose: purpose, amount_cents: amount_cents, reference: reference, identification: identification } ) response = self.class.post('/transfers', attributes) raise(response.body) if !response.code.eql?(200) && !response.code.eql?(201) end private def encoded_credentials raise 'No credentials given' unless credentials credentials.encode end end end