banktools-se 2.5.0 → 2.6.0
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 +4 -4
- data/README.markdown +4 -3
- data/lib/banktools-se/account.rb +1 -1
- data/lib/banktools-se/ocr.rb +20 -6
- data/lib/banktools-se/version.rb +1 -1
- data/spec/account_spec.rb +34 -34
- data/spec/bankgiro_spec.rb +20 -20
- data/spec/ocr_spec.rb +25 -29
- data/spec/plusgiro_spec.rb +21 -21
- data/spec/spec_helper.rb +0 -1
- data/spec/utils_spec.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7718cc4bfa1982f2d41ca5a8e1a8ca872f647ba9
|
4
|
+
data.tar.gz: 61a4d355be53ebf5e0537b5b0a60b07af4869386
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 309b92f8160559eee8e70e22c3d24c961210272d6d6122770ab6ff36fae38c3855fc95a0c495aaeb304ef3e9cdd03fb3feb9aedd2b8645ed658cc0551b6351b6
|
7
|
+
data.tar.gz: b4efc5ea6430a8a6dddabee2113f30f935587b948f9088cc21ac368693536e10efde30e7d9c395697948d186084582ba9dc0a4305a68be9ff1d449d6521de041
|
data/README.markdown
CHANGED
@@ -46,9 +46,9 @@ Inspired by [iulianu/iban-tools](https://github.com/iulianu/iban-tools). Please
|
|
46
46
|
# This feature is intended to try to find all OCR numbers in a noisy bank statement string.
|
47
47
|
# By design it may find too many numbers (e.g. valid substrings of other numbers), so you should check results against actual outstanding invoices.
|
48
48
|
# By default, it excludes OCRs shorter than 4 digits and longer than 19 digits, but this limit can be specified per below.
|
49
|
-
BankTools::SE::OCR.find_all_in_string("OCR1230 and ref4564 and 7890") # => [ "1230", "4564" ]
|
49
|
+
BankTools::SE::OCR.find_all_in_string("OCR1230 and ref4564 and 7890") # => [ "1230", "4564", … ]
|
50
50
|
BankTools::SE::OCR.find_all_in_string("1230 and 123067", length_digit: true, pad: "0") # => [ "123067" ]
|
51
|
-
BankTools::SE::OCR.find_all_in_string("00 and 18 and 1230", min_length: 2, max_length: 3) # => [ "00", "18" ]
|
51
|
+
BankTools::SE::OCR.find_all_in_string("00 and 18 and 1230", min_length: 2, max_length: 3) # => [ "00", "018", "18" ]
|
52
52
|
|
53
53
|
# Plusgiro
|
54
54
|
|
@@ -112,13 +112,14 @@ Or install it yourself as:
|
|
112
112
|
|
113
113
|
Possible improvements to make:
|
114
114
|
|
115
|
-
* Luhn validation based on [
|
115
|
+
* Luhn validation based on [these docs](https://www.bankgirot.se/globalassets/dokument/anvandarmanualer/bankernaskontonummeruppbyggnad_anvandarmanual_sv.pdf).
|
116
116
|
|
117
117
|
|
118
118
|
## Also see
|
119
119
|
|
120
120
|
* [BankTools::DE (German)](https://github.com/barsoom/banktools-de)
|
121
121
|
* [iban-tools](https://github.com/iulianu/iban-tools)
|
122
|
+
* [Bankgirocentral documentation](https://www.bankgirot.se/kundservice/handbocker/)
|
122
123
|
|
123
124
|
|
124
125
|
## Credits and license
|
data/lib/banktools-se/account.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
|
-
#
|
3
|
+
# https://www.bankgirot.se/globalassets/dokument/anvandarmanualer/bankernaskontonummeruppbyggnad_anvandarmanual_sv.pdf
|
4
4
|
|
5
5
|
require "banktools-se/account/clearing_number"
|
6
6
|
|
data/lib/banktools-se/ocr.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# https://www.bankgirot.se/globalassets/dokument/anvandarmanualer/bankgiroinbetalningar_anvandarmanual_sv_31okt2016.pdf section 5.2
|
2
2
|
|
3
3
|
module BankTools
|
4
4
|
module SE
|
@@ -66,14 +66,28 @@ module BankTools
|
|
66
66
|
|
67
67
|
# max_length is 19 because that's the longest allowed integer by default in a Postgres integer column with Ruby on Rails. So attempting some queries with longer OCRs may cause exceptions.
|
68
68
|
def self.find_all_in_string(string, length_digit: false, pad: "", min_length: 4, max_length: 19)
|
69
|
-
|
69
|
+
# First, treat the input as one long string of digits.
|
70
|
+
# E.g. "1234 and 5678" becomes "12345678".
|
71
|
+
digit_string = string.gsub(/\D/, "")
|
72
|
+
digit_string_length = digit_string.length
|
70
73
|
|
71
|
-
|
74
|
+
candidates = []
|
72
75
|
|
73
|
-
|
74
|
-
|
76
|
+
# Then find all substrings of min_length, and of all other lengths, up to max_length.
|
77
|
+
# So e.g. find all four-digit substrings ("1234", "2345", …), all five-digit substrings and so on.
|
75
78
|
|
76
|
-
|
79
|
+
0.upto(digit_string.length - min_length) do |start_pos|
|
80
|
+
min_end_pos = [ start_pos + min_length, digit_string_length ].min - 1
|
81
|
+
max_end_pos = [ start_pos + max_length, digit_string_length ].min - 1
|
82
|
+
|
83
|
+
min_end_pos.upto(max_end_pos) do |end_pos|
|
84
|
+
candidates << digit_string.slice(start_pos..end_pos)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Finally, limit these substrings to ones that are actually valid OCRs.
|
89
|
+
|
90
|
+
candidates.select { |candidate|
|
77
91
|
begin
|
78
92
|
to_number(candidate, length_digit: length_digit, pad: pad)
|
79
93
|
true
|
data/lib/banktools-se/version.rb
CHANGED
data/spec/account_spec.rb
CHANGED
@@ -5,20 +5,20 @@ require "banktools-se"
|
|
5
5
|
|
6
6
|
describe BankTools::SE::Account do
|
7
7
|
it "should initialize" do
|
8
|
-
BankTools::SE::Account.new("foo").
|
8
|
+
expect(BankTools::SE::Account.new("foo")).to be_a(BankTools::SE::Account)
|
9
9
|
end
|
10
10
|
|
11
11
|
describe "#valid?" do
|
12
12
|
it "should be true with no errors" do
|
13
13
|
account = BankTools::SE::Account.new("foo")
|
14
|
-
account.
|
15
|
-
account.
|
14
|
+
allow(account).to receive(:errors).and_return([])
|
15
|
+
expect(account).to be_valid
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should be false with errors" do
|
19
19
|
account = BankTools::SE::Account.new("foo")
|
20
|
-
account.
|
21
|
-
account.
|
20
|
+
allow(account).to receive(:errors).and_return([:error])
|
21
|
+
expect(account).not_to be_valid
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -68,99 +68,99 @@ describe BankTools::SE::Account do
|
|
68
68
|
|
69
69
|
].each do |number|
|
70
70
|
it "should be empty for a valid number like #{number}" do
|
71
|
-
BankTools::SE::Account.new(number).errors.
|
71
|
+
expect(BankTools::SE::Account.new(number).errors).to eq([])
|
72
72
|
end
|
73
73
|
end
|
74
74
|
|
75
75
|
it "should include :too_short for numbers shorter than the bank allows" do
|
76
|
-
BankTools::SE::Account.new("11007").errors.
|
76
|
+
expect(BankTools::SE::Account.new("11007").errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
77
77
|
end
|
78
78
|
|
79
79
|
it "should not include :too_short for Swedbank/Sparbanker numbers that can be zerofilled" do
|
80
|
-
BankTools::SE::Account.new("8000-2-00000000").errors.
|
81
|
-
BankTools::SE::Account.new("9300-2-00000000").errors.
|
82
|
-
BankTools::SE::Account.new("9570-2-00000000").errors.
|
80
|
+
expect(BankTools::SE::Account.new("8000-2-00000000").errors).not_to include(BankTools::SE::Errors::TOO_SHORT)
|
81
|
+
expect(BankTools::SE::Account.new("9300-2-00000000").errors).not_to include(BankTools::SE::Errors::TOO_SHORT)
|
82
|
+
expect(BankTools::SE::Account.new("9570-2-00000000").errors).not_to include(BankTools::SE::Errors::TOO_SHORT)
|
83
83
|
end
|
84
84
|
|
85
85
|
it "should include :too_long for numbers longer than the bank allows" do
|
86
|
-
BankTools::SE::Account.new("1100000000007").errors.
|
86
|
+
expect(BankTools::SE::Account.new("1100000000007").errors).to include(BankTools::SE::Errors::TOO_LONG)
|
87
87
|
end
|
88
88
|
|
89
89
|
it "should not include :too_long for Swedbank/Sparbanker numbers with clearing checksum" do
|
90
|
-
BankTools::SE::Account.new("8000-2-0000000000").errors.
|
90
|
+
expect(BankTools::SE::Account.new("8000-2-0000000000").errors).not_to include(BankTools::SE::Errors::TOO_LONG)
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should include :invalid_characters for numbers with other character than digits, spaces and dashes" do
|
94
|
-
BankTools::SE::Account.new("1 2-3X").errors.
|
95
|
-
BankTools::SE::Account.new("1 2-3").errors.
|
94
|
+
expect(BankTools::SE::Account.new("1 2-3X").errors).to include(BankTools::SE::Errors::INVALID_CHARACTERS)
|
95
|
+
expect(BankTools::SE::Account.new("1 2-3").errors).not_to include(BankTools::SE::Errors::INVALID_CHARACTERS)
|
96
96
|
end
|
97
97
|
|
98
98
|
it "should include :bad_checksum for Nordea personkonto if the serial Luhn/mod 10 checksum is incorrect" do
|
99
|
-
BankTools::SE::Utils.valid_luhn?("800928-6249").
|
100
|
-
BankTools::SE::Utils.valid_luhn?("3300-800928-6249").
|
101
|
-
BankTools::SE::Account.new("3300-800928-6249").errors.
|
99
|
+
expect(BankTools::SE::Utils.valid_luhn?("800928-6249")).to eq(true)
|
100
|
+
expect(BankTools::SE::Utils.valid_luhn?("3300-800928-6249")).to eq(false)
|
101
|
+
expect(BankTools::SE::Account.new("3300-800928-6249").errors).not_to include(BankTools::SE::Errors::BAD_CHECKSUM)
|
102
102
|
end
|
103
103
|
|
104
104
|
it "should include :unknown_clearing_number if the clearing number is unknown" do
|
105
|
-
BankTools::SE::Account.new("10000000009").errors.
|
106
|
-
BankTools::SE::Account.new("11000000007").errors.
|
105
|
+
expect(BankTools::SE::Account.new("10000000009").errors).to include(BankTools::SE::Errors::UNKNOWN_CLEARING_NUMBER)
|
106
|
+
expect(BankTools::SE::Account.new("11000000007").errors).not_to include(BankTools::SE::Errors::UNKNOWN_CLEARING_NUMBER)
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
110
|
describe "#bank" do
|
111
111
|
it "should return the bank for the current clearing number" do
|
112
|
-
BankTools::SE::Account.new("11000000007").bank.
|
113
|
-
BankTools::SE::Account.new("11550000001").bank.
|
114
|
-
BankTools::SE::Account.new("11990000009").bank.
|
115
|
-
BankTools::SE::Account.new("12000000005").bank.
|
112
|
+
expect(BankTools::SE::Account.new("11000000007").bank).to eq("Nordea")
|
113
|
+
expect(BankTools::SE::Account.new("11550000001").bank).to eq("Nordea")
|
114
|
+
expect(BankTools::SE::Account.new("11990000009").bank).to eq("Nordea")
|
115
|
+
expect(BankTools::SE::Account.new("12000000005").bank).to eq("Danske Bank")
|
116
116
|
end
|
117
117
|
|
118
118
|
it "should return nil for unknown clearing numbers" do
|
119
|
-
BankTools::SE::Account.new("10000000009").bank.
|
119
|
+
expect(BankTools::SE::Account.new("10000000009").bank).to be_nil
|
120
120
|
end
|
121
121
|
end
|
122
122
|
|
123
123
|
describe "#clearing_number" do
|
124
124
|
it "should be the first four digits" do
|
125
|
-
BankTools::SE::Account.new("12345678").clearing_number.
|
125
|
+
expect(BankTools::SE::Account.new("12345678").clearing_number).to eq("1234")
|
126
126
|
end
|
127
127
|
|
128
128
|
it "should be the first five digits if there is a clearing number checksum" do
|
129
|
-
BankTools::SE::Account.new("8000-2-0000000000").clearing_number.
|
129
|
+
expect(BankTools::SE::Account.new("8000-2-0000000000").clearing_number).to eq("8000-2")
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
133
|
describe "#serial_number" do
|
134
134
|
it "should be the digits after the first four digits" do
|
135
|
-
BankTools::SE::Account.new("12345678").serial_number.
|
135
|
+
expect(BankTools::SE::Account.new("12345678").serial_number).to eq("5678")
|
136
136
|
end
|
137
137
|
|
138
138
|
it "should be the digits after the first five digits if there is a clearing number checksum" do
|
139
|
-
BankTools::SE::Account.new("8000-2-0000000000").serial_number.
|
139
|
+
expect(BankTools::SE::Account.new("8000-2-0000000000").serial_number).to eq("0000000000")
|
140
140
|
end
|
141
141
|
|
142
142
|
it "should be the empty string if there aren't enough numbers" do
|
143
|
-
BankTools::SE::Account.new("12").serial_number.
|
143
|
+
expect(BankTools::SE::Account.new("12").serial_number).to eq("")
|
144
144
|
end
|
145
145
|
|
146
146
|
end
|
147
147
|
|
148
148
|
describe "#normalize" do
|
149
149
|
it "should normalize to clearing number dash serial number" do
|
150
|
-
account = BankTools::SE::Account.new("11000000007").normalize.
|
150
|
+
account = expect(BankTools::SE::Account.new("11000000007").normalize).to eq("1100-0000007")
|
151
151
|
end
|
152
152
|
|
153
153
|
it "should keep any Swedbank/Sparbanker clearing checksum" do
|
154
|
-
BankTools::SE::Account.new("8000-2-0000000000").normalize.
|
154
|
+
expect(BankTools::SE::Account.new("8000-2-0000000000").normalize).to eq("8000-2-0000000000")
|
155
155
|
end
|
156
156
|
|
157
157
|
it "should not attempt to normalize invalid numbers" do
|
158
|
-
account = BankTools::SE::Account.new(" 1-2-3 ").normalize.
|
158
|
+
account = expect(BankTools::SE::Account.new(" 1-2-3 ").normalize).to eq(" 1-2-3 ")
|
159
159
|
end
|
160
160
|
|
161
161
|
it "should prepend zeroes to the serial number if necessary" do
|
162
|
-
BankTools::SE::Account.new("8000-2-80000003").normalize.
|
163
|
-
BankTools::SE::Account.new("8000-2-8000000003").normalize.
|
162
|
+
expect(BankTools::SE::Account.new("8000-2-80000003").normalize).to eq("8000-2-0080000003")
|
163
|
+
expect(BankTools::SE::Account.new("8000-2-8000000003").normalize).to eq("8000-2-8000000003")
|
164
164
|
end
|
165
165
|
end
|
166
166
|
end
|
data/spec/bankgiro_spec.rb
CHANGED
@@ -3,20 +3,20 @@ require "banktools-se"
|
|
3
3
|
|
4
4
|
describe BankTools::SE::Bankgiro do
|
5
5
|
it "should initialize" do
|
6
|
-
BankTools::SE::Bankgiro.new("foo").
|
6
|
+
expect(BankTools::SE::Bankgiro.new("foo")).to be_a(BankTools::SE::Bankgiro)
|
7
7
|
end
|
8
8
|
|
9
9
|
describe "#valid?" do
|
10
10
|
it "should be true with no errors" do
|
11
11
|
account = BankTools::SE::Bankgiro.new("foo")
|
12
|
-
account.
|
13
|
-
account.
|
12
|
+
allow(account).to receive(:errors).and_return([])
|
13
|
+
expect(account).to be_valid
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should be false with errors" do
|
17
17
|
account = BankTools::SE::Bankgiro.new("foo")
|
18
|
-
account.
|
19
|
-
account.
|
18
|
+
allow(account).to receive(:errors).and_return([:error])
|
19
|
+
expect(account).not_to be_valid
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
@@ -28,59 +28,59 @@ describe BankTools::SE::Bankgiro do
|
|
28
28
|
"640-5070",
|
29
29
|
].each do |number|
|
30
30
|
it "should be empty for a valid number like #{number}" do
|
31
|
-
BankTools::SE::Bankgiro.new(number).errors.
|
31
|
+
expect(BankTools::SE::Bankgiro.new(number).errors).to be_empty
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
35
|
it "should include :too_short for numbers shorter than 7 digits" do
|
36
|
-
BankTools::SE::Bankgiro.new(nil).errors.
|
37
|
-
BankTools::SE::Bankgiro.new("").errors.
|
38
|
-
BankTools::SE::Bankgiro.new("54-0296").errors.
|
39
|
-
BankTools::SE::Bankgiro.new("54---------0296").errors.
|
36
|
+
expect(BankTools::SE::Bankgiro.new(nil).errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
37
|
+
expect(BankTools::SE::Bankgiro.new("").errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
38
|
+
expect(BankTools::SE::Bankgiro.new("54-0296").errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
39
|
+
expect(BankTools::SE::Bankgiro.new("54---------0296").errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
40
40
|
end
|
41
41
|
|
42
42
|
it "should include :too_long for numbers longer than 8 digits" do
|
43
|
-
BankTools::SE::Bankgiro.new("5402-96810").errors.
|
43
|
+
expect(BankTools::SE::Bankgiro.new("5402-96810").errors).to include(BankTools::SE::Errors::TOO_LONG)
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should include :invalid_characters for numbers with other character than digits, spaces and dashes" do
|
47
|
-
BankTools::SE::Bankgiro.new("5402-9681X").errors.
|
48
|
-
BankTools::SE::Bankgiro.new(" 5 4 0 2 - 9 6 8 1 ").errors.
|
47
|
+
expect(BankTools::SE::Bankgiro.new("5402-9681X").errors).to include(BankTools::SE::Errors::INVALID_CHARACTERS)
|
48
|
+
expect(BankTools::SE::Bankgiro.new(" 5 4 0 2 - 9 6 8 1 ").errors).not_to include(BankTools::SE::Errors::INVALID_CHARACTERS)
|
49
49
|
end
|
50
50
|
|
51
51
|
it "should include :bad_checksum if the Luhn/mod 10 checksum is incorrect" do
|
52
|
-
BankTools::SE::Bankgiro.new("5402-9682").errors.
|
52
|
+
expect(BankTools::SE::Bankgiro.new("5402-9682").errors).to include(BankTools::SE::Errors::BAD_CHECKSUM)
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
describe "#normalize" do
|
57
57
|
it "should normalize 7-digit numbers to NNN-NNNN" do
|
58
58
|
account = BankTools::SE::Bankgiro.new(" 6-40 - 5070")
|
59
|
-
account.normalize.
|
59
|
+
expect(account.normalize).to eq("640-5070")
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should normalize 8-digit numbers to NNNN-NNNN" do
|
63
63
|
account = BankTools::SE::Bankgiro.new(" 5-4-0-2-9-6- 81- ")
|
64
|
-
account.normalize.
|
64
|
+
expect(account.normalize).to eq("5402-9681")
|
65
65
|
end
|
66
66
|
|
67
67
|
it "should not attempt to normalize invalid numbers" do
|
68
68
|
account = BankTools::SE::Bankgiro.new(" 1-2-3 ")
|
69
|
-
account.normalize.
|
69
|
+
expect(account.normalize).to eq(" 1-2-3 ")
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
73
|
describe "#fundraising? (90-konto)" do
|
74
74
|
it "should be true for the number series 900-nnnn to 904-nnnn" do
|
75
|
-
BankTools::SE::Bankgiro.new("902-0033").
|
75
|
+
expect(BankTools::SE::Bankgiro.new("902-0033")).to be_fundraising
|
76
76
|
end
|
77
77
|
|
78
78
|
it "should be false for invalid numbers in the right series" do
|
79
|
-
BankTools::SE::Bankgiro.new("902-0034").
|
79
|
+
expect(BankTools::SE::Bankgiro.new("902-0034")).not_to be_fundraising
|
80
80
|
end
|
81
81
|
|
82
82
|
it "should be false for numbers outside the right series" do
|
83
|
-
BankTools::SE::Bankgiro.new("5402-9681").
|
83
|
+
expect(BankTools::SE::Bankgiro.new("5402-9681")).not_to be_fundraising
|
84
84
|
end
|
85
85
|
end
|
86
86
|
end
|
data/spec/ocr_spec.rb
CHANGED
@@ -2,22 +2,22 @@ require "spec_helper"
|
|
2
2
|
require "banktools-se"
|
3
3
|
|
4
4
|
describe BankTools::SE::OCR do
|
5
|
-
#
|
5
|
+
# https://www.bankgirot.se/globalassets/dokument/anvandarmanualer/bankgiroinbetalningar_anvandarmanual_sv_31okt2016.pdf section 5.2
|
6
6
|
describe ".from_number" do
|
7
7
|
it "adds a mod-10 check digit" do
|
8
|
-
BankTools::SE::OCR.from_number("123").
|
8
|
+
expect(BankTools::SE::OCR.from_number("123")).to eq "1230"
|
9
9
|
end
|
10
10
|
|
11
11
|
it "handles integer input" do
|
12
|
-
BankTools::SE::OCR.from_number(123).
|
12
|
+
expect(BankTools::SE::OCR.from_number(123)).to eq "1230"
|
13
13
|
end
|
14
14
|
|
15
15
|
it "can add an optional length digit" do
|
16
|
-
BankTools::SE::OCR.from_number("1234567890", length_digit: true).
|
16
|
+
expect(BankTools::SE::OCR.from_number("1234567890", length_digit: true)).to eq "123456789023"
|
17
17
|
end
|
18
18
|
|
19
19
|
it "can pad the number" do
|
20
|
-
BankTools::SE::OCR.from_number("1234567890", length_digit: true, pad: "0").
|
20
|
+
expect(BankTools::SE::OCR.from_number("1234567890", length_digit: true, pad: "0")).to eq "1234567890037"
|
21
21
|
end
|
22
22
|
|
23
23
|
it "raises if resulting number is > 25 digits" do
|
@@ -31,19 +31,19 @@ describe BankTools::SE::OCR do
|
|
31
31
|
|
32
32
|
describe ".to_number" do
|
33
33
|
it "strips the mod-10 check digit" do
|
34
|
-
BankTools::SE::OCR.to_number("1230").
|
34
|
+
expect(BankTools::SE::OCR.to_number("1230")).to eq "123"
|
35
35
|
end
|
36
36
|
|
37
37
|
it "handles integer input" do
|
38
|
-
BankTools::SE::OCR.to_number(1230).
|
38
|
+
expect(BankTools::SE::OCR.to_number(1230)).to eq "123"
|
39
39
|
end
|
40
40
|
|
41
41
|
it "can strip an optional length digit" do
|
42
|
-
BankTools::SE::OCR.to_number("123456789023", length_digit: true).
|
42
|
+
expect(BankTools::SE::OCR.to_number("123456789023", length_digit: true)).to eq "1234567890"
|
43
43
|
end
|
44
44
|
|
45
45
|
it "strips the given padding" do
|
46
|
-
BankTools::SE::OCR.to_number("1234567890037", length_digit: true, pad: "0").
|
46
|
+
expect(BankTools::SE::OCR.to_number("1234567890037", length_digit: true, pad: "0")).to eq "1234567890"
|
47
47
|
end
|
48
48
|
|
49
49
|
it "raises if the given number is too short to be a valid OCR" do
|
@@ -74,32 +74,25 @@ describe BankTools::SE::OCR do
|
|
74
74
|
|
75
75
|
describe ".find_all_in_string" do
|
76
76
|
it "detects only number sequences that are valid OCRs" do
|
77
|
-
expect(BankTools::SE::OCR.find_all_in_string("1230 1234 4564")).to
|
77
|
+
expect(BankTools::SE::OCR.find_all_in_string("1230 1234 4564")).to include "1230", "4564"
|
78
|
+
expect(BankTools::SE::OCR.find_all_in_string("1230 1234 4564")).not_to include "1234"
|
78
79
|
end
|
79
80
|
|
80
81
|
it "requires OCRs to comply with the specified length_digit and pad options" do
|
81
82
|
string = "1230 4564 123067 456061"
|
82
|
-
expect(BankTools::SE::OCR.find_all_in_string(string)).to
|
83
|
-
|
83
|
+
expect(BankTools::SE::OCR.find_all_in_string(string)).to include "1230", "4564", "123067", "456061"
|
84
|
+
|
85
|
+
expect(BankTools::SE::OCR.find_all_in_string(string, length_digit: true, pad: "0")).to include "123067", "456061"
|
86
|
+
expect(BankTools::SE::OCR.find_all_in_string(string, length_digit: true, pad: "0")).not_to include "1230", "4564"
|
84
87
|
end
|
85
88
|
|
86
89
|
it "finds digits among any non-digit characters" do
|
87
90
|
expect(BankTools::SE::OCR.find_all_in_string("x1230x")).to eq [ "1230" ]
|
88
91
|
end
|
89
92
|
|
90
|
-
it "handles OCR numbers both separated and split by
|
91
|
-
expect(BankTools::SE::OCR.find_all_in_string("
|
92
|
-
expect(BankTools::SE::OCR.find_all_in_string("
|
93
|
-
end
|
94
|
-
|
95
|
-
it "handles OCR numbers both separated and split by semicolons" do
|
96
|
-
expect(BankTools::SE::OCR.find_all_in_string("1230;4564")).to include "1230", "4564", "12304564"
|
97
|
-
expect(BankTools::SE::OCR.find_all_in_string("45;64")).to eq [ "4564" ]
|
98
|
-
end
|
99
|
-
|
100
|
-
it "handles OCR numbers both separated and split by '.'" do
|
101
|
-
expect(BankTools::SE::OCR.find_all_in_string("1230.4564")).to include "1230", "4564", "12304564"
|
102
|
-
expect(BankTools::SE::OCR.find_all_in_string("45.64")).to eq [ "4564" ]
|
93
|
+
it "handles OCR numbers both separated and split by non-digits" do
|
94
|
+
expect(BankTools::SE::OCR.find_all_in_string("12\n30\n4564")).to include "1230", "4564", "12304564"
|
95
|
+
expect(BankTools::SE::OCR.find_all_in_string("12;30.4564")).to include "1230", "4564", "12304564"
|
103
96
|
end
|
104
97
|
|
105
98
|
it "handles numbers smushed together" do
|
@@ -118,11 +111,13 @@ describe BankTools::SE::OCR do
|
|
118
111
|
end
|
119
112
|
|
120
113
|
it "lets you configure the accepted OCR min_length" do
|
121
|
-
expect(BankTools::SE::OCR.find_all_in_string("12304564")).to
|
122
|
-
|
114
|
+
expect(BankTools::SE::OCR.find_all_in_string("12304564")).to include "1230", "12304564"
|
115
|
+
|
116
|
+
expect(BankTools::SE::OCR.find_all_in_string("12304564", min_length: 6)).to include "12304564"
|
117
|
+
expect(BankTools::SE::OCR.find_all_in_string("12304564", min_length: 6)).not_to include "1230"
|
123
118
|
|
124
119
|
expect(BankTools::SE::OCR.find_all_in_string("1234")).to eq []
|
125
|
-
expect(BankTools::SE::OCR.find_all_in_string("1234", min_length: 2)).to
|
120
|
+
expect(BankTools::SE::OCR.find_all_in_string("1234", min_length: 2)).to include "34"
|
126
121
|
end
|
127
122
|
|
128
123
|
it "lets you configure the accepted OCR max_length" do
|
@@ -138,7 +133,8 @@ describe BankTools::SE::OCR do
|
|
138
133
|
end
|
139
134
|
|
140
135
|
it "excludes duplicates" do
|
141
|
-
|
136
|
+
results = BankTools::SE::OCR.find_all_in_string("1230 1230 4564")
|
137
|
+
expect(results.length).to eq results.uniq.length
|
142
138
|
end
|
143
139
|
end
|
144
140
|
end
|
data/spec/plusgiro_spec.rb
CHANGED
@@ -4,21 +4,21 @@ require "banktools-se"
|
|
4
4
|
describe BankTools::SE::Plusgiro do
|
5
5
|
|
6
6
|
it "should initialize" do
|
7
|
-
BankTools::SE::Plusgiro.new("foo").
|
7
|
+
expect(BankTools::SE::Plusgiro.new("foo")).to be_a(BankTools::SE::Plusgiro)
|
8
8
|
end
|
9
9
|
|
10
10
|
describe "#valid?" do
|
11
11
|
|
12
12
|
it "should be true with no errors" do
|
13
13
|
account = BankTools::SE::Plusgiro.new("foo")
|
14
|
-
account.
|
15
|
-
account.
|
14
|
+
allow(account).to receive(:errors).and_return([])
|
15
|
+
expect(account).to be_valid
|
16
16
|
end
|
17
17
|
|
18
18
|
it "should be false with errors" do
|
19
19
|
account = BankTools::SE::Plusgiro.new("foo")
|
20
|
-
account.
|
21
|
-
account.
|
20
|
+
allow(account).to receive(:errors).and_return([:error])
|
21
|
+
expect(account).not_to be_valid
|
22
22
|
end
|
23
23
|
|
24
24
|
end
|
@@ -30,28 +30,28 @@ describe BankTools::SE::Plusgiro do
|
|
30
30
|
"4-2", # Sveriges riksbank
|
31
31
|
].each do |number|
|
32
32
|
it "should be empty for a valid number like #{number}" do
|
33
|
-
BankTools::SE::Plusgiro.new(number).errors.
|
33
|
+
expect(BankTools::SE::Plusgiro.new(number).errors).to be_empty
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
it "should include :too_short for numbers shorter than 2 digits" do
|
38
|
-
BankTools::SE::Plusgiro.new(nil).errors.
|
39
|
-
BankTools::SE::Plusgiro.new("").errors.
|
40
|
-
BankTools::SE::Plusgiro.new("1").errors.
|
41
|
-
BankTools::SE::Plusgiro.new("1---------").errors.
|
38
|
+
expect(BankTools::SE::Plusgiro.new(nil).errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
39
|
+
expect(BankTools::SE::Plusgiro.new("").errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
40
|
+
expect(BankTools::SE::Plusgiro.new("1").errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
41
|
+
expect(BankTools::SE::Plusgiro.new("1---------").errors).to include(BankTools::SE::Errors::TOO_SHORT)
|
42
42
|
end
|
43
43
|
|
44
44
|
it "should include :too_long for numbers longer than 8 digits" do
|
45
|
-
BankTools::SE::Plusgiro.new("410 54 68-51").errors.
|
45
|
+
expect(BankTools::SE::Plusgiro.new("410 54 68-51").errors).to include(BankTools::SE::Errors::TOO_LONG)
|
46
46
|
end
|
47
47
|
|
48
48
|
it "should include :invalid_characters for numbers with other character than digits, spaces and dashes" do
|
49
|
-
BankTools::SE::Plusgiro.new("410 54 68-5X").errors.
|
50
|
-
BankTools::SE::Plusgiro.new("4 1 0 5 4 6 8 - 5 ").errors.
|
49
|
+
expect(BankTools::SE::Plusgiro.new("410 54 68-5X").errors).to include(BankTools::SE::Errors::INVALID_CHARACTERS)
|
50
|
+
expect(BankTools::SE::Plusgiro.new("4 1 0 5 4 6 8 - 5 ").errors).not_to include(BankTools::SE::Errors::INVALID_CHARACTERS)
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should include :bad_checksum if the Luhn/mod 10 checksum is incorrect" do
|
54
|
-
BankTools::SE::Plusgiro.new("410 54 68-6").errors.
|
54
|
+
expect(BankTools::SE::Plusgiro.new("410 54 68-6").errors).to include(BankTools::SE::Errors::BAD_CHECKSUM)
|
55
55
|
end
|
56
56
|
|
57
57
|
end
|
@@ -60,22 +60,22 @@ describe BankTools::SE::Plusgiro do
|
|
60
60
|
|
61
61
|
it "should normalize short numbers to the format N-N" do
|
62
62
|
account = BankTools::SE::Plusgiro.new(" 4 - 2")
|
63
|
-
account.normalize.
|
63
|
+
expect(account.normalize).to eq("4-2")
|
64
64
|
end
|
65
65
|
|
66
66
|
it "should normalize odd-length numbers to the format NN NN NN-N" do
|
67
67
|
account = BankTools::SE::Plusgiro.new("2865434")
|
68
|
-
account.normalize.
|
68
|
+
expect(account.normalize).to eq("28 65 43-4")
|
69
69
|
end
|
70
70
|
|
71
71
|
it "should normalize even-length numbers to the format NNN NN NN-N" do
|
72
72
|
account = BankTools::SE::Plusgiro.new("41054685")
|
73
|
-
account.normalize.
|
73
|
+
expect(account.normalize).to eq("410 54 68-5")
|
74
74
|
end
|
75
75
|
|
76
76
|
it "should not attempt to normalize invalid numbers" do
|
77
77
|
account = BankTools::SE::Plusgiro.new(" 1-2-3 ")
|
78
|
-
account.normalize.
|
78
|
+
expect(account.normalize).to eq(" 1-2-3 ")
|
79
79
|
end
|
80
80
|
|
81
81
|
end
|
@@ -83,15 +83,15 @@ describe BankTools::SE::Plusgiro do
|
|
83
83
|
describe "#fundraising? (90-konto)" do
|
84
84
|
|
85
85
|
it "should be true for the number series 90-nnnn" do
|
86
|
-
BankTools::SE::Plusgiro.new("90 20 03-3").
|
86
|
+
expect(BankTools::SE::Plusgiro.new("90 20 03-3")).to be_fundraising
|
87
87
|
end
|
88
88
|
|
89
89
|
it "should be false for invalid numbers in the right series" do
|
90
|
-
BankTools::SE::Plusgiro.new("90 20 03-4").
|
90
|
+
expect(BankTools::SE::Plusgiro.new("90 20 03-4")).not_to be_fundraising
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should be false for numbers outside the right series" do
|
94
|
-
BankTools::SE::Plusgiro.new("4-2").
|
94
|
+
expect(BankTools::SE::Plusgiro.new("4-2")).not_to be_fundraising
|
95
95
|
end
|
96
96
|
|
97
97
|
end
|
data/spec/spec_helper.rb
CHANGED
data/spec/utils_spec.rb
CHANGED
@@ -19,7 +19,7 @@ describe BankTools::SE::Utils, "valid_luhn?" do
|
|
19
19
|
"5402-9681",
|
20
20
|
].each do |number|
|
21
21
|
it "should allow a valid number like #{number}" do
|
22
|
-
BankTools::SE::Utils.valid_luhn?(number).
|
22
|
+
expect(BankTools::SE::Utils.valid_luhn?(number)).to eq(true)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -29,7 +29,7 @@ describe BankTools::SE::Utils, "valid_luhn?" do
|
|
29
29
|
"5402-9682",
|
30
30
|
].each do |number|
|
31
31
|
it "should disallow an invalid number like #{number}" do
|
32
|
-
BankTools::SE::Utils.valid_luhn?(number).
|
32
|
+
expect(BankTools::SE::Utils.valid_luhn?(number)).to eq(false)
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: banktools-se
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-09-
|
11
|
+
date: 2016-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|