Class: BaiParser::Parser

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

Constant Summary collapse

RECORD_CODES =
{'01' => :file_header,
'02' => :group_header,
'03' => :account_identifier,
'16' => :transaction_detail,
'49' => :account_trailer,
'88' => :continuation,
'98' => :group_trailer,
'99' => :file_trailer }
FIELDS =
{:file_header => [:record_code,:sender_identification, :receiver_identification, :file_creation_date, :file_creation_time,
                 :file_identification_number, :physical_record_length, :block_size, :version_number],
:group_header => [:record_code,:ultimate_receiver_identification, :originator_identification, :group_status, :as_of_date,
                  :as_of_time, :currency_code, :as_of_date_modifier],
:group_trailer => [:record_code,:group_control_total, :number_of_accounts, :number_of_records],
:account_trailer => [:record_code,:account_control_total, :number_of_records],
:file_trailer => [:record_code,:file_control_total, :number_of_groups, :number_of_records]                          
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeParser

Returns a new instance of Parser.



26
27
28
# File 'lib/bai_parser.rb', line 26

def initialize
  @data = {}
end

Class Method Details

.parse(filename_or_file_contents) ⇒ Object



30
31
32
33
# File 'lib/bai_parser.rb', line 30

def self.parse(filename_or_file_contents)
  p = self.new
  p.parse filename_or_file_contents
end

Instance Method Details

#parse(filename_or_file_contents) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/bai_parser.rb', line 35

def parse(filename_or_file_contents)
  if File.file? filename_or_file_contents
    f = File.open(filename_or_file_contents)
  elsif filename_or_file_contents =~ /^01,/
    f = StringIO.new(filename_or_file_contents)
  else
    raise ArgumentError, "Did not provide valid BAI data (does not start with 01 File Header record) or valid file name"
  end
  record = next_line = f.gets.chomp
  count = 1
  loop do
    loop do # gather continuation lines
      next_line = f.gets
      break if next_line.nil?
      next_line.chomp!
      count += 1
      if next_line[0..1] == '88'
        record.sub!(/\/\s*$/,',')
        record += next_line[3..-1]
      else
        break
      end
    end
    record.sub!(/\/\s*$/,'')
    self.send RECORD_CODES[record[0..1]], record
    break if next_line.nil?
    record = next_line
  end
  f.close
  return @data
end