banktools-se 0.11.0 → 1.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 +6 -7
- data/lib/banktools-se/bankgiro.rb +0 -11
- data/lib/banktools-se/ocr.rb +51 -0
- data/lib/banktools-se/version.rb +1 -1
- data/lib/banktools-se.rb +1 -0
- data/spec/bankgiro_spec.rb +0 -53
- data/spec/ocr_spec.rb +57 -0
- metadata +6 -4
- data/lib/banktools-se/bankgiro/ocr.rb +0 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6e034f74096ed867627484354817c11aba890bb
|
4
|
+
data.tar.gz: 0418f31cf0df526976031a3cbeee701f59fc0a58
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d53bc1373a91f12a8a65ea3b2e09734f0e6e23fa25a16af4c63fd6b5a577919b955b18c60327ef8d5b20ba828e6826a287dbdce77b481da71bf5b9f409365270
|
7
|
+
data.tar.gz: b1babe91af17f1029af9d71fea57cf0fb6819e8bcab2922c0579169a68a84ed575bc005b279d3eba4e22c3653af1c2fb1ce72325f58c16318d49ae4e9d3197b1
|
data/README.markdown
CHANGED
@@ -8,7 +8,7 @@ Ruby gem to validate, normalize/prettify and to some extent interpret
|
|
8
8
|
* Swedish plusgiro numbers
|
9
9
|
* Swedish bankgiro numbers
|
10
10
|
|
11
|
-
It can also generate and unpack
|
11
|
+
It can also generate and unpack payment OCR numbers (reference codes) with a check digit, optional length digit and optional padding digits.
|
12
12
|
|
13
13
|
This gem does what it can to weed out invalid numbers but errs on the side of allowing too much, in the absence of good specifications, so be advised that a "valid" number might still be incorrect.
|
14
14
|
|
@@ -34,12 +34,11 @@ Inspired by [iulianu/iban-tools](https://github.com/iulianu/iban-tools). Please
|
|
34
34
|
fundraising_account.fundraising? # => true
|
35
35
|
|
36
36
|
# OCR
|
37
|
-
BankTools::SE::
|
38
|
-
BankTools::SE::
|
39
|
-
BankTools::SE::
|
40
|
-
BankTools::SE::
|
41
|
-
BankTools::SE::
|
42
|
-
|
37
|
+
BankTools::SE::OCR.from_number("123") # => "1230"
|
38
|
+
BankTools::SE::OCR.from_number("123", length_digit: true) # => "12351"
|
39
|
+
BankTools::SE::OCR.from_number("123", length_digit: true, pad: "0") # => "123067"
|
40
|
+
BankTools::SE::OCR.to_number("1230") # => "123"
|
41
|
+
BankTools::SE::OCR.to_number("123067", length_digit: true, pad: "0") # => "123"
|
43
42
|
|
44
43
|
# Plusgiro
|
45
44
|
|
@@ -1,20 +1,9 @@
|
|
1
1
|
# http://sv.wikipedia.org/wiki/Bankgirot#Bankgironummer
|
2
|
-
|
3
|
-
require "banktools-se/bankgiro/ocr"
|
4
|
-
|
5
2
|
module BankTools
|
6
3
|
module SE
|
7
4
|
class Bankgiro
|
8
5
|
attr_reader :number
|
9
6
|
|
10
|
-
def self.number_to_ocr(number, opts = {})
|
11
|
-
OCR.number_to_ocr(number, opts)
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.number_from_ocr(number, opts = {})
|
15
|
-
OCR.number_from_ocr(number, opts)
|
16
|
-
end
|
17
|
-
|
18
7
|
def initialize(number)
|
19
8
|
@number = number
|
20
9
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
# http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
|
2
|
+
|
3
|
+
module BankTools
|
4
|
+
module SE
|
5
|
+
class OCR
|
6
|
+
class InvalidOCR < StandardError; end
|
7
|
+
class OverlongOCR < InvalidOCR; end
|
8
|
+
class BadChecksum < InvalidOCR; end
|
9
|
+
class MustBeNumeric < InvalidOCR; end
|
10
|
+
|
11
|
+
MIN_LENGTH = 2
|
12
|
+
MAX_LENGTH = 25
|
13
|
+
|
14
|
+
def self.from_number(number, opts = {})
|
15
|
+
number = number.to_s
|
16
|
+
add_length_digit = opts.fetch(:length_digit, false)
|
17
|
+
pad = opts.fetch(:pad, "").to_s
|
18
|
+
|
19
|
+
raise MustBeNumeric unless number.match(/\A\d+\z/)
|
20
|
+
# Padding isn't something BGC specifies, but we needed it to support a legacy scheme.
|
21
|
+
number += pad
|
22
|
+
# Adding 2: 1 length digit, 1 check digit.
|
23
|
+
number += ((number.length + 2) % 10).to_s if add_length_digit
|
24
|
+
|
25
|
+
number_with_ocr = number + Utils.luhn_checksum(number).to_s
|
26
|
+
|
27
|
+
length = number_with_ocr.length
|
28
|
+
if length > MAX_LENGTH
|
29
|
+
raise OverlongOCR, "Bankgiro OCR must be #{MIN_LENGTH} - #{MAX_LENGTH} characters (this one would be #{length} characters)"
|
30
|
+
end
|
31
|
+
|
32
|
+
number_with_ocr
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.to_number(number, opts = {})
|
36
|
+
number = number.to_s
|
37
|
+
strip_length_digit = opts.fetch(:length_digit, false)
|
38
|
+
strip_padding = opts.fetch(:pad, "").to_s
|
39
|
+
|
40
|
+
raise MustBeNumeric unless number.match(/\A\d+\z/)
|
41
|
+
raise BadChecksum unless Utils.valid_luhn?(number)
|
42
|
+
|
43
|
+
digits_to_chop = 1 # Checksum.
|
44
|
+
digits_to_chop += 1 if strip_length_digit
|
45
|
+
digits_to_chop += strip_padding.length
|
46
|
+
|
47
|
+
number[0...-digits_to_chop]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
data/lib/banktools-se/version.rb
CHANGED
data/lib/banktools-se.rb
CHANGED
data/spec/bankgiro_spec.rb
CHANGED
@@ -83,57 +83,4 @@ describe BankTools::SE::Bankgiro do
|
|
83
83
|
BankTools::SE::Bankgiro.new("5402-9681").should_not be_fundraising
|
84
84
|
end
|
85
85
|
end
|
86
|
-
|
87
|
-
# http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
|
88
|
-
describe ".number_to_ocr" do
|
89
|
-
it "adds a mod-10 check digit" do
|
90
|
-
BankTools::SE::Bankgiro.number_to_ocr("123").should eq "1230"
|
91
|
-
end
|
92
|
-
|
93
|
-
it "handles integer input" do
|
94
|
-
BankTools::SE::Bankgiro.number_to_ocr(123).should eq "1230"
|
95
|
-
end
|
96
|
-
|
97
|
-
it "can add an optional length digit" do
|
98
|
-
BankTools::SE::Bankgiro.number_to_ocr("1234567890", length_digit: true).should eq "123456789023"
|
99
|
-
end
|
100
|
-
|
101
|
-
it "can pad the number" do
|
102
|
-
BankTools::SE::Bankgiro.number_to_ocr("1234567890", length_digit: true, pad: "0").should eq "1234567890037"
|
103
|
-
end
|
104
|
-
|
105
|
-
it "raises if resulting number is > 25 digits" do
|
106
|
-
expect { BankTools::SE::Bankgiro.number_to_ocr("1234567890123456789012345") }.to raise_error(BankTools::SE::Bankgiro::OverlongOCR)
|
107
|
-
end
|
108
|
-
|
109
|
-
it "raises if input is non-numeric" do
|
110
|
-
expect { BankTools::SE::Bankgiro.number_to_ocr("garbage") }.to raise_error(BankTools::SE::Bankgiro::MustBeNumeric)
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
describe ".number_from_ocr" do
|
115
|
-
it "strips the mod-10 check digit" do
|
116
|
-
BankTools::SE::Bankgiro.number_from_ocr("1230").should eq "123"
|
117
|
-
end
|
118
|
-
|
119
|
-
it "handles integer input" do
|
120
|
-
BankTools::SE::Bankgiro.number_from_ocr(1230).should eq "123"
|
121
|
-
end
|
122
|
-
|
123
|
-
it "can strip an optional length digit" do
|
124
|
-
BankTools::SE::Bankgiro.number_from_ocr("123456789023", length_digit: true).should eq "1234567890"
|
125
|
-
end
|
126
|
-
|
127
|
-
it "can pad the number" do
|
128
|
-
BankTools::SE::Bankgiro.number_from_ocr("1234567890037", length_digit: true, pad: "0").should eq "1234567890"
|
129
|
-
end
|
130
|
-
|
131
|
-
it "raises if checksum is wrong" do
|
132
|
-
expect { BankTools::SE::Bankgiro.number_from_ocr("1231") }.to raise_error(BankTools::SE::Bankgiro::BadChecksum)
|
133
|
-
end
|
134
|
-
|
135
|
-
it "raises if input is non-numeric" do
|
136
|
-
expect { BankTools::SE::Bankgiro.number_from_ocr("garbage") }.to raise_error(BankTools::SE::Bankgiro::MustBeNumeric)
|
137
|
-
end
|
138
|
-
end
|
139
86
|
end
|
data/spec/ocr_spec.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "banktools-se"
|
3
|
+
|
4
|
+
describe BankTools::SE::OCR do
|
5
|
+
# http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
|
6
|
+
describe ".from_number" do
|
7
|
+
it "adds a mod-10 check digit" do
|
8
|
+
BankTools::SE::OCR.from_number("123").should eq "1230"
|
9
|
+
end
|
10
|
+
|
11
|
+
it "handles integer input" do
|
12
|
+
BankTools::SE::OCR.from_number(123).should eq "1230"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "can add an optional length digit" do
|
16
|
+
BankTools::SE::OCR.from_number("1234567890", length_digit: true).should eq "123456789023"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "can pad the number" do
|
20
|
+
BankTools::SE::OCR.from_number("1234567890", length_digit: true, pad: "0").should eq "1234567890037"
|
21
|
+
end
|
22
|
+
|
23
|
+
it "raises if resulting number is > 25 digits" do
|
24
|
+
expect { BankTools::SE::OCR.from_number("1234567890123456789012345") }.to raise_error(BankTools::SE::OCR::OverlongOCR)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "raises if input is non-numeric" do
|
28
|
+
expect { BankTools::SE::OCR.from_number("garbage") }.to raise_error(BankTools::SE::OCR::MustBeNumeric)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe ".to_number" do
|
33
|
+
it "strips the mod-10 check digit" do
|
34
|
+
BankTools::SE::OCR.to_number("1230").should eq "123"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "handles integer input" do
|
38
|
+
BankTools::SE::OCR.to_number(1230).should eq "123"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "can strip an optional length digit" do
|
42
|
+
BankTools::SE::OCR.to_number("123456789023", length_digit: true).should eq "1234567890"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "can pad the number" do
|
46
|
+
BankTools::SE::OCR.to_number("1234567890037", length_digit: true, pad: "0").should eq "1234567890"
|
47
|
+
end
|
48
|
+
|
49
|
+
it "raises if checksum is wrong" do
|
50
|
+
expect { BankTools::SE::OCR.to_number("1231") }.to raise_error(BankTools::SE::OCR::BadChecksum)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "raises if input is non-numeric" do
|
54
|
+
expect { BankTools::SE::OCR.to_number("garbage") }.to raise_error(BankTools::SE::OCR::MustBeNumeric)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
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: 0.
|
4
|
+
version: 1.0.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:
|
11
|
+
date: 2016-09-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -83,13 +83,14 @@ files:
|
|
83
83
|
- lib/banktools-se/account.rb
|
84
84
|
- lib/banktools-se/account/clearing_number.rb
|
85
85
|
- lib/banktools-se/bankgiro.rb
|
86
|
-
- lib/banktools-se/bankgiro/ocr.rb
|
87
86
|
- lib/banktools-se/errors.rb
|
87
|
+
- lib/banktools-se/ocr.rb
|
88
88
|
- lib/banktools-se/plusgiro.rb
|
89
89
|
- lib/banktools-se/utils.rb
|
90
90
|
- lib/banktools-se/version.rb
|
91
91
|
- spec/account_spec.rb
|
92
92
|
- spec/bankgiro_spec.rb
|
93
|
+
- spec/ocr_spec.rb
|
93
94
|
- spec/plusgiro_spec.rb
|
94
95
|
- spec/spec_helper.rb
|
95
96
|
- spec/utils_spec.rb
|
@@ -112,13 +113,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
112
113
|
version: '0'
|
113
114
|
requirements: []
|
114
115
|
rubyforge_project: banktools-se
|
115
|
-
rubygems_version: 2.2.
|
116
|
+
rubygems_version: 2.2.1
|
116
117
|
signing_key:
|
117
118
|
specification_version: 4
|
118
119
|
summary: Validate and normalize Swedish bank account numbers, plusgiro and bankgiro.
|
119
120
|
test_files:
|
120
121
|
- spec/account_spec.rb
|
121
122
|
- spec/bankgiro_spec.rb
|
123
|
+
- spec/ocr_spec.rb
|
122
124
|
- spec/plusgiro_spec.rb
|
123
125
|
- spec/spec_helper.rb
|
124
126
|
- spec/utils_spec.rb
|
@@ -1,53 +0,0 @@
|
|
1
|
-
# http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
|
2
|
-
|
3
|
-
module BankTools
|
4
|
-
module SE
|
5
|
-
class Bankgiro
|
6
|
-
class InvalidOCR < StandardError; end
|
7
|
-
class OverlongOCR < InvalidOCR; end
|
8
|
-
class BadChecksum < InvalidOCR; end
|
9
|
-
class MustBeNumeric < InvalidOCR; end
|
10
|
-
|
11
|
-
class OCR
|
12
|
-
MIN_LENGTH = 2
|
13
|
-
MAX_LENGTH = 25
|
14
|
-
|
15
|
-
def self.number_to_ocr(number, opts = {})
|
16
|
-
number = number.to_s
|
17
|
-
add_length_digit = opts.fetch(:length_digit, false)
|
18
|
-
pad = opts.fetch(:pad, "").to_s
|
19
|
-
|
20
|
-
raise MustBeNumeric unless number.match(/\A\d+\z/)
|
21
|
-
# Padding isn't something BGC specifies, but we needed it to support a legacy scheme.
|
22
|
-
number += pad
|
23
|
-
# Adding 2: 1 length digit, 1 check digit.
|
24
|
-
number += ((number.length + 2) % 10).to_s if add_length_digit
|
25
|
-
|
26
|
-
number_with_ocr = number + Utils.luhn_checksum(number).to_s
|
27
|
-
|
28
|
-
length = number_with_ocr.length
|
29
|
-
if length > MAX_LENGTH
|
30
|
-
raise OverlongOCR, "Bankgiro OCR must be #{MIN_LENGTH} - #{MAX_LENGTH} characters (this one would be #{length} characters)"
|
31
|
-
end
|
32
|
-
|
33
|
-
number_with_ocr
|
34
|
-
end
|
35
|
-
|
36
|
-
def self.number_from_ocr(number, opts = {})
|
37
|
-
number = number.to_s
|
38
|
-
strip_length_digit = opts.fetch(:length_digit, false)
|
39
|
-
strip_padding = opts.fetch(:pad, "").to_s
|
40
|
-
|
41
|
-
raise MustBeNumeric unless number.match(/\A\d+\z/)
|
42
|
-
raise BadChecksum unless Utils.valid_luhn?(number)
|
43
|
-
|
44
|
-
digits_to_chop = 1 # Checksum.
|
45
|
-
digits_to_chop += 1 if strip_length_digit
|
46
|
-
digits_to_chop += strip_padding.length
|
47
|
-
|
48
|
-
number[0...-digits_to_chop]
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|