Sha256: c67a0ba1b8f41911bf7b702bba1a271191129b9b72275c52c1c8dcfda4d20bd9

Contents?: true

Size: 1.16 KB

Versions: 6

Compression:

Stored size: 1.16 KB

Contents

class Ibanizator
  class BankDb
    class BankNotFoundError < StandardError ; end

    attr_reader :known_banks

    def initialize
      @known_banks = []
      @bic_index, @bank_code_index = {}, {}
      populate_known_banks!
    end

    def bank_by_bic(a_bic)
      @bic_index.fetch(a_bic.to_s) do
        error! "No bank for bic: #{a_bic} found"
      end
    end

    def bank_by_bank_code(a_bank_code)
      @bank_code_index.fetch(sanitize_bank_code(a_bank_code)) do
        error! "No bank for bank_code: #{a_bank_code} found"
      end
    end

    private
    def populate_known_banks!
      File.open(File.expand_path("../../../db/blz.txt", __FILE__), 'r').each_line do |line|
        code, _, _, _, _, name, _, bic = line.unpack 'A8A1A58A5A35A27A5A11'
        next if bic.empty?
        name.force_encoding('iso-8859-1').encode!('utf-8')
        bank = Bank.new(bic, name, code)
        @known_banks << bank
        @bic_index[bank.bic] = bank
        @bank_code_index[bank.bank_code] = bank
      end
    end

    def sanitize_bank_code(a_bank_code)
      a_bank_code.to_s.gsub(/\s+/, '')
    end

    def error!(message)
      raise BankNotFoundError, message
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ibanizator-0.3.5 lib/ibanizator/bank_db.rb
ibanizator-0.3.4 lib/ibanizator/bank_db.rb
ibanizator-0.3.3 lib/ibanizator/bank_db.rb
ibanizator-0.3.2 lib/ibanizator/bank_db.rb
ibanizator-0.3.1 lib/ibanizator/bank_db.rb
ibanizator-0.3.0 lib/ibanizator/bank_db.rb