class Headache::DocumentParser

Constants

LINE_SEPARATOR

Public Class Methods

new(string_or_file) click to toggle source
# File lib/headache/document_parser.rb, line 5
def initialize(string_or_file)
  @ach_string = string_or_file.respond_to?(:read) ? string_or_file.read : string_or_file
end

Public Instance Methods

parse() click to toggle source
# File lib/headache/document_parser.rb, line 9
def parse
  fail Headache::InvalidRecordType, invalid_fail_message if invalid_ach?

  Headache::Document.new(
    Record::FileHeader.new(nil).parse(records.shift),
    Record::FileControl.new(nil).parse(records.pop),
    get_batches.map { |b| Headache::Batch.new(self).parse(b) }
  )
end

Protected Instance Methods

get_batches() click to toggle source
# File lib/headache/document_parser.rb, line 37
def get_batches
  batches = []
  batch   = []
  records.each do |record|
    if record.starts_with?(Headache::Record::BatchHeader.record_type_codes[:batch_header].to_s)
      batches << batch unless batches.empty? && batch == []
      batch = [record]
    else
      batch << record
    end
  end
  batches << batch
end
invalid_ach?() click to toggle source
# File lib/headache/document_parser.rb, line 29
def invalid_ach?
  invalid_records.any?
end
invalid_fail_message() click to toggle source
# File lib/headache/document_parser.rb, line 25
def invalid_fail_message
  "unknown record type(s): #{invalid_records.map(&:first).inspect}"
end
invalid_records() click to toggle source
# File lib/headache/document_parser.rb, line 33
def invalid_records
  @invalid_records ||= records.reject { |record| Headache::Record::FileHeader.record_type_codes.values.include?(record.first.to_i) }
end
records() click to toggle source
# File lib/headache/document_parser.rb, line 21
def records
  @records ||= @ach_string.split(LINE_SEPARATOR).reject { |record| record == Headache::Record::Overflow.new.generate.strip }
end