Class: ExtrasDeCont::Rules::RevolutCsv

Inherits:
Rules::Base
  • Object
show all
Defined in:
lib/extras_de_cont/rules/revolut_csv.rb

Overview

Parses Revolut's account-statement CSV export.

Constant Summary collapse

REQUIRED_HEADERS =
["Started Date", "Description", "Amount", "Fee", "Currency", "State"].freeze
INCLUDED_STATES =
%w[COMPLETED REVERTED].freeze

Instance Method Summary collapse

Instance Method Details

#parse(source) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/extras_de_cont/rules/revolut_csv.rb', line 15

def parse(source)
  csv_data = source.respond_to?(:read) ? source.read : source.to_s
  csv_data = csv_data.dup.force_encoding(Encoding::UTF_8).delete_prefix("\uFEFF")
  rows = CSV.parse(csv_data, headers: true, skip_blanks: true)
  validate_headers!(rows.headers)

  rows.filter_map do |row|
    state = row["State"].to_s.strip.upcase
    next unless INCLUDED_STATES.include?(state)

    parse_row(row, state)
  end
end