Class: F2ynab::YNAB::Client
- Inherits:
-
Object
- Object
- F2ynab::YNAB::Client
- Defined in:
- lib/f2ynab/ynab/client.rb
Instance Method Summary collapse
- #accounts ⇒ Object
- #budgets ⇒ Object
- #category(category_id) ⇒ Object
- #create_transaction(id: nil, payee_id: nil, payee_name: nil, amount: nil, cleared: nil, date: nil, memo: nil, flag: nil) ⇒ Object
- #create_transactions(transactions) ⇒ Object
-
#initialize(access_token, budget_id, account_id) ⇒ Client
constructor
A new instance of Client.
- #selected_account_id ⇒ Object
- #selected_budget_id ⇒ Object
- #transactions(since_date: nil, account_id: nil) ⇒ Object
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, account_id) @access_token = access_token @budget_id = budget_id @account_id = account_id end |
Instance Method Details
#accounts ⇒ Object
14 15 16 |
# File 'lib/f2ynab/ynab/client.rb', line 14 def accounts @_accounts ||= client.accounts.get_accounts(selected_budget_id).data.accounts end |
#budgets ⇒ Object
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: selected_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. != 'Conflict' { error: e. } 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. != 'Conflict' raise end |
#selected_account_id ⇒ Object
68 69 70 |
# File 'lib/f2ynab/ynab/client.rb', line 68 def selected_account_id @account_id || accounts.reject(&:closed).select { |a| a.type == 'checking' }.first.id end |
#selected_budget_id ⇒ Object
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 account_id.present? client.transactions.get_transactions_by_account(selected_budget_id, account_id, since_date: since_date).data.transactions else client.transactions.get_transactions(selected_budget_id, since_date: since_date).data.transactions end end |