class Ibanizator

Public Class Methods

bank_db() click to toggle source
# File lib/ibanizator.rb, line 7
def self.bank_db
  @bank_db ||= BankDb.new
end
iban_from_string(a_string) click to toggle source
# File lib/ibanizator.rb, line 11
def self.iban_from_string(a_string)
  Iban.from_string(a_string)
end

Public Instance Methods

calculate_checksum(bank_code, account_number, country_code_num) click to toggle source
# File lib/ibanizator.rb, line 38
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
calculate_iban(options) click to toggle source
# File lib/ibanizator.rb, line 15
def calculate_iban(options) # rubocop:disable Metrics/AbcSize
  # Error handling
  # TODO

  # delete spaces
  options[:account_number] = options[:account_number].to_s.gsub(/\s+/, '')
  options[:bank_code]      = options[:bank_code].to_s.gsub(/\s+/, '')

  # Fill account number to 10 digits
  while options[:account_number].size < 10
    options[:account_number] = options[:account_number].rjust(10, '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
character_to_digit(char) click to toggle source
# File lib/ibanizator.rb, line 34
def character_to_digit(char)
  char.upcase.split('').inject('') { |code, c| code + (c.ord - 55).to_s }
end