Class: FindSubscriptions::Detectors::KnownPayees

Inherits:
Object
  • Object
show all
Defined in:
lib/detectors/known_payees.rb

Overview

Matches transactions to known subscription payees via configurable name matchers.

Instance Method Summary collapse

Constructor Details

#initialize(known_payees:) ⇒ KnownPayees

Returns a new instance of KnownPayees.



10
11
12
# File 'lib/detectors/known_payees.rb', line 10

def initialize(known_payees:)
  @known_payees = known_payees # hash: canonical_name => [matchers]
end

Instance Method Details

#detect(transactions) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/detectors/known_payees.rb', line 14

def detect(transactions)
  # returns hash: canonical_name => earliest_transaction_date
  found = {}

  transactions.each do |tx|
    canonical = match_payee(tx.payee)
    next unless canonical

    found[canonical] = tx.date if !found.key?(canonical) || tx.date < found[canonical]
  end

  found
end