class Ovchipkaart::Parser

Attributes

additions[R]
check_ins[R]
forgotten_check_outs[R]
journeys[R]
others[R]
products[R]

Public Class Methods

new() click to toggle source
# File lib/ovchipkaart/parser.rb, line 8
def initialize
  @journeys   = []
  @check_ins  = []
  @additions  = []
  @others     = []
  @products   = []
  @forgotten_check_outs = []
end
process_transactions() click to toggle source
# File lib/ovchipkaart/parser.rb, line 17
def self.process_transactions
  new.sort_csv_file
end

Public Instance Methods

csv_file() click to toggle source
# File lib/ovchipkaart/parser.rb, line 50
def csv_file
  File.read last_downloaded_file
end
last_downloaded_file() click to toggle source
# File lib/ovchipkaart/parser.rb, line 54
def last_downloaded_file
  Dir['tmp/downloads/*'].sort_by{ |f| File.basename(f) }.first
end
parse_csv_file() click to toggle source
# File lib/ovchipkaart/parser.rb, line 46
def parse_csv_file
  CSV.new(csv_file, headers: true, header_converters: :symbol, col_sep: ';')
end
sort_csv_file() click to toggle source
# File lib/ovchipkaart/parser.rb, line 21
def sort_csv_file
  transactions_enum = transactions.each
  begin
    transaction = transactions_enum.next
    case transaction[:transactie]
    when "Reis"
      journeys << transaction
    when "Check-in"
      if transactions_enum.peek[:transactie] == "Check-in"
        forgotten_check_outs << transaction
      else
        check_ins << transaction
      end
    when "Saldo opgeladen"
      additions << transaction
    when "Product op kaart gezet"
      products << transaction
    else
      others << transaction
    end
  rescue StopIteration
    break
  end while true
end

Private Instance Methods

transactions() click to toggle source
# File lib/ovchipkaart/parser.rb, line 60
def transactions
  @transactions ||= parse_csv_file.to_a.map { |row| row.to_hash }
end