ibandit 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 79bb8d78b071a6a3cfb74852b2f62641af34014d
4
- data.tar.gz: 3868681d7108888d0259ff51f145bfefb481035f
3
+ metadata.gz: 3c7cc32ffca411662b2ec9370ec48119b19c7c29
4
+ data.tar.gz: efad5f26823ef1843a63351de65d3c618ff2f0ee
5
5
  SHA512:
6
- metadata.gz: b3e0bb71e33693b7b7ad0a67a3b7aa911dd3d1ef0c0f2e1c1cfa4ca2a0815f2b4f3701cf76f6414fb674da299dc6c06cef64ad2edc98dfad990c5a9095dcff2e
7
- data.tar.gz: 859b58bd96989e6bf05ab2127fb29939ad4db924bf816d97c5bfd86ee6b13d424d51ee7f6a3536def34375b17fab2bd9489f7dc1178ed4f98f06b921a294b3ad
6
+ metadata.gz: 85a6244394f46b3f9513314bcdc69875a6f1d14cf4ca33a17ff9ee913de7f76746690eadc64ca66384c36cbe2536c5e8bef26745d6ddffa9cefe971bc476c26e
7
+ data.tar.gz: 44b6dba9272e2b9a357421a415f8269b8edf04f143b2164b3ba2b2d5930120e94c49db9920699a41503bb6c0976d33f994cccfe81745d7a06e34119a54af7871
@@ -12,7 +12,7 @@ LineLength:
12
12
  Max: 80
13
13
 
14
14
  ClassLength:
15
- Max: 200
15
+ Max: 300
16
16
 
17
17
  # Avoid single-line methods.
18
18
  SingleLineMethods:
@@ -1,3 +1,7 @@
1
+ ## 0.3.2 - February 24, 2015
2
+
3
+ - Allow for injection of a modulus checker
4
+
1
5
  ## 0.3.1 - February 14, 2015
2
6
 
3
7
  - Add Malta to LocalDetailsCleaner and IBANAssembler
data/README.md CHANGED
@@ -64,6 +64,27 @@ The following error keys may be set:
64
64
  - `length`
65
65
  - `format`
66
66
 
67
+ Ibandit will also apply local modulus checks if you set a modulus checker:
68
+
69
+ ```ruby
70
+ module ModulusChecker
71
+ def self.valid_bank_code?(iban_string)
72
+ some_codes
73
+ end
74
+
75
+ def self.valid_account_number?(iban_string)
76
+ some_codes
77
+ end
78
+ end
79
+
80
+ Ibandit.modulus_checker = ModulusChecker
81
+ ```
82
+
83
+ Both the `valid_bank_code?` and `valid_account_number?` methods will receive the plain-text IBAN.
84
+ `valid_bank_code?` should return true unless it is known that the bank/branch code in this IBAN are
85
+ invalid in the country specified. `valid_account_number?` should return true unless it is known that
86
+ the account number in this IBAN cannot be valid due to local modulus checking rules.
87
+
67
88
  ### Deconstructing an IBAN into national banking details
68
89
 
69
90
  SWIFT define the following components for IBANs, and publish details of how each
@@ -9,7 +9,7 @@ require 'ibandit/check_digit'
9
9
 
10
10
  module Ibandit
11
11
  class << self
12
- attr_accessor :bic_finder
12
+ attr_accessor :bic_finder, :modulus_checker
13
13
 
14
14
  def find_bic(country_code, national_id)
15
15
  raise NotImplementedError, 'BIC finder is not defined' unless @bic_finder
@@ -67,7 +67,8 @@ module Ibandit
67
67
  valid_format?,
68
68
  valid_bank_code_format?,
69
69
  valid_branch_code_format?,
70
- valid_account_number_format?
70
+ valid_account_number_format?,
71
+ valid_local_modulus_check?
71
72
  ].all?
72
73
  end
73
74
 
@@ -210,6 +211,13 @@ module Ibandit
210
211
  end
211
212
  end
212
213
 
214
+ def valid_local_modulus_check?
215
+ return unless valid_format?
216
+ return true unless Ibandit.modulus_checker
217
+
218
+ valid_modulus_check_bank_code? && valid_modulus_check_account_number?
219
+ end
220
+
213
221
  ###################
214
222
  # Private methods #
215
223
  ###################
@@ -254,5 +262,28 @@ module Ibandit
254
262
  def formatted
255
263
  iban.to_s.gsub(/(.{4})/, '\1 ').strip
256
264
  end
265
+
266
+ def valid_modulus_check_bank_code?
267
+ return true if Ibandit.modulus_checker.valid_bank_code?(iban.to_s)
268
+
269
+ @errors[modulus_check_bank_code_field] = 'is invalid'
270
+ false
271
+ end
272
+
273
+ def valid_modulus_check_account_number?
274
+ return true if Ibandit.modulus_checker.valid_account_number?(iban.to_s)
275
+
276
+ @errors[:account_number] = 'is invalid'
277
+ false
278
+ end
279
+
280
+ def modulus_check_bank_code_field
281
+ if LocalDetailsCleaner.required_fields(country_code).
282
+ include?(:branch_code)
283
+ :branch_code
284
+ else
285
+ :bank_code
286
+ end
287
+ end
257
288
  end
258
289
  end
@@ -1,3 +1,3 @@
1
1
  module Ibandit
2
- VERSION = '0.3.1'.freeze
2
+ VERSION = '0.3.2'.freeze
3
3
  end
@@ -456,6 +456,51 @@ describe Ibandit::IBAN do
456
456
  end
457
457
  end
458
458
 
459
+ describe '#valid_local_modulus_check?' do
460
+ subject(:valid_local_modulus_check?) { iban.valid_local_modulus_check? }
461
+
462
+ context 'without a modulus checker defined' do
463
+ it { is_expected.to be(true) }
464
+ end
465
+
466
+ context 'with a modulus checker defined' do
467
+ before do
468
+ Ibandit.modulus_checker = double(
469
+ valid_bank_code?: valid_bank_code,
470
+ valid_account_number?: valid_account_number)
471
+ end
472
+ after { Ibandit.modulus_checker = nil }
473
+ before { iban.valid_local_modulus_check? }
474
+
475
+ context 'with an invalid bank code' do
476
+ let(:iban_code) { 'AT611904300234573201' }
477
+ let(:valid_bank_code) { false }
478
+ let(:valid_account_number) { true }
479
+
480
+ it { is_expected.to be(false) }
481
+ specify { expect(iban.errors).to include(bank_code: 'is invalid') }
482
+
483
+ context 'when the bank code is not required' do
484
+ let(:iban_code) { 'GB60BARC20000055779911' }
485
+ before { Ibandit.bic_finder = double(call: 'BARCGB22XXX') }
486
+ after { Ibandit.bic_finder = nil }
487
+ before { iban.valid_local_modulus_check? }
488
+
489
+ it { is_expected.to be(false) }
490
+ specify { expect(iban.errors).to include(branch_code: 'is invalid') }
491
+ end
492
+ end
493
+
494
+ context 'with an invalid account number' do
495
+ let(:valid_bank_code) { true }
496
+ let(:valid_account_number) { false }
497
+
498
+ it { is_expected.to be(false) }
499
+ specify { expect(iban.errors).to include(account_number: 'is invalid') }
500
+ end
501
+ end
502
+ end
503
+
459
504
  describe '#valid?' do
460
505
  describe 'validations called' do
461
506
  after { iban.valid? }
@@ -483,6 +528,10 @@ describe Ibandit::IBAN do
483
528
  it 'validates the account number format' do
484
529
  expect(iban).to receive(:valid_account_number_format?).at_least(1)
485
530
  end
531
+
532
+ it 'runs local modulus checks' do
533
+ expect(iban).to receive(:valid_local_modulus_check?).at_least(1)
534
+ end
486
535
  end
487
536
 
488
537
  context 'for a valid Albanian IBAN' do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ibandit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grey Baker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-15 00:00:00.000000000 Z
11
+ date: 2015-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec