Sha256: 507b2daf69ff981fceddbe5915851e50d3ca7e872fb9513f588f75a2444fede8

Contents?: true

Size: 1.22 KB

Versions: 4

Compression:

Stored size: 1.22 KB

Contents

require_relative 'iban/validator'
require_relative 'swift_bic/bank_db'

class Ibanizator
  def calculate_iban options
    # Error handling
    # TODO

    # Fill account number to 10 digits
    while options[:account_number].size < 10 do
      options[:account_number].insert(0, '0')
    end

    country_code_num = character_to_digit options[:country_code].to_s
    checksum = calculate_checksum options[:bank_code], options[:account_number], country_code_num

    options[:country_code].to_s.upcase + checksum + options[:bank_code] + options[:account_number]
  end

  def validate_iban iban
    # for the sake of compatibility
    validator = Iban::Validator.new
    validator.validate_iban(iban)
  end

  def bic bank_code
    bank_db = SwiftBic::BankDb.new bank_code
    bank_db.bic
  end

  def bank_name bank_code
    bank_db = SwiftBic::BankDb.new bank_code
    bank_db.bank_name
  end

  def character_to_digit char
    char.upcase!.split('').inject('') { |code, c| code + (c.ord - 55).to_s }
  end

  def calculate_checksum bank_code, account_number, country_code_num
    x = (bank_code + account_number + country_code_num + '00').to_i % 97
    checksum = (98 - x).to_s
    checksum.length == 1 ? checksum.insert(0, '0') : checksum
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ibanizator-0.1.3 lib/ibanizator.rb
ibanizator-0.1.2 lib/ibanizator.rb
ibanizator-0.1.1 lib/ibanizator.rb
ibanizator-0.1.0 lib/ibanizator.rb