Module: MxnBanks
- Defined in:
- lib/mxn_banks.rb,
lib/mxn_banks/bank.rb,
lib/mxn_banks/version.rb
Defined Under Namespace
Classes: Bank
Constant Summary collapse
- VERSION =
'0.1.6'
Class Method Summary collapse
- .current_control_digit(arr) ⇒ Object
- .from_iban(num) ⇒ Object
- .to_hash ⇒ Object
- .valid?(clabe) ⇒ Boolean
- .weight(clabe) ⇒ Object
Class Method Details
.current_control_digit(arr) ⇒ Object
32 33 34 35 |
# 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) ⇒ Object
8 9 10 11 12 |
# 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 ⇒ Object
14 15 16 17 |
# 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) ⇒ Boolean
19 20 21 22 23 24 25 |
# 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) ⇒ Object
27 28 29 30 |
# 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 |