banktools-se 0.4.0 → 0.5.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 +7 -0
- data/lib/banktools-se/bankgiro.rb +37 -2
- data/lib/banktools-se/utils.rb +10 -0
- data/lib/banktools-se/version.rb +1 -1
- data/spec/bankgiro_spec.rb +37 -0
- metadata +1 -2
- data/Guardfile +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56a0412c8e17d142b99162549712b831e282fdd8
|
4
|
+
data.tar.gz: bc6dae711b9df4a89094eae81abf6f148a6092e1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 08a10797c97ab40fae64930595cac729b92dea89762fec47fe90914f7d84735acad17e920322c09a783cafc89847c255ba462b222f24aebcc9aef8f33a5a5cd5
|
7
|
+
data.tar.gz: 27fa5ab9c2585e3a410333ac9722ab3bb1a2f235d285af2863d8495fc1b47710231c611972961572d178d5bafa69cae86d44682ec47669cd14e31ce8dff12289
|
data/README.markdown
CHANGED
@@ -44,6 +44,13 @@ to install it.
|
|
44
44
|
fundraising_account = BankTools::SE::Bankgiro.new("902-0033")
|
45
45
|
fundraising_account.fundraising? # => true
|
46
46
|
|
47
|
+
# OCR
|
48
|
+
BankTools::SE::Bankgiro.number_to_ocr("123") # => "1230"
|
49
|
+
BankTools::SE::Bankgiro.number_to_ocr("123", length_digit: true) # => "12351"
|
50
|
+
BankTools::SE::Bankgiro.number_to_ocr("123", length_digit: true, pad: "0") # => "123067"
|
51
|
+
BankTools::SE::Bankgiro.number_from_ocr("1230") # => "123"
|
52
|
+
BankTools::SE::Bankgiro.number_from_ocr("123067", length_digit: true, pad: "0") # => "123"
|
53
|
+
|
47
54
|
|
48
55
|
# Plusgiro
|
49
56
|
|
@@ -1,11 +1,46 @@
|
|
1
|
+
# http://sv.wikipedia.org/wiki/Bankgirot#Bankgironummer
|
2
|
+
|
1
3
|
module BankTools
|
2
4
|
module SE
|
3
5
|
class Bankgiro
|
4
|
-
|
5
|
-
|
6
|
+
class OverlongOCR < StandardError; end
|
7
|
+
class BadCheckDigit < StandardError; end
|
6
8
|
|
7
9
|
attr_reader :number
|
8
10
|
|
11
|
+
# http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
|
12
|
+
def self.number_to_ocr(number, opts = {})
|
13
|
+
add_length_digit = opts.fetch(:length_digit, false)
|
14
|
+
pad = opts.fetch(:pad, nil)
|
15
|
+
|
16
|
+
number = number.to_s
|
17
|
+
|
18
|
+
if pad
|
19
|
+
number += pad
|
20
|
+
end
|
21
|
+
|
22
|
+
if add_length_digit
|
23
|
+
# Adding 2: 1 length digit, 1 check digit
|
24
|
+
number += ((number.length + 2) % 10).to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
number_with_ocr = number.to_s + Utils.luhn_checksum(number).to_s
|
28
|
+
raise OverlongOCR, "Bankgiro OCR must be 2-25 characters" if number_with_ocr.length > 25
|
29
|
+
number_with_ocr
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.number_from_ocr(number, opts = {})
|
33
|
+
strip_length_digit = opts.fetch(:length_digit, false)
|
34
|
+
strip_padding = opts.fetch(:pad, false)
|
35
|
+
|
36
|
+
raise BadCheckDigit unless Utils.valid_luhn?(number)
|
37
|
+
|
38
|
+
number = number.to_s
|
39
|
+
number.slice!(-2) if strip_length_digit
|
40
|
+
number.slice!(-strip_padding.length.succ..-2) if strip_padding
|
41
|
+
number.chop
|
42
|
+
end
|
43
|
+
|
9
44
|
def initialize(number)
|
10
45
|
@number = number
|
11
46
|
end
|
data/lib/banktools-se/utils.rb
CHANGED
@@ -13,6 +13,16 @@ module BankTools
|
|
13
13
|
sum % 10 == 0
|
14
14
|
end
|
15
15
|
|
16
|
+
def self.luhn_checksum(number)
|
17
|
+
digits = number.to_s.scan(/\d/).reverse.map { |x| x.to_i }
|
18
|
+
digits = digits.each_with_index.map { |d, i|
|
19
|
+
d *= 2 if i.even?
|
20
|
+
d > 9 ? d - 9 : d
|
21
|
+
}
|
22
|
+
sum = digits.inject(0) { |m, x| m + x }
|
23
|
+
mod = 10 - sum % 10
|
24
|
+
mod == 10 ? 0 : mod
|
25
|
+
end
|
16
26
|
end
|
17
27
|
end
|
18
28
|
end
|
data/lib/banktools-se/version.rb
CHANGED
data/spec/bankgiro_spec.rb
CHANGED
@@ -83,4 +83,41 @@ 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 "can add an optional length digit" do
|
94
|
+
BankTools::SE::Bankgiro.number_to_ocr("1234567890", length_digit: true).should eq "123456789023"
|
95
|
+
end
|
96
|
+
|
97
|
+
it "can pad the number" do
|
98
|
+
BankTools::SE::Bankgiro.number_to_ocr("1234567890", length_digit: true, pad: "0").should eq "1234567890037"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "raises if resulting number is > 25 digits" do
|
102
|
+
expect { BankTools::SE::Bankgiro.number_to_ocr("1234567890123456789012345") }.to raise_error(BankTools::SE::Bankgiro::OverlongOCR)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
describe ".number_from_ocr" do
|
107
|
+
it "strips the mod-10 check digit" do
|
108
|
+
BankTools::SE::Bankgiro.number_from_ocr("1230").should eq "123"
|
109
|
+
end
|
110
|
+
|
111
|
+
it "can strip an optional length digit" do
|
112
|
+
BankTools::SE::Bankgiro.number_from_ocr("123456789023", length_digit: true).should eq "1234567890"
|
113
|
+
end
|
114
|
+
|
115
|
+
it "can pad the number" do
|
116
|
+
BankTools::SE::Bankgiro.number_from_ocr("1234567890037", length_digit: true, pad: "0").should eq "1234567890"
|
117
|
+
end
|
118
|
+
|
119
|
+
it "raises if check digit is wrong" do
|
120
|
+
expect { BankTools::SE::Bankgiro.number_from_ocr("1231") }.to raise_error(BankTools::SE::Bankgiro::BadCheckDigit)
|
121
|
+
end
|
122
|
+
end
|
86
123
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: banktools-se
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henrik Nyh
|
@@ -76,7 +76,6 @@ files:
|
|
76
76
|
- .gitignore
|
77
77
|
- .travis.yml
|
78
78
|
- Gemfile
|
79
|
-
- Guardfile
|
80
79
|
- README.markdown
|
81
80
|
- Rakefile
|
82
81
|
- banktools-se.gemspec
|