Sha256: 7109b51aed9789c0f0e33e31f69888a727ac0fbaec6e092e589935935e77f149

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module MT940

  class NoFileGiven < Exception; end
  class UnknownBank < Exception; end

  class Parser

    attr_accessor :transactions

    def initialize(file)
      file = File.open(file) if file.is_a?(String) 
      if file.is_a?(File) || file.is_a?(Tempfile)
        process(file)
        file.close
      else
        raise NoFileGiven.new('No file is given!')
      end
    end

    private

    def process(file)
      begin
        bank_class = determine_bank_class(file)
        instance   = bank_class.new(file)
        instance.parse
        @transactions = instance.transactions
      rescue NoMethodError => exception
         if exception.message == "undefined method `new' for nil:NilClass"
          raise UnknownBank.new('Could not determine bank!') 
        else
          raise exception
        end
      end
    end

    def determine_bank_class(file)
      first_line = file.readline
      case first_line
      when /^:940:/
        Rabobank
      when /INGBNL/
        Ing
      when /ABNANL/
        Abnamro
      when /^:20:/
        Triodos
      end
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mt940-0.8.0 lib/mt940/parser.rb
mt940-0.7.1 lib/mt940/parser.rb
mt940-0.7.0 lib/mt940/parser.rb