viitenumero 0.0.2 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -2
- data/lib/viitenumero.rb +2 -0
- data/lib/viitenumero/fi_viite.rb +84 -0
- data/lib/viitenumero/rf_viite.rb +86 -0
- data/lib/viitenumero/version.rb +1 -1
- data/lib/viitenumero/viite.rb +17 -52
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbddfa4ece091a8353870235c9667943118770d0
|
4
|
+
data.tar.gz: aaefedb9210111517e2b4d776f08c51dc568298b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 238cfc61b92a611e7876ec948dfde03aa672ace9843d092231cc4f2b64986569a48ec580e19ae4b24070f0b07ea31c6a9baaab5e7b67429fc2733259642bb824
|
7
|
+
data.tar.gz: adefdab209ae3cbe00e6d53af82c2a8631dfbf80af4fbf092c43c16eb9282295455d30c962d78c681eb79a755803f2341fd42d058fef570fa0e89cd581dd14ae
|
data/README.md
CHANGED
@@ -27,15 +27,20 @@ viite = Viite.new('75310116600498132890')
|
|
27
27
|
viite.valid?
|
28
28
|
=> true
|
29
29
|
|
30
|
+
viite.fi.to_s
|
31
|
+
=> "75310116600498132890"
|
32
|
+
|
30
33
|
viite.paper_format
|
31
34
|
=> "75310 11660 04981 32890"
|
32
35
|
|
33
|
-
|
36
|
+
FIViite.random(length: 10).to_s
|
34
37
|
=> "3084950403"
|
35
38
|
|
36
39
|
laskun_numero = 1000
|
37
|
-
|
40
|
+
FIViite.generate(laskun_numero).to_s
|
38
41
|
=> "10003"
|
42
|
+
RFViite.generate(laskun_numero).to_s
|
43
|
+
=> "RF6810003"
|
39
44
|
|
40
45
|
class Bill < ActiveRecord
|
41
46
|
validates :reference_number, viite: true
|
data/lib/viitenumero.rb
CHANGED
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module Viitenumero
|
4
|
+
class FIViite
|
5
|
+
include ActiveModel::Validations
|
6
|
+
|
7
|
+
attr_reader :number
|
8
|
+
|
9
|
+
def initialize(s)
|
10
|
+
@number = format(s)
|
11
|
+
end
|
12
|
+
|
13
|
+
def paper_format
|
14
|
+
number.gsub(/.{5}(?=.)/, '\0 ')
|
15
|
+
end
|
16
|
+
|
17
|
+
def machine_format
|
18
|
+
number
|
19
|
+
end
|
20
|
+
|
21
|
+
def valid?
|
22
|
+
valid_format? and valid_length? and valid_checksum?
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.generate(base)
|
26
|
+
base = (base || '').to_s.gsub(/\s+/, '')
|
27
|
+
raise ArgumentError.new('must be a number') if base.match(/\A\d+\z/).nil?
|
28
|
+
raise ArgumentError.new('must be between 3-19 chars long') if base.length < 3 || base.length > 19
|
29
|
+
|
30
|
+
FIViite.new(base + checksum(base))
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.random(length: 4)
|
34
|
+
raise ArgumentError if length < 4 || length > 20
|
35
|
+
base = ''
|
36
|
+
base_length = length - 1
|
37
|
+
base = rand(10**base_length).to_s while base.length != base_length
|
38
|
+
generate(base.to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
def self.valid?(s)
|
42
|
+
FIViite.new(s).valid?
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_s
|
46
|
+
machine_format
|
47
|
+
end
|
48
|
+
|
49
|
+
def to_rf
|
50
|
+
RFViite.generate(machine_format, add_fi_checksum: false)
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def format(s)
|
56
|
+
r = (s || '').to_s.gsub(/\s+/, '')
|
57
|
+
r.slice!(0) while r[0] == '0'
|
58
|
+
r
|
59
|
+
end
|
60
|
+
|
61
|
+
def valid_format?
|
62
|
+
!number.match(/\A\d+\z/).nil?
|
63
|
+
end
|
64
|
+
|
65
|
+
def valid_length?
|
66
|
+
number.length >= 4 && number.length <= 20
|
67
|
+
end
|
68
|
+
|
69
|
+
def valid_checksum?
|
70
|
+
last_digit = number[-1, 1]
|
71
|
+
last_digit == self.class.checksum(number[0..-2])
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.checksum(base)
|
75
|
+
weights = [7, 3, 1]
|
76
|
+
checksum, current_weight = 0, 0
|
77
|
+
base.split('').reverse.each do |digit|
|
78
|
+
checksum += digit.to_i * weights[current_weight % 3]
|
79
|
+
current_weight += 1
|
80
|
+
end
|
81
|
+
((10 - checksum % 10) % 10).to_s
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
module Viitenumero
|
4
|
+
# Note: Supports only ISO 11649 creditor references that are based on
|
5
|
+
# Finnish national reference numbers (see fi_viite). Does *not* support
|
6
|
+
# other SEPA countries.
|
7
|
+
class RFViite
|
8
|
+
include ActiveModel::Validations
|
9
|
+
|
10
|
+
attr_reader :number
|
11
|
+
|
12
|
+
def initialize(s)
|
13
|
+
@number = format(s)
|
14
|
+
end
|
15
|
+
|
16
|
+
def paper_format
|
17
|
+
number.gsub(/.{4}(?=.)/, '\0 ')
|
18
|
+
end
|
19
|
+
|
20
|
+
def machine_format
|
21
|
+
number
|
22
|
+
end
|
23
|
+
|
24
|
+
def valid?
|
25
|
+
valid_format? and valid_length? and valid_checksum? and valid_national_checksum?
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.generate(base, add_fi_checksum: true)
|
29
|
+
base = (base || '').to_s.gsub(/\s+/, '')
|
30
|
+
raise ArgumentError.new('must be a number') if base.match(/\A\d+\z/).nil?
|
31
|
+
raise ArgumentError.new('must be between 3-19 chars long') if base.length < 3 || base.length > 19
|
32
|
+
|
33
|
+
base = base + FIViite.checksum(base) if add_fi_checksum
|
34
|
+
RFViite.new('RF' + checksum(base) + base)
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.random(length: 8)
|
38
|
+
raise ArgumentError if length < 8 || length > 24
|
39
|
+
base_length = length - 4
|
40
|
+
FIViite.random(length: base_length).to_rf
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.valid?(s)
|
44
|
+
RFViite.new(s).valid?
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_s
|
48
|
+
machine_format
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_fi
|
52
|
+
FIViite.new(national_part)
|
53
|
+
end
|
54
|
+
|
55
|
+
def national_part
|
56
|
+
number[4..-1]
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def format(s)
|
62
|
+
(s || '').to_s.gsub(/\s+/, '').upcase
|
63
|
+
end
|
64
|
+
|
65
|
+
def valid_format?
|
66
|
+
!number.match(/\ARF\d+\z/).nil?
|
67
|
+
end
|
68
|
+
|
69
|
+
def valid_length?
|
70
|
+
number.length >= 8 && number.length <= 24
|
71
|
+
end
|
72
|
+
|
73
|
+
def valid_checksum?
|
74
|
+
check_digits = number[2..3]
|
75
|
+
check_digits == self.class.checksum(national_part)
|
76
|
+
end
|
77
|
+
|
78
|
+
def valid_national_checksum?
|
79
|
+
FIViite.valid?(national_part)
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.checksum(base)
|
83
|
+
(98 - (base + '271500').to_i % 97).to_s.rjust(2, '0')
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
data/lib/viitenumero/version.rb
CHANGED
data/lib/viitenumero/viite.rb
CHANGED
@@ -7,37 +7,33 @@ module Viitenumero
|
|
7
7
|
attr_reader :number
|
8
8
|
|
9
9
|
def initialize(s)
|
10
|
-
|
10
|
+
if FIViite.valid?(s)
|
11
|
+
@number = FIViite.new(s)
|
12
|
+
elsif RFViite.valid?(s)
|
13
|
+
@number = RFViite.new(s)
|
14
|
+
else
|
15
|
+
@number = FIViite.new(s)
|
16
|
+
end
|
11
17
|
end
|
12
18
|
|
13
|
-
def
|
14
|
-
number.
|
19
|
+
def rf
|
20
|
+
number.is_a?(RFViite) ? number : number.to_rf
|
15
21
|
end
|
16
22
|
|
17
|
-
def
|
18
|
-
number
|
23
|
+
def fi
|
24
|
+
number.is_a?(FIViite) ? number : number.to_fi
|
19
25
|
end
|
20
26
|
|
21
|
-
def
|
22
|
-
|
27
|
+
def paper_format
|
28
|
+
number.paper_format
|
23
29
|
end
|
24
30
|
|
25
|
-
def
|
26
|
-
|
27
|
-
raise ArgumentError.new('must be a number') if base.match(/\A\d+\z/).nil?
|
28
|
-
raise ArgumentError.new('must be between 3-19 chars long') if base.length < 3 || base.length > 19
|
29
|
-
|
30
|
-
Viite.new(base + checksum(base))
|
31
|
+
def machine_format
|
32
|
+
number.machine_format
|
31
33
|
end
|
32
34
|
|
33
|
-
def
|
34
|
-
|
35
|
-
length = opts.fetch(:length, 4)
|
36
|
-
raise ArgumentError if length < 4 || length > 20
|
37
|
-
base = ''
|
38
|
-
base_length = length - 1
|
39
|
-
base = rand(10**base_length).to_s while base.length != base_length
|
40
|
-
generate(base.to_s)
|
35
|
+
def valid?
|
36
|
+
number.valid?
|
41
37
|
end
|
42
38
|
|
43
39
|
def self.valid?(s)
|
@@ -47,36 +43,5 @@ module Viitenumero
|
|
47
43
|
def to_s
|
48
44
|
machine_format
|
49
45
|
end
|
50
|
-
|
51
|
-
private
|
52
|
-
|
53
|
-
def format(s)
|
54
|
-
r = (s || '').to_s.gsub(/\s+/, '')
|
55
|
-
r.slice!(0) while r[0] == '0'
|
56
|
-
r
|
57
|
-
end
|
58
|
-
|
59
|
-
def valid_format?
|
60
|
-
!number.match(/\A\d+\z/).nil?
|
61
|
-
end
|
62
|
-
|
63
|
-
def valid_length?
|
64
|
-
number.length >= 4 && number.length <= 20
|
65
|
-
end
|
66
|
-
|
67
|
-
def valid_checksum?
|
68
|
-
last_digit = number[-1, 1]
|
69
|
-
last_digit == self.class.checksum(number[0..-2])
|
70
|
-
end
|
71
|
-
|
72
|
-
def self.checksum(base)
|
73
|
-
weights = [7, 3, 1]
|
74
|
-
checksum, current_weight = 0, 0
|
75
|
-
base.split('').reverse.each do |digit|
|
76
|
-
checksum += digit.to_i * weights[current_weight % 3]
|
77
|
-
current_weight += 1
|
78
|
-
end
|
79
|
-
((10 - checksum % 10) % 10).to_s
|
80
|
-
end
|
81
46
|
end
|
82
47
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: viitenumero
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alexander Hanhikoski
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2016-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -97,6 +97,8 @@ files:
|
|
97
97
|
- bin/console
|
98
98
|
- bin/setup
|
99
99
|
- lib/viitenumero.rb
|
100
|
+
- lib/viitenumero/fi_viite.rb
|
101
|
+
- lib/viitenumero/rf_viite.rb
|
100
102
|
- lib/viitenumero/version.rb
|
101
103
|
- lib/viitenumero/viite.rb
|
102
104
|
- lib/viitenumero/viite_validator.rb
|