Class: Ibanizator

Inherits:
Object
  • Object
show all
Defined in:
lib/ibanizator.rb,
lib/ibanizator/bank.rb,
lib/ibanizator/iban.rb,
lib/ibanizator/bank_db.rb,
lib/ibanizator/iban/invalid.rb,
lib/ibanizator/iban/validator.rb,
lib/ibanizator/iban/country_codes.rb,
lib/ibanizator/iban/extended_data.rb,
lib/ibanizator/iban/extended_data/de.rb

Defined Under Namespace

Classes: Bank, BankDb, Iban

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.bank_dbObject



7
8
9
# File 'lib/ibanizator.rb', line 7

def self.bank_db
  @bank_db ||= BankDb.new
end

.iban_from_string(a_string) ⇒ Object



11
12
13
# File 'lib/ibanizator.rb', line 11

def self.iban_from_string(a_string)
  Iban.from_string(a_string)
end

Instance Method Details

#calculate_checksum(bank_code, account_number, country_code_num) ⇒ Object



38
39
40
41
42
# File 'lib/ibanizator.rb', line 38

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

#calculate_iban(options) ⇒ Object

rubocop:disable Metrics/AbcSize



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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) ⇒ Object



34
35
36
# File 'lib/ibanizator.rb', line 34

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