Sha256: 1c915436bf878eb3b23e239633e896921c9eea98767fe9dab18c0796dd1e10dc

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 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 such 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! # rubocop:disable Metrics/AbcSize
      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

2 entries across 2 versions & 1 rubygems

Version Path
dkd-ibanizator-0.10.0 lib/ibanizator/bank_db.rb
dkd-ibanizator-0.9.0 lib/ibanizator/bank_db.rb