module LogCabin::Modules::Plaid

Plaid

Constants

PLAID_DOMAIN

Public Instance Methods

raw_transactions() click to toggle source
# File lib/burglar/modules/plaid.rb, line 13
def raw_transactions # rubocop:disable Metrics/MethodLength
  @raw_transactions ||= all_transactions.map do |row|
    amount = format('$%.2f', row.amount)
    name = row.name.downcase
    action = guess_action(name)
    state = row.pending ? :pending : :cleared

    ::Ledger::Entry.new(
      name: name,
      state: state,
      date: row.date,
      actions: [
        { name: action, amount: amount },
        { name: account_name }
      ],
      tags: { 'transaction_id' => row.transaction_id }
    )
  end
end

Private Instance Methods

access_token() click to toggle source
# File lib/burglar/modules/plaid.rb, line 104
def access_token
  @access_token ||= creds(PLAID_DOMAIN, account_name)
end
account() click to toggle source
# File lib/burglar/modules/plaid.rb, line 81
def account
  @account ||= accounts.first if accounts.size == 1
  @account ||= accounts.find { |x| x['name'] == account_clean_name }
end
account_clean_name() click to toggle source
# File lib/burglar/modules/plaid.rb, line 90
def account_clean_name
  @account_clean_name ||= @options[:name] || raise(
    'Account name needed (more than one option) but not provided'
  )
end
account_id() click to toggle source
# File lib/burglar/modules/plaid.rb, line 86
def account_id
  @account_id ||= account['account_id']
end
accounts() click to toggle source
# File lib/burglar/modules/plaid.rb, line 77
def accounts
  @accounts ||= api_client.accounts.get(access_token)['accounts']
end
all_transactions() click to toggle source
# File lib/burglar/modules/plaid.rb, line 54
def all_transactions
  return @all_transactions if @all_transactions
  list, total = get_transactions_page(0)
  while list.length < total
    new, total = get_transactions_page(list.length)
    list += new
  end
  list.reject!(&:pending) unless @options[:pending]
  @all_transactions = list
end
api_client() click to toggle source
# File lib/burglar/modules/plaid.rb, line 35
def api_client
  @api_client ||= ::Plaid::Client.new(
    env: 'development',
    client_id: client_id,
    secret: secret_key
  )
end
begin_date_str() click to toggle source
# File lib/burglar/modules/plaid.rb, line 65
def begin_date_str
  @begin_date_str ||= date_str(begin_date)
end
client_id() click to toggle source
# File lib/burglar/modules/plaid.rb, line 96
def client_id
  @client_id ||= creds(PLAID_DOMAIN, 'client_id')
end
date_str(date) click to toggle source
# File lib/burglar/modules/plaid.rb, line 73
def date_str(date)
  date.strftime('%Y-%m-%d')
end
end_date_str() click to toggle source
# File lib/burglar/modules/plaid.rb, line 69
def end_date_str
  @end_date_str ||= date_str(end_date)
end
get_transactions_page(offset) click to toggle source
# File lib/burglar/modules/plaid.rb, line 43
def get_transactions_page(offset)
  resp = api_client.transactions.get(
    access_token,
    begin_date_str,
    end_date_str,
    account_ids: [account_id],
    offset: offset
  )
  [resp.transactions, resp.total_transactions]
end
secret_key() click to toggle source
# File lib/burglar/modules/plaid.rb, line 100
def secret_key
  @secret_key ||= creds(PLAID_DOMAIN, 'secret_key')
end