class Tide::API::Client

Main interface to Tide API

Constants

BASE_PATH

Base path to Tide API version 1

Attributes

http_client[R]

@api private

mapper[R]

@api private

Public Class Methods

new(mapper: Mapper.new, http_client: HTTPClient.new) click to toggle source

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

fetch_accounts(company_id) click to toggle source

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
fetch_companies() click to toggle source

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
fetch_tokens(auth_grant_code) click to toggle source

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
fetch_transactions(account_id) click to toggle source

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

build_accounts_from(response) click to toggle source

@api private

# File lib/tide/api/client.rb, line 93
def build_accounts_from(response)
  mapper.map(response.payload, Account)
end
build_companies_from(response) click to toggle source

@api private

# File lib/tide/api/client.rb, line 83
def build_companies_from(response)
  mapper.map(response.payload, Company)
end
build_error_from(response) click to toggle source

@api private

# File lib/tide/api/client.rb, line 98
def build_error_from(response)
  mapper.map_one(response.payload, Error)
end
build_tokens_from(response) click to toggle source

@api private

# File lib/tide/api/client.rb, line 78
def build_tokens_from(response)
  mapper.map_one(response.payload, Tokens)
end
build_transactions_from(response) click to toggle source

@api private

# File lib/tide/api/client.rb, line 88
def build_transactions_from(response)
  mapper.map(response.payload, Transaction)
end