Class: F2ynab::YNAB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/f2ynab/ynab/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(access_token, budget_id, account_id) ⇒ Client

Returns a new instance of Client.



4
5
6
7
8
# File 'lib/f2ynab/ynab/client.rb', line 4

def initialize(access_token, budget_id, )
  @access_token = access_token
  @budget_id = budget_id
  @account_id = 
end

Instance Method Details

#accountsObject



14
15
16
# File 'lib/f2ynab/ynab/client.rb', line 14

def accounts
  @_accounts ||= client.accounts.get_accounts(selected_budget_id).data.accounts
end

#budgetsObject



10
11
12
# File 'lib/f2ynab/ynab/client.rb', line 10

def budgets
  @_budgets ||= client.budgets.get_budgets.data.budgets
end

#category(category_id) ⇒ Object



18
19
20
# File 'lib/f2ynab/ynab/client.rb', line 18

def category(category_id)
  client.categories.get_category_by_id(selected_budget_id, category_id).data.category
end

#create_transaction(id: nil, payee_id: nil, payee_name: nil, amount: nil, cleared: nil, date: nil, memo: nil, flag: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/f2ynab/ynab/client.rb', line 30

def create_transaction(id: nil, payee_id: nil, payee_name: nil, amount: nil, cleared: nil, date: nil, memo: nil, flag: nil)
  client.transactions.create_transaction(
    selected_budget_id,
    transaction: {
      account_id: ,
      date: date.to_s,
      amount: amount,
      payee_id: payee_id,
      payee_name: payee_name,
      cleared: cleared ? "Cleared" : 'Uncleared',
      memo: memo,
      flag_color: flag,
      import_id: id,
    },
  ).data.transaction
rescue StandardError => e
  Rails.logger.error('YNAB::Client.create_transaction failure')
  Rails.logger.error("YNAB::Client.create_transaction Response: #{e.response_body}")
  Rails.logger.error(e)
  Raven.capture_exception(e) if defined?(Raven) && e.message != 'Conflict'
  { error: e.message }
end

#create_transactions(transactions) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/f2ynab/ynab/client.rb', line 53

def create_transactions(transactions)
  client.transactions.bulk_create_transactions(selected_budget_id, transactions: transactions).data.bulk
rescue StandardError => e
  Rails.logger.error('YNAB::Client.create_transactions failure')
  Rails.logger.error("YNAB::Client.create_transactions Request Body: #{transactions}")
  Rails.logger.error("YNAB::Client.create_transactions Response: #{e.response_body}")
  Rails.logger.error(e)
  Raven.capture_exception(e) if defined?(Raven) && e.message != 'Conflict'
  raise
end

#selected_account_idObject



68
69
70
# File 'lib/f2ynab/ynab/client.rb', line 68

def 
  @account_id || accounts.reject(&:closed).select { |a| a.type == 'checking' }.first.id
end

#selected_budget_idObject



64
65
66
# File 'lib/f2ynab/ynab/client.rb', line 64

def selected_budget_id
  @budget_id || budgets.first.id
end

#transactions(since_date: nil, account_id: nil) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/f2ynab/ynab/client.rb', line 22

def transactions(since_date: nil, account_id: nil)
  if .present?
    client.transactions.(selected_budget_id, , since_date: since_date).data.transactions
  else
    client.transactions.get_transactions(selected_budget_id, since_date: since_date).data.transactions
  end
end