class Tide::API::Client
Constants
Attributes
@api private
@api private
Public Class Methods
Instantiates an API
client
@param [Mapper] mapper Converts JSON responses into concrete instances of Tide
API
concepts @param [HTTPClient] http_client
Performs HTTP requests
# File lib/tide/api/client.rb, line 21 def initialize(mapper: Mapper.new, http_client: HTTPClient.new) @mapper = mapper @http_client = http_client end
Public Instance Methods
Retrieves all accounts of a given company
@param [Integer] company_id Tide's internal ID of the company
@return [Error|Array<Transaction>]
# File lib/tide/api/client.rb, line 56 def fetch_accounts(company_id) response = http_client.get("#{BASE_PATH}/external/companies/#{company_id}/accounts") response.error? && build_error_from(response) || build_accounts_from(response) end
Retrieves all companies of the authenticated user
@return [Error|Array<Company>] A list of companies
# File lib/tide/api/client.rb, line 45 def fetch_companies response = http_client.get("#{BASE_PATH}/external/companies") response.error? && build_error_from(response) || build_companies_from(response) end
Exchanges an auth nonce for OAuth2 access and refresh tokens.
@param [String] auth_grant_code An authentication nonce provided by the OAuth2 redirect callback.
# File lib/tide/api/client.rb, line 30 def fetch_tokens(auth_grant_code) response = http_client.get("#{BASE_PATH}/oauth2/tokens?code=#{auth_grant_code}") return build_error_from(response) if response.error? build_tokens_from(response).tap do |tokens| http_client.refresh_token = tokens.refresh_token http_client.access_token = tokens.access_token end end
Retrieves all transactions of a given account
@param [Integer] account_id Tide's internal ID of the account
@return [Error|Array<Transaction>]
# File lib/tide/api/client.rb, line 67 def fetch_transactions(account_id) response = http_client.get("#{BASE_PATH}/external/accounts/#{account_id}/transactions") response.error? && build_error_from(response) || build_transactions_from(response) end
Private Instance Methods
@api private
# File lib/tide/api/client.rb, line 93 def build_accounts_from(response) mapper.map(response.payload, Account) end
@api private
# File lib/tide/api/client.rb, line 83 def build_companies_from(response) mapper.map(response.payload, Company) end
@api private
# File lib/tide/api/client.rb, line 98 def build_error_from(response) mapper.map_one(response.payload, Error) end
@api private
# File lib/tide/api/client.rb, line 78 def build_tokens_from(response) mapper.map_one(response.payload, Tokens) end
@api private
# File lib/tide/api/client.rb, line 88 def build_transactions_from(response) mapper.map(response.payload, Transaction) end