Module: Hotfile::Parser

Included in:
Hotfile
Defined in:
lib/hotfile/parser.rb

Overview

Functions for parsing HOT file data

Instance Method Summary collapse

Instance Method Details

#parseObject



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/hotfile/parser.rb', line 6

def parse
  return if @file.nil?

  records = @file.lines(chomp: true).map { |line| parse_line line }
  @parse ||= {
    records: records,
    date: records.map { |x| x[:date] }.compact.first,
    doc_nr: records.map { |x| x[:doc_nr] }.compact.first,
    transactions: records.map { |x| x[:transaction] }.compact.uniq.count
  }
end

#parse_line(line) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/hotfile/parser.rb', line 22

def parse_line(line)
  code1, line_number, code2, payload = line.scan(/([A-Z]{3})(\d{8})(\d{2})(.*)/).flatten
  code = code1 + code2
  line_number = line_number.to_i

  if (10..81).include? code2.to_i
    date, transaction, doc_nr, check_digit, payload =
      payload.scan(/
        (\d{6})
        (\d{6})
        ([A-Z0-9 ]{14})
        (\d)
        (.*)
      /x).flatten
  end

  begin
    record_parser = Hotfile::Record.const_get(code)
  rescue NameError
    # Record type does not have a class implementing it.
  end
  data = record_parser.new(payload).parse if record_parser

  {
    code: { id: code, category: code1, number: code2.to_i },
    line_number: line_number,
    raw_payload: payload.strip,
    date: Hotfile::Date.new(date).to_date,
    transaction: transaction&.to_i,
    doc_nr: doc_nr&.strip,
    check_digit: check_digit&.to_i,
    data: data
  }
end

#unparsableObject



18
19
20
# File 'lib/hotfile/parser.rb', line 18

def unparsable
  parse[:records].select! { |x| x[:data].nil? }
end