Class: MFYNAB::YnabTransactionImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/mfynab/ynab_transaction_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key, budget_name, account_mappings, logger:) ⇒ YnabTransactionImporter

Returns a new instance of YnabTransactionImporter.



7
8
9
10
11
12
# File 'lib/mfynab/ynab_transaction_importer.rb', line 7

def initialize(api_key, budget_name, , logger:)
  @api_key = api_key
  @budget_name = budget_name
  @account_mappings = 
  @logger = logger
end

Instance Method Details

#run(mf_transactions) ⇒ Object

rubocop:disable Metrics/AbcSize



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mfynab/ynab_transaction_importer.rb', line 14

def run(mf_transactions) # rubocop:disable Metrics/AbcSize
  mf_transactions.map do |, |
     = ()
    next unless 

    transactions = .map do |row|
      convert_mf_transaction(row, )
    end

    begin
      logger.info("Importing #{transactions.size} transactions for #{.name}")
      result = ynab_transactions_api.create_transaction(budget.id, transactions: transactions)
      logger.info(
        "Imported #{result.data.transactions.size} transactions " \
        "for #{.name} " \
        "(#{result.data.duplicate_import_ids.size} duplicates)",
      )
    rescue StandardError => e
      logger.error("Error importing transactions for #{budget.name}. #{e} : #{e.detail}")
    end
  end
end