module MxnBanks

Constants

VERSION

Public Class Methods

current_control_digit(arr) click to toggle source
# File lib/mxn_banks.rb, line 32
def self.current_control_digit(arr)
  module_sum = arr.reduce { |a, e| a + e }
  (10 - module_sum.modulo(10)).modulo(10)
end
from_iban(num) click to toggle source
# File lib/mxn_banks.rb, line 8
def self.from_iban(num)
  a = to_hash.select { |b| b[:number] == num.slice(0, 3) }
  fail ArgumentError, 'Invalid bank code' if a.empty?
  Bank.new(a.first)
end
to_hash() click to toggle source
# File lib/mxn_banks.rb, line 14
def self.to_hash
  json_file = File.read(File.join(File.dirname(__FILE__), 'banks.json'))
  @banks ||= JSON.parse(json_file, symbolize_names: true)
end
valid?(clabe) click to toggle source
# File lib/mxn_banks.rb, line 19
def self.valid?(clabe)
  return false if clabe.length < 18
  sliced_clabe = clabe.slice(0, 17).split('')
  w_factor = weight(sliced_clabe)
  module_ten = sliced_clabe.map.with_index { |n, i| (n.to_i * w_factor[i]).modulo(10) }
  clabe.slice(-1).to_i == current_control_digit(module_ten)
end
weight(clabe) click to toggle source
# File lib/mxn_banks.rb, line 27
def self.weight(clabe)
  w_factor = { 0 => 3, 1 => 7, 2 => 1 }
  clabe.map.with_index { |_n, i| w_factor[i.to_i.modulo(3)] }
end