Class: ParamsConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/ynap/values/params_converter.rb

Constant Summary collapse

IMPORT_ID_PREFIX =
"GW"
IMPORT_ID_FIXED_SIZE =
IMPORT_ID_PREFIX.length + 10 + 3
MAX_IMPORT_ID_SIZE =
36

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account, plaid_transaction) ⇒ ParamsConverter

Returns a new instance of ParamsConverter.



8
9
10
11
# File 'lib/ynap/values/params_converter.rb', line 8

def initialize(, plaid_transaction)
  @account           = 
  @plaid_transaction = plaid_transaction
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



6
7
8
# File 'lib/ynap/values/params_converter.rb', line 6

def 
  @account
end

#plaid_transactionObject (readonly)

Returns the value of attribute plaid_transaction.



6
7
8
# File 'lib/ynap/values/params_converter.rb', line 6

def plaid_transaction
  @plaid_transaction
end

Instance Method Details

#amountObject



13
14
15
# File 'lib/ynap/values/params_converter.rb', line 13

def amount
  @amount ||= -(BigDecimal(plaid_transaction.amount.to_s) * 100).to_i
end

#dateObject



17
18
19
# File 'lib/ynap/values/params_converter.rb', line 17

def date
  @date ||= Date.parse(plaid_transaction.date)
end

#import_idObject



33
34
35
# File 'lib/ynap/values/params_converter.rb', line 33

def import_id
  [IMPORT_ID_PREFIX, amount, date, sliced_transaction_id].join(":")
end

#payee_nameObject



37
38
39
# File 'lib/ynap/values/params_converter.rb', line 37

def payee_name
  @payee_name ||= plaid_transaction.merchant_name || PayeeParser.new.cleaned_name(plaid_transaction.name)[0, 50]
end

#sliced_transaction_idObject



29
30
31
# File 'lib/ynap/values/params_converter.rb', line 29

def sliced_transaction_id
  @sliced_transaction_id ||= transaction_id[0, transaction_id_length]
end

#to_paramsObject



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ynap/values/params_converter.rb', line 41

def to_params
  {
    account_id: .ynab_id,
    amount: amount * 10,
    cleared: 'cleared',
    date: date,
    import_id: import_id,
    memo: plaid_transaction.name,
    payee_name: payee_name
  }
end

#transaction_idObject



21
22
23
# File 'lib/ynap/values/params_converter.rb', line 21

def transaction_id
  @transaction_id ||= plaid_transaction.pending_transaction_id || plaid_transaction.transaction_id
end

#transaction_id_lengthObject



25
26
27
# File 'lib/ynap/values/params_converter.rb', line 25

def transaction_id_length
  @transaction_id_length ||= MAX_IMPORT_ID_SIZE - IMPORT_ID_FIXED_SIZE - amount.to_s.size
end