banktools-se 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56a0412c8e17d142b99162549712b831e282fdd8
4
- data.tar.gz: bc6dae711b9df4a89094eae81abf6f148a6092e1
3
+ metadata.gz: 2adb32315551258f25fd25045f10ddba1be9ed38
4
+ data.tar.gz: 07a40e43257c6cac10625e79332210aa5c6d1c56
5
5
  SHA512:
6
- metadata.gz: 08a10797c97ab40fae64930595cac729b92dea89762fec47fe90914f7d84735acad17e920322c09a783cafc89847c255ba462b222f24aebcc9aef8f33a5a5cd5
7
- data.tar.gz: 27fa5ab9c2585e3a410333ac9722ab3bb1a2f235d285af2863d8495fc1b47710231c611972961572d178d5bafa69cae86d44682ec47669cd14e31ce8dff12289
6
+ metadata.gz: 59341b4c01549b9ed8bb597886f93f77149126284d2c2e902e0dec17eac56d735d2153e0d9fd173a0cdc38450806bc43d62f92fc2b15a8a80abbcca7ae7e44fe
7
+ data.tar.gz: 26529338077b1e088fd19e78b60232af82944e59e04b14f733a94b91b5b59c62232877dfd4474c7db316de290e22a1b6793e76fac5bdeaacb342b5038ecfe5e6
@@ -0,0 +1,46 @@
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 OverlongOCR < StandardError; end
7
+ class BadChecksum < StandardError; end
8
+
9
+ class OCR
10
+ def self.number_to_ocr(number, opts = {})
11
+ add_length_digit = opts.fetch(:length_digit, false)
12
+ pad = opts.fetch(:pad, nil)
13
+
14
+ number = number.to_s
15
+
16
+ number += pad if pad
17
+
18
+ # Adding 2: 1 length digit, 1 check digit.
19
+ number += ((number.length + 2) % 10).to_s if add_length_digit
20
+
21
+ number_with_ocr = number + Utils.luhn_checksum(number).to_s
22
+
23
+ length = number_with_ocr.length
24
+ if length > 25
25
+ raise OverlongOCR, "Bankgiro OCR must be 2-25 characters (this one would be #{length} characters)"
26
+ end
27
+
28
+ number_with_ocr
29
+ end
30
+
31
+ def self.number_from_ocr(number, opts = {})
32
+ strip_length_digit = opts.fetch(:length_digit, false)
33
+ strip_padding = opts.fetch(:pad, "")
34
+
35
+ raise BadChecksum unless Utils.valid_luhn?(number)
36
+
37
+ digits_to_chop = 1 # Checksum.
38
+ digits_to_chop += 1 if strip_length_digit
39
+ digits_to_chop += strip_padding.length
40
+
41
+ number[0...-digits_to_chop]
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,44 +1,18 @@
1
1
  # http://sv.wikipedia.org/wiki/Bankgirot#Bankgironummer
2
2
 
3
+ require "banktools-se/bankgiro/ocr"
4
+
3
5
  module BankTools
4
6
  module SE
5
7
  class Bankgiro
6
- class OverlongOCR < StandardError; end
7
- class BadCheckDigit < StandardError; end
8
-
9
8
  attr_reader :number
10
9
 
11
- # http://www.bgc.se/upload/Gemensamt/Trycksaker/Manualer/BG6070.pdf section 5.2
12
10
  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
11
+ OCR.number_to_ocr(number, opts)
30
12
  end
31
13
 
32
14
  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
15
+ OCR.number_from_ocr(number, opts)
42
16
  end
43
17
 
44
18
  def initialize(number)
@@ -1,5 +1,5 @@
1
1
  module BankTools
2
2
  module SE
3
- VERSION = "0.5.0"
3
+ VERSION = "0.6.0"
4
4
  end
5
5
  end
@@ -116,8 +116,8 @@ describe BankTools::SE::Bankgiro do
116
116
  BankTools::SE::Bankgiro.number_from_ocr("1234567890037", length_digit: true, pad: "0").should eq "1234567890"
117
117
  end
118
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)
119
+ it "raises if checksum is wrong" do
120
+ expect { BankTools::SE::Bankgiro.number_from_ocr("1231") }.to raise_error(BankTools::SE::Bankgiro::BadChecksum)
121
121
  end
122
122
  end
123
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.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henrik Nyh
@@ -82,6 +82,7 @@ files:
82
82
  - lib/banktools-se.rb
83
83
  - lib/banktools-se/account.rb
84
84
  - lib/banktools-se/bankgiro.rb
85
+ - lib/banktools-se/bankgiro/ocr.rb
85
86
  - lib/banktools-se/errors.rb
86
87
  - lib/banktools-se/plusgiro.rb
87
88
  - lib/banktools-se/utils.rb