class BaiParser::Parser

Constants

FIELDS
RECORD_CODES

Public Class Methods

new() click to toggle source
# File lib/bai_parser.rb, line 26
def initialize
  @data = {}
end
parse(filename_or_file_contents) click to toggle source
# File lib/bai_parser.rb, line 30
def self.parse(filename_or_file_contents)
  p = self.new
  p.parse filename_or_file_contents
end

Public Instance Methods

parse(filename_or_file_contents) click to toggle source
# 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

Private Instance Methods

account_identifier(record) click to toggle source
# File lib/bai_parser.rb, line 87
def account_identifier(record)
  @account = Hash.new
  h = Hash.new
  h[:record_code], record = next_field record
  h[:customer_account_number], record = next_field record
  h[:currency_code], record = next_field record
  h[:summaries] ||= []
  loop do
    s, record = parse_transaction(record)
    h[:summaries] << s
    break if record == ''
  end
  @account[:account_identifier] = h
end
account_trailer(record) click to toggle source
# File lib/bai_parser.rb, line 144
def account_trailer(record)
  @account[:account_trailer] = default_record_parse(:account_trailer, record)
  (@group[:accounts] ||= []) << @account
end
default_record_parse(type, record) click to toggle source
# File lib/bai_parser.rb, line 69
def default_record_parse(type, record)
  h = Hash.new
  values = record.split(',')
  FIELDS[type].each do |k|
    h[k] = values.shift
  end
  return h
end
file_header(record) click to toggle source
# File lib/bai_parser.rb, line 78
def file_header(record)
  @data[:file_header] = default_record_parse(:file_header, record)
end
file_trailer(record) click to toggle source
# File lib/bai_parser.rb, line 154
def file_trailer(record)
  @data[:file_trailer] = default_record_parse(:file_trailer, record)
end
group_header(record) click to toggle source
# File lib/bai_parser.rb, line 82
def group_header(record)
  @group = Hash.new
  @group[:group_header] = default_record_parse(:group_header, record)
end
group_trailer(record) click to toggle source
# File lib/bai_parser.rb, line 149
def group_trailer(record)
  @group[:group_trailer] = default_record_parse(:group_trailer, record)
  (@data[:groups] ||= []) << @group
end
next_field(record) click to toggle source
# File lib/bai_parser.rb, line 158
def next_field(record)
  a, b, c = record.partition(',')
  return [a, c]
end
parse_transaction(record, detail=false) click to toggle source
# File lib/bai_parser.rb, line 102
def parse_transaction(record, detail=false)
  h = Hash.new
  h[:type_code], record = next_field record
  h[:amount], record = next_field record
  unless detail
    h[:item_count], record = next_field record
  end
  h[:funds_type], record = next_field record
  case h[:funds_type]
  when 'S'
    h[:immediate_availability_amount], record = next_field record
    h[:one_day_availability_amount], record = next_field record
    h[:more_than_one_day_availability_amount], record = next_field record
  when 'V'
    h[:value_date], record = next_field record
    h[:value_time], record = next_field record
  when 'D'
    h[:number_of_availability_distributions], record = next_field record
    h[:distributed_availabilties] = []
    h[:number_of_availability_distributions].times do
      days, record = next_field record
      amount, record = next_field record
      if days and amount
        h[:distributed_availabilties] << {availability_in_days: days, availability_amount: amount}
      end
    end
  end
  return [h, record]
end
transaction_detail(record) click to toggle source
# File lib/bai_parser.rb, line 133
def transaction_detail(record)
  t = Hash.new
  t[:record_code], record = next_field record
  h, record = parse_transaction(record, true)
  t.merge! h
  t[:bank_reference_number], record = next_field record
  t[:customer_reference_number], record = next_field record
  t[:text] = record
  (@account[:transactions] ||= []) << t
end