Class: Tide::API::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/tide/api/client.rb

Overview

Main interface to Tide API

Constant Summary collapse

BASE_PATH =

Base path to Tide API version 1

'https://api.tide.co/tide-backend/rest/api/v1'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(mapper: Mapper.new, http_client: HTTPClient.new) ⇒ Client

Instantiates an API client

Parameters:

  • mapper (Mapper) (defaults to: Mapper.new)

    Converts JSON responses into concrete instances of Tide API concepts

  • http_client (HTTPClient) (defaults to: HTTPClient.new)

    Performs HTTP requests



21
22
23
24
# File 'lib/tide/api/client.rb', line 21

def initialize(mapper: Mapper.new, http_client: HTTPClient.new)
  @mapper = mapper
  @http_client = http_client
end

Instance Method Details

#fetch_accounts(company_id) ⇒ Error|Array<Transaction>

Retrieves all accounts of a given company

Parameters:

  • company_id (Integer)

    Tide’s internal ID of the company

Returns:



56
57
58
59
# 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_companiesError|Array<Company>

Retrieves all companies of the authenticated user

Returns:



45
46
47
48
# 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) ⇒ Object

Exchanges an auth nonce for OAuth2 access and refresh tokens.

Parameters:

  • auth_grant_code (String)

    An authentication nonce provided by the OAuth2 redirect callback.



30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Error|Array<Transaction>

Retrieves all transactions of a given account

Parameters:

  • account_id (Integer)

    Tide’s internal ID of the account

Returns:



67
68
69
70
# File 'lib/tide/api/client.rb', line 67

def fetch_transactions()
  response = http_client.get("#{BASE_PATH}/external/accounts/#{}/transactions")
  response.error? && build_error_from(response) || build_transactions_from(response)
end