banktools-se 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.markdown +3 -0
- data/lib/banktools-se/ocr.rb +17 -2
- data/lib/banktools-se/version.rb +1 -1
- data/spec/ocr_spec.rb +13 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4f480d8889741823d7cc4e020dff15d39406fd68
|
4
|
+
data.tar.gz: 547a6c83c6fe15e4a6e17f943a9b1b925ff1f3ad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e2b9fa6aeae917ee57d0b2c98303b4437fdb9f7f084ef873cffefc3c9d243a111565f0bf52b7fb10e9af6cc502fee6a72c45ac021e344f9ba6dcdabeaab4b770
|
7
|
+
data.tar.gz: 1070b2bcecd0aca9bdb4a2de78bd94111242a42fa28b072d4ec06fccb15cc8f17e69bc269e0eb218ac4a8b1467b16b8cf68fec6242898da9c03e0f00bf2e0cb8
|
data/README.markdown
CHANGED
@@ -34,11 +34,14 @@ Inspired by [iulianu/iban-tools](https://github.com/iulianu/iban-tools). Please
|
|
34
34
|
fundraising_account.fundraising? # => true
|
35
35
|
|
36
36
|
# OCR
|
37
|
+
|
37
38
|
BankTools::SE::OCR.from_number("123") # => "1230"
|
38
39
|
BankTools::SE::OCR.from_number("123", length_digit: true) # => "12351"
|
39
40
|
BankTools::SE::OCR.from_number("123", length_digit: true, pad: "0") # => "123067"
|
41
|
+
|
40
42
|
BankTools::SE::OCR.to_number("1230") # => "123"
|
41
43
|
BankTools::SE::OCR.to_number("123067", length_digit: true, pad: "0") # => "123"
|
44
|
+
BankTools::SE::OCR.to_number("1230", length_digit: true, pad: "0") # Raises exception because there's no length digit or padding.
|
42
45
|
|
43
46
|
# Plusgiro
|
44
47
|
|
data/lib/banktools-se/ocr.rb
CHANGED
@@ -5,6 +5,8 @@ module BankTools
|
|
5
5
|
class OCR
|
6
6
|
class InvalidOCR < StandardError; end
|
7
7
|
class OverlongOCR < InvalidOCR; end
|
8
|
+
class BadPadding < InvalidOCR; end
|
9
|
+
class BadLengthDigit < InvalidOCR; end
|
8
10
|
class BadChecksum < InvalidOCR; end
|
9
11
|
class MustBeNumeric < InvalidOCR; end
|
10
12
|
|
@@ -34,14 +36,27 @@ module BankTools
|
|
34
36
|
|
35
37
|
def self.to_number(number, opts = {})
|
36
38
|
number = number.to_s
|
37
|
-
|
39
|
+
should_have_length_digit = opts.fetch(:length_digit, false)
|
38
40
|
strip_padding = opts.fetch(:pad, "").to_s
|
39
41
|
|
40
42
|
raise MustBeNumeric unless number.match(/\A\d+\z/)
|
41
43
|
raise BadChecksum unless Utils.valid_luhn?(number)
|
42
44
|
|
45
|
+
if should_have_length_digit
|
46
|
+
length_digit = number[-2]
|
47
|
+
last_digit_of_actual_length = number.length.to_s[-1]
|
48
|
+
raise BadLengthDigit if length_digit != last_digit_of_actual_length
|
49
|
+
end
|
50
|
+
|
43
51
|
digits_to_chop = 1 # Checksum.
|
44
|
-
digits_to_chop += 1 if
|
52
|
+
digits_to_chop += 1 if should_have_length_digit
|
53
|
+
|
54
|
+
if strip_padding.length > 0
|
55
|
+
expected_padding_end = -digits_to_chop - 1
|
56
|
+
expected_padding_start = expected_padding_end - strip_padding.length + 1
|
57
|
+
raise BadPadding if number[expected_padding_start..expected_padding_end] != strip_padding
|
58
|
+
end
|
59
|
+
|
45
60
|
digits_to_chop += strip_padding.length
|
46
61
|
|
47
62
|
number[0...-digits_to_chop]
|
data/lib/banktools-se/version.rb
CHANGED
data/spec/ocr_spec.rb
CHANGED
@@ -42,7 +42,7 @@ describe BankTools::SE::OCR do
|
|
42
42
|
BankTools::SE::OCR.to_number("123456789023", length_digit: true).should eq "1234567890"
|
43
43
|
end
|
44
44
|
|
45
|
-
it "
|
45
|
+
it "strips the given padding" do
|
46
46
|
BankTools::SE::OCR.to_number("1234567890037", length_digit: true, pad: "0").should eq "1234567890"
|
47
47
|
end
|
48
48
|
|
@@ -50,6 +50,18 @@ describe BankTools::SE::OCR do
|
|
50
50
|
expect { BankTools::SE::OCR.to_number("1231") }.to raise_error(BankTools::SE::OCR::BadChecksum)
|
51
51
|
end
|
52
52
|
|
53
|
+
it "raises if length digit is wrong" do
|
54
|
+
expect { BankTools::SE::OCR.to_number("12369", length_digit: true) }.to raise_error(BankTools::SE::OCR::BadLengthDigit)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "raises if padding doesn't match the given value" do
|
58
|
+
expect { BankTools::SE::OCR.to_number("1230", pad: "") }.not_to raise_error
|
59
|
+
expect { BankTools::SE::OCR.to_number("12302", pad: "0") }.not_to raise_error
|
60
|
+
expect { BankTools::SE::OCR.to_number("1230002", pad: "000") }.not_to raise_error
|
61
|
+
|
62
|
+
expect { BankTools::SE::OCR.to_number("12344", pad: "0") }.to raise_error(BankTools::SE::OCR::BadPadding)
|
63
|
+
end
|
64
|
+
|
53
65
|
it "raises if input is non-numeric" do
|
54
66
|
expect { BankTools::SE::OCR.to_number("garbage") }.to raise_error(BankTools::SE::OCR::MustBeNumeric)
|
55
67
|
end
|