Skip to content

Commit

Permalink
Increase YNAB max lengths to follow API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidstosik committed Oct 21, 2024
1 parent d4b73db commit cc0ffcf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 11 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions lib/mfynab/ynab_transaction_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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!
Expand All @@ -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
Expand Down

0 comments on commit cc0ffcf

Please sign in to comment.