banktools-se 2.0.0 → 2.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4f480d8889741823d7cc4e020dff15d39406fd68
4
- data.tar.gz: 547a6c83c6fe15e4a6e17f943a9b1b925ff1f3ad
3
+ metadata.gz: 74491115cff66b4bf05d2d842ebc5e244580c253
4
+ data.tar.gz: 51f456d3da82641e22dc0c236c84b0fdf9b1b66e
5
5
  SHA512:
6
- metadata.gz: e2b9fa6aeae917ee57d0b2c98303b4437fdb9f7f084ef873cffefc3c9d243a111565f0bf52b7fb10e9af6cc502fee6a72c45ac021e344f9ba6dcdabeaab4b770
7
- data.tar.gz: 1070b2bcecd0aca9bdb4a2de78bd94111242a42fa28b072d4ec06fccb15cc8f17e69bc269e0eb218ac4a8b1467b16b8cf68fec6242898da9c03e0f00bf2e0cb8
6
+ metadata.gz: b28bb0e72521ab8641fa2545002a4a86e6add6856d526c6a98f681002265aa3a4b8f531959c330beba6552fc6138fbd45f598f8fb9023092d9474b91ce491669
7
+ data.tar.gz: b61fb6be0c11bf1c784dec49d58a4ed199a9adfc244fe119aa2152e2e60c198fe70ca3d74a057bc6ac3985eff4fab3c5aac0a40b6681d8d2715a0a8a2ec1624a
data/.travis.yml CHANGED
@@ -1,5 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.0
4
- - 2.0.0
5
- - 1.9.3
3
+ - 2.3.0
4
+ - 2.2.5
data/README.markdown CHANGED
@@ -43,6 +43,11 @@ Inspired by [iulianu/iban-tools](https://github.com/iulianu/iban-tools). Please
43
43
  BankTools::SE::OCR.to_number("123067", length_digit: true, pad: "0") # => "123"
44
44
  BankTools::SE::OCR.to_number("1230", length_digit: true, pad: "0") # Raises exception because there's no length digit or padding.
45
45
 
46
+ # This feature is intended to try to find all OCR numbers in a noisy bank statement string.
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
+ BankTools::SE::OCR.find_all_in_string("OCR1230 and ref4564 and 789") # => [ "1230", "4564" ]
49
+ BankTools::SE::OCR.find_all_in_string("1230 and 123067", length_digit: true, pad: "0") # => [ "123067" ]
50
+
46
51
  # Plusgiro
47
52
 
48
53
  valid_account = BankTools::SE::Plusgiro.new("2865434")
@@ -105,7 +110,7 @@ Or install it yourself as:
105
110
 
106
111
  Possible improvements to make:
107
112
 
108
- * Luhn validation based on [BGC docs](http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG910.pdf).
113
+ * Luhn validation based on [BGC docs](http://web.archive.org/web/20130613010943/http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG910.pdf).
109
114
 
110
115
 
111
116
  ## Also see
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- # http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG910.pdf
3
+ # http://web.archive.org/web/20130613010943/http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG910.pdf
4
4
 
5
5
  require "banktools-se/account/clearing_number"
6
6
 
@@ -1,4 +1,4 @@
1
- # http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
1
+ # http://web.archive.org/web/20111216065227/http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
2
2
 
3
3
  module BankTools
4
4
  module SE
@@ -13,10 +13,10 @@ module BankTools
13
13
  MIN_LENGTH = 2
14
14
  MAX_LENGTH = 25
15
15
 
16
- def self.from_number(number, opts = {})
16
+ def self.from_number(number, length_digit: false, pad: "")
17
17
  number = number.to_s
18
- add_length_digit = opts.fetch(:length_digit, false)
19
- pad = opts.fetch(:pad, "").to_s
18
+ add_length_digit = length_digit
19
+ pad = pad.to_s
20
20
 
21
21
  raise MustBeNumeric unless number.match(/\A\d+\z/)
22
22
  # Padding isn't something BGC specifies, but we needed it to support a legacy scheme.
@@ -34,10 +34,10 @@ module BankTools
34
34
  number_with_ocr
35
35
  end
36
36
 
37
- def self.to_number(number, opts = {})
37
+ def self.to_number(number, length_digit: false, pad: "")
38
38
  number = number.to_s
39
- should_have_length_digit = opts.fetch(:length_digit, false)
40
- strip_padding = opts.fetch(:pad, "").to_s
39
+ should_have_length_digit = length_digit
40
+ strip_padding = pad.to_s
41
41
 
42
42
  raise MustBeNumeric unless number.match(/\A\d+\z/)
43
43
  raise BadChecksum unless Utils.valid_luhn?(number)
@@ -61,6 +61,17 @@ module BankTools
61
61
 
62
62
  number[0...-digits_to_chop]
63
63
  end
64
+
65
+ def self.find_all_in_string(string, length_digit: false, pad: "")
66
+ string.scan(/\d+/).select { |candidate|
67
+ begin
68
+ to_number(candidate, length_digit: length_digit, pad: pad)
69
+ true
70
+ rescue InvalidOCR
71
+ false
72
+ end
73
+ }.uniq
74
+ end
64
75
  end
65
76
  end
66
77
  end
@@ -1,5 +1,5 @@
1
1
  module BankTools
2
2
  module SE
3
- VERSION = "2.0.0"
3
+ VERSION = "2.1.0"
4
4
  end
5
5
  end
data/spec/ocr_spec.rb CHANGED
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
  require "banktools-se"
3
3
 
4
4
  describe BankTools::SE::OCR do
5
- # http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
5
+ # http://web.archive.org/web/20111216065227/http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
6
6
  describe ".from_number" do
7
7
  it "adds a mod-10 check digit" do
8
8
  BankTools::SE::OCR.from_number("123").should eq "1230"
@@ -66,4 +66,24 @@ describe BankTools::SE::OCR do
66
66
  expect { BankTools::SE::OCR.to_number("garbage") }.to raise_error(BankTools::SE::OCR::MustBeNumeric)
67
67
  end
68
68
  end
69
+
70
+ describe ".find_all_in_string" do
71
+ it "detects only number sequences that are valid OCRs" do
72
+ expect(BankTools::SE::OCR.find_all_in_string("1230 1234 4564")).to eq [ "1230", "4564" ]
73
+ end
74
+
75
+ it "requires OCRs to comply with length_digit and pad options" do
76
+ string = "1230 4564 123067 456061"
77
+ expect(BankTools::SE::OCR.find_all_in_string(string)).to eq [ "1230", "4564", "123067", "456061" ]
78
+ expect(BankTools::SE::OCR.find_all_in_string(string, length_digit: true, pad: "0")).to eq [ "123067", "456061" ]
79
+ end
80
+
81
+ it "finds digits among any non-digit characters" do
82
+ expect(BankTools::SE::OCR.find_all_in_string("x1230x")).to eq [ "1230" ]
83
+ end
84
+
85
+ it "excludes duplicates" do
86
+ expect(BankTools::SE::OCR.find_all_in_string("1230 1230 4564")).to eq [ "1230", "4564" ]
87
+ end
88
+ end
69
89
  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: 2.0.0
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
@@ -113,7 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
113
113
  version: '0'
114
114
  requirements: []
115
115
  rubyforge_project: banktools-se
116
- rubygems_version: 2.2.1
116
+ rubygems_version: 2.5.1
117
117
  signing_key:
118
118
  specification_version: 4
119
119
  summary: Validate and normalize Swedish bank account numbers, plusgiro and bankgiro.