Sha256: 422c6ec33b96d185e32380fa66afd8b135ab2d72af3361e337a2593926e6abcb

Contents?: true

Size: 1011 Bytes

Versions: 6

Compression:

Stored size: 1011 Bytes

Contents

# frozen_string_literal: true

module Buckaruby
  # Helper for calculating the IBAN for a given account number, bank code and country code.
  class Iban
    def self.calculate_iban(account_number, bank_code, country_code = "NL")
      if account_number.nil? || account_number.to_s.empty?
        raise ArgumentError, "Invalid account number"
      end

      if bank_code.nil? || bank_code.to_s.empty?
        raise ArgumentError, "Invalid bank code"
      end

      if country_code.nil? || !country_code.match(/^[A-Z]{2}$/)
        raise ArgumentError, "Invalid or unknown country code"
      end

      account_identification = bank_code + account_number.rjust(10, '0')
      country_code_added = account_identification + country_code
      all_numbers = country_code_added.gsub(/[A-Z]/) { |p| (p.respond_to?(:ord) ? p.ord : p[0]) - 55 } + '00'
      verification_number = (98 - (all_numbers.to_i % 97)).to_s.rjust(2, '0')

      country_code + verification_number + account_identification
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
buckaruby-1.4.0 lib/buckaruby/iban.rb
buckaruby-1.3.1 lib/buckaruby/iban.rb
buckaruby-1.3.0 lib/buckaruby/iban.rb
buckaruby-1.2.0 lib/buckaruby/iban.rb
buckaruby-1.1.1 lib/buckaruby/iban.rb
buckaruby-1.1.0 lib/buckaruby/iban.rb