Sha256: d9d0d600ba2c9a2658145523733688cc3549936ad59bc689f5fbe11bfa44bb65

Contents?: true

Size: 1010 Bytes

Versions: 1

Compression:

Stored size: 1010 Bytes

Contents

module MT940Structured::Parsers
  class Base
    def initialize(bank, transaction_parsers)
      @bank = bank
      @transaction_parsers = transaction_parsers
    end

    def transform(lines)
      bank_statements = Hash.new { |h, k| h[k] = [] }
      result = group_lines_by_tag(lines)
      result.each do |bank_statement_lines|
        bank_statement = BankStatementParser.new(@bank, @transaction_parsers, bank_statement_lines).bank_statement
        bank_statements[bank_statement.bank_account] << bank_statement
      end
      bank_statements
    end

    private
    def group_lines_by_tag(lines)
      result = []
      while !lines.empty? do
        start_index = lines.index { |line| line.match(/^:20:/)}
        end_index = lines.index { |line| line.match(/^:62F:/)}
        if start_index && end_index > start_index
          result << lines[start_index..end_index]
          lines = lines.drop(end_index + 1)
        else
          lines = []
        end
      end
      result
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
zilverline-mt940-2.0 lib/mt940_structured/parsers/base.rb