From cc0ffcf3879efaa748ee31af9c35cb49d94a47c8 Mon Sep 17 00:00:00 2001 From: David Stosik <816901+davidstosik@users.noreply.github.com> Date: Tue, 15 Oct 2024 15:44:18 +0900 Subject: [PATCH] Increase YNAB max lengths to follow API changes --- CHANGELOG.md | 5 ++++- Gemfile.lock | 2 +- lib/mfynab/ynab_transaction_importer.rb | 19 ++++++++++--------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b714b00..0737ae4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,12 @@ ## (unreleased) +- Increase memo and payee max lengths, following changes in YNAB's API + ([243b60b](243b60b90efe2e70e50156d1a9cc4330f81cb563)) + ## 0.1.4 (2024-08-25) -- Fix `months_to_sync` configuration key not being unsed +- Fix `months_to_sync` configuration key not being used ([889d241](889d241ce5a56672e2fd9dac639fc29b78aea168)) - Log how many transactions were actually imported vs duplicates ([a7d21de](a7d21de7b26319c362d3dda0119de3167042cc9b)) diff --git a/Gemfile.lock b/Gemfile.lock index 286f3d6..c50cdf6 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -108,7 +108,7 @@ GEM websocket-driver (0.7.6) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) - ynab (3.4.0) + ynab (3.6.0) typhoeus (~> 1.0, >= 1.0.1) PLATFORMS diff --git a/lib/mfynab/ynab_transaction_importer.rb b/lib/mfynab/ynab_transaction_importer.rb index 8790957..57ceb3f 100644 --- a/lib/mfynab/ynab_transaction_importer.rb +++ b/lib/mfynab/ynab_transaction_importer.rb @@ -4,6 +4,11 @@ module MFYNAB class YnabTransactionImporter + # See https://github.com/ynab/ynab-sdk-ruby/issues/77 + MEMO_MAX_LENGTH = 500 + PAYEE_MAX_LENGTH = 200 + IMPORT_ID_MAX_LENGTH = 36 + def initialize(api_key, budget_name, account_mappings, logger:) @api_key = api_key @budget_name = budget_name @@ -42,7 +47,7 @@ def convert_mf_transaction(row, account) { account_id: account.id, amount: row["amount"] * 1_000, - payee_name: row["content"][0, 100], + payee_name: row["content"][0, PAYEE_MAX_LENGTH], date: Date.strptime(row["date"], "%Y/%m/%d").strftime("%Y-%m-%d"), cleared: "cleared", memo: generate_memo_for(row), @@ -65,9 +70,7 @@ def generate_memo_for(row) memo_parts .delete_if { _1.nil? || _1.empty? } .join(" - ") - .slice(0, 200) # YNAB's API currently limits memo to 200 characters, - # even though YNAB itself allows longer memos. See: - # https://github.com/ynab/ynab-sdk-ruby/issues/77 + .slice(0, MEMO_MAX_LENGTH) end # ⚠️ Be very careful when changing this method! @@ -91,16 +94,14 @@ def generate_import_id_for(row) # but changing it now would require a lot of work in preventing import # duplicates due to inconsistent import_id. prefix = "MFBY:v1:" - - max_length = 36 # YNAB API limit - id_max_length = 28 # this leaves 8 characters for the prefix + digest_max_length = IMPORT_ID_MAX_LENGTH - prefix.length id = row["id"] # Only hash if the ID would exceed YNAB's limit. # This improves backwards compatibility with old import_ids. - if prefix.length + id.length > max_length - id = Digest::SHA256.hexdigest(id)[0, id_max_length] + if prefix.length + id.length > IMPORT_ID_MAX_LENGTH + id = Digest::SHA256.hexdigest(id)[0, digest_max_length] end prefix + id