Module: FindSubscriptions::Schemas

Defined in:
lib/schemas/generic.rb,
lib/schemas/navy_federal.rb,
lib/schemas/american_express.rb

Overview

CSV schema definitions for supported banks and card issuers.

Constant Summary collapse

DATE_FORMAT =
'%Y-%m-%d'
lambda { |row, _amount|
  type = row.fetch('Credit Debit Indicator').to_s.strip.downcase
  case type
  when 'debit' then :debit
  when 'credit' then :credit
  else raise ArgumentError, "Unknown Credit Debit Indicator: #{row['Credit Debit Indicator']}"
  end
}.freeze
lambda { |row, signed_amount|
  Transaction.new(
    date: Date.strptime(row.fetch('Transaction Date'), '%m/%d/%Y'),
    payee: row.fetch('Description').to_s.strip,
    amount: signed_amount,
    raw: row
  )
}.freeze
AMEX_DIRECTION =
lambda { |_row, amount|
  amount.negative? ? :credit : :debit
}.freeze
AMEX_MAPPING =
lambda { |row, signed_amount|
  Transaction.new(
    date: Date.strptime(row['Date'], '%m/%d/%Y'),
    payee: row.fetch('Description').to_s.strip,
    amount: signed_amount,
    raw: row
  )
}.freeze

Class Method Summary collapse

Class Method Details

.american_expressObject



22
23
24
25
26
27
28
29
# File 'lib/schemas/american_express.rb', line 22

def self.american_express
  CsvSchema.new(
    required_headers: ['Date', 'Description', 'Amount', 'Card Member'],
    amount_key: 'Amount',
    direction: AMEX_DIRECTION,
    mapping: AMEX_MAPPING
  )
end

.genericObject



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/schemas/generic.rb', line 13

def self.generic
  CsvSchema.new(
    required_headers: %w[Date Description Amount],
    amount_key: 'Amount',
    direction: lambda { |_row, amount|
      amount.negative? ? :debit : :credit
    },
    mapping: lambda do |row, signed_amount|
      map_row(row, signed_amount)
    end
  )
end

.map_row(row, signed_amount) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/schemas/generic.rb', line 26

def self.map_row(row, signed_amount)
  Transaction.new(
    date: Date.strptime(row.fetch('Date'), DATE_FORMAT),
    payee: row.fetch('Description').to_s.strip,
    amount: signed_amount,
    raw: row
  )
end


27
28
29
30
31
32
33
34
# File 'lib/schemas/navy_federal.rb', line 27

def self.navy_federal
  CsvSchema.new(
    required_headers: ['Transaction Date', 'Description', 'Amount', 'Credit Debit Indicator'],
    amount_key: 'Amount',
    direction: NAVY_FEDERAL_DIRECTION,
    mapping: NAVY_FEDERAL_MAPPING
  )
end