banktools-de 0.0.2 → 0.0.3

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: 010b6ad5764da47d0eee569b99a8494b4ea73d52
4
- data.tar.gz: e02e69d013380a473866a788a04013831d8d775a
3
+ metadata.gz: 89114d9d963ca0aaba7ed20b2347965289b4c46e
4
+ data.tar.gz: bb9412ad2010aa834351d0ec94f4663c950ae1a3
5
5
  SHA512:
6
- metadata.gz: 76cac880128d73863d1d75a8d3eecb4db6f1121f06268b34a298504b3b8974312214334b9e4ef8b67be3cf0404f92c85927fabe8bf9b847fa133d10c0f3367dd
7
- data.tar.gz: 9d9fc8c51b69eac2fcadf788caf721eaf39f65cc37156366c084d33bf5101518a44cd0c442c48420af0696002d24f909f5db6cbdd4fb41cc462b6e0316f320c5
6
+ metadata.gz: e686988439bb9ef324ded10e87c8d9639db2d97828320ea36a6deb5fac4453c0300e2ddbf9214c814eab261ae0c9dad5e286c0785364aa9e73406d83ec6a3704
7
+ data.tar.gz: d2a368d77d104022ab247d5cb21b50cbd62ce0b7b2b85e7cb61b1964be62e43e717ed0e43b082e8937b19366ec23ee4f20987c89434033c81367733e377812cf
data/README.md CHANGED
@@ -19,9 +19,21 @@ If we got anything wrong, please file an issue or contribute a fix yourself.
19
19
  blz.errors # => []
20
20
 
21
21
  bad_blz = BankTools::DE::BLZ.new("1X")
22
+ bad_blz.normalize # => "1X"
22
23
  bad_blz.valid? # => false
23
24
  bad_blz.errors # => [ :too_short, :invalid_characters ]
24
- bad_account.normalize # => "1X"
25
+
26
+ # Account
27
+
28
+ account = BankTools::DE::Account.new("123456789")
29
+ account.normalize # => "123 456 789"
30
+ account.valid? # => true
31
+ account.errors # => []
32
+
33
+ bad_account = BankTools::DE::Account.new("1")
34
+ bad_account.normalize # => "1"
35
+ bad_account.valid? # => false
36
+ bad_account.errors # => [ :too_short ]
25
37
 
26
38
 
27
39
  ## Tests
@@ -0,0 +1,47 @@
1
+ # http://de.wikipedia.org/wiki/Bankleitzahl
2
+ # http://translate.google.com/translate?hl=en&sl=de&tl=en&u=http%3A%2F%2Fde.wikipedia.org%2Fwiki%2FBankleitzahl
3
+
4
+ require "attr_extras"
5
+ require "bank_tools/de/errors"
6
+
7
+ module BankTools
8
+ module DE
9
+ class Account
10
+ # Picked some reasonable values, erring on the side of allowing too much.
11
+ # Seems to claim max 10: http://docs.oracle.com/cd/E18727_01/doc.121/e13483/T359831T498954.htm
12
+ # Seems to claim 2 - 13: http://www.credit-card.be/BankAccount/ValidationRules.htm#DE_Validation
13
+ MIN_LENGTH = 2
14
+ MAX_LENGTH = 15
15
+
16
+ GROUPS_OF = 3
17
+
18
+ pattr_initialize :original_value
19
+
20
+ def normalize
21
+ if valid?
22
+ compacted_value.scan(/\d{#{GROUPS_OF}}|\d+/).join(" ")
23
+ else
24
+ original_value
25
+ end
26
+ end
27
+
28
+ def valid?
29
+ errors.empty?
30
+ end
31
+
32
+ def errors
33
+ errors = []
34
+ errors << Errors::TOO_SHORT if compacted_value.length < MIN_LENGTH
35
+ errors << Errors::TOO_LONG if compacted_value.length > MAX_LENGTH
36
+ errors << Errors::INVALID_CHARACTERS if compacted_value.match(/\D/)
37
+ errors
38
+ end
39
+
40
+ private
41
+
42
+ def compacted_value
43
+ original_value.to_s.gsub(/[\s-]/, "")
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,6 @@
1
+ # http://de.wikipedia.org/wiki/Bankleitzahl
2
+ # http://translate.google.com/translate?hl=en&sl=de&tl=en&u=http%3A%2F%2Fde.wikipedia.org%2Fwiki%2FBankleitzahl
3
+
1
4
  require "attr_extras"
2
5
  require "bank_tools/de/errors"
3
6
 
@@ -1,5 +1,5 @@
1
1
  module BankTools
2
2
  module DE
3
- VERSION = "0.0.2"
3
+ VERSION = "0.0.3"
4
4
  end
5
5
  end
@@ -0,0 +1,57 @@
1
+ require "spec_helper"
2
+ require "bank_tools/de/account"
3
+
4
+ describe BankTools::DE::Account, "#normalize" do
5
+ it "groups the number in threes" do
6
+ expect_normalization "12", "12"
7
+ expect_normalization "123", "123"
8
+ expect_normalization "1234", "123 4"
9
+ expect_normalization "1234567890", "123 456 789 0"
10
+ end
11
+
12
+ it "ignores previous whitespace" do
13
+ expect_normalization " 1 23 4 ", "123 4"
14
+ end
15
+
16
+ it "ignores previous dashes" do
17
+ expect_normalization " 1-2-3-4 ", "123 4"
18
+ end
19
+
20
+ it "leaves a bad format as-is" do
21
+ expect_normalization "hej", "hej"
22
+ end
23
+
24
+ def expect_normalization(from, unto)
25
+ expect(BankTools::DE::Account.new(from).normalize).to eq unto
26
+ end
27
+ end
28
+
29
+ describe BankTools::DE::Account, "#valid?" do
30
+ it "is true with no errors" do
31
+ expect(BankTools::DE::Account.new("12").valid?).to be_true
32
+ end
33
+
34
+ it "is false with errors" do
35
+ expect(BankTools::DE::Account.new("1").valid?).to be_false
36
+ end
37
+ end
38
+
39
+ describe BankTools::DE::Account, "#errors" do
40
+ Errors = BankTools::DE::Errors
41
+
42
+ it "is empty when valid" do
43
+ expect(BankTools::DE::Account.new(" 1-2 ").errors).to be_empty
44
+ end
45
+
46
+ it "includes TOO_SHORT if below 2 characters" do
47
+ expect(BankTools::DE::Account.new("1").errors).to include(Errors::TOO_SHORT)
48
+ end
49
+
50
+ it "includes TOO_LONG if above 15 characters" do
51
+ expect(BankTools::DE::Account.new("1234567890123456").errors).to include(Errors::TOO_LONG)
52
+ end
53
+
54
+ it "includes INVALID_CHARACTERS if there's other things than digits, whitespace and dashes" do
55
+ expect(BankTools::DE::Account.new("1X").errors).to include(Errors::INVALID_CHARACTERS)
56
+ end
57
+ end
data/spec/blz_spec.rb CHANGED
@@ -30,8 +30,8 @@ end
30
30
  describe BankTools::DE::BLZ, "#errors" do
31
31
  Errors = BankTools::DE::Errors
32
32
 
33
- it "is empty with no errors" do
34
- expect(BankTools::DE::BLZ.new("123 456 78").errors).to eq []
33
+ it "is empty if there's 8 digits and possibly whitespace" do
34
+ expect(BankTools::DE::BLZ.new(" 123 456 78 ").errors).to be_empty
35
35
  end
36
36
 
37
37
  it "includes TOO_SHORT if below 8 characters" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: banktools-de
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
@@ -79,9 +79,11 @@ files:
79
79
  - Rakefile
80
80
  - banktools-de.gemspec
81
81
  - lib/bank_tools/de.rb
82
+ - lib/bank_tools/de/account.rb
82
83
  - lib/bank_tools/de/blz.rb
83
84
  - lib/bank_tools/de/errors.rb
84
85
  - lib/bank_tools/de/version.rb
86
+ - spec/account_spec.rb
85
87
  - spec/blz_spec.rb
86
88
  - spec/spec_helper.rb
87
89
  homepage: ''
@@ -109,5 +111,6 @@ signing_key:
109
111
  specification_version: 4
110
112
  summary: Validate and normalize German Bankleitzahl (BLZ) and bank account numbers.
111
113
  test_files:
114
+ - spec/account_spec.rb
112
115
  - spec/blz_spec.rb
113
116
  - spec/spec_helper.rb