viitelaskuri 0.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.
- data/.gitignore +3 -0
- data/History.txt +4 -0
- data/README.txt +58 -0
- data/Rakefile +19 -0
- data/lib/viitelaskuri.rb +106 -0
- data/lib/viitelaskuri/version.rb +3 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +15 -0
- data/spec/viitelaskuri_spec.rb +66 -0
- data/version.txt +1 -0
- data/viitelaskuri.gemspec +36 -0
- metadata +102 -0
data/.gitignore
ADDED
data/History.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
Viitelaskuri
|
2
|
+
by Toni Tuominen
|
3
|
+
http://github.com/tjtuom/viitelaskuri
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
A collection of methods for calculating finnish bank references.
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* Calculate the check number for different numbers
|
12
|
+
* Format the reference number
|
13
|
+
* Check for validity of existing references
|
14
|
+
* Generate (almost certainly) unique references
|
15
|
+
|
16
|
+
== SYNOPSIS:
|
17
|
+
|
18
|
+
To calculate a valid reference from an invoice id for instance use something along these lines.
|
19
|
+
|
20
|
+
require 'viitelaskuri'
|
21
|
+
Viitelaskuri.calculate(100) # => 1009
|
22
|
+
|
23
|
+
See the documentation for other uses.
|
24
|
+
|
25
|
+
== REQUIREMENTS:
|
26
|
+
|
27
|
+
None.
|
28
|
+
|
29
|
+
Tested on ruby 1.8.7 but should work on other versions as well.
|
30
|
+
|
31
|
+
== INSTALL:
|
32
|
+
|
33
|
+
sudo gem install viitelaskuri
|
34
|
+
|
35
|
+
== LICENSE:
|
36
|
+
|
37
|
+
(The MIT License)
|
38
|
+
|
39
|
+
Copyright (c) 2010 Toni Tuominen
|
40
|
+
|
41
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
42
|
+
a copy of this software and associated documentation files (the
|
43
|
+
'Software'), to deal in the Software without restriction, including
|
44
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
45
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
46
|
+
permit persons to whom the Software is furnished to do so, subject to
|
47
|
+
the following conditions:
|
48
|
+
|
49
|
+
The above copyright notice and this permission notice shall be
|
50
|
+
included in all copies or substantial portions of the Software.
|
51
|
+
|
52
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
53
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
54
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
55
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
56
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
57
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
58
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
begin
|
3
|
+
require 'bones'
|
4
|
+
rescue LoadError
|
5
|
+
abort '### Please install the "bones" gem ###'
|
6
|
+
end
|
7
|
+
|
8
|
+
task :default => 'spec:run'
|
9
|
+
task 'gem:release' => 'spec:run'
|
10
|
+
|
11
|
+
Bones {
|
12
|
+
name 'viitelaskuri'
|
13
|
+
authors 'Toni Tuominen'
|
14
|
+
email 'toni@piranhadigital.fi'
|
15
|
+
url 'http://github.com/tjtuom/viitelaskuri'
|
16
|
+
ignore_file '.gitignore'
|
17
|
+
depend_on 'rspec', :development => true
|
18
|
+
}
|
19
|
+
|
data/lib/viitelaskuri.rb
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
# A collection of methods for calculating
|
2
|
+
# bank references for finnish invoices.
|
3
|
+
module Viitelaskuri
|
4
|
+
autoload :Version, 'viitelaskuri/version'
|
5
|
+
|
6
|
+
extend self
|
7
|
+
|
8
|
+
# Calculates the bank reference for a given number
|
9
|
+
#
|
10
|
+
# @param [Number, String] seed The number to calculate the check number for. For instance in invoice number.
|
11
|
+
# @return [Integer] The bank reference calculated.
|
12
|
+
# @raise [ArgumentError] Raises an error if the seed is less than 3 or more than 19 digits.
|
13
|
+
def calculate(seed)
|
14
|
+
str = seed.to_s
|
15
|
+
|
16
|
+
if str.length < 3
|
17
|
+
raise ArgumentError.new('The seed must be at least 3 digits.')
|
18
|
+
end
|
19
|
+
|
20
|
+
if str.length > 19
|
21
|
+
raise ArgumentError.new('The seed must not be more than 19 digits.')
|
22
|
+
end
|
23
|
+
|
24
|
+
reversed = str.reverse
|
25
|
+
sum = 0
|
26
|
+
coefficients = [7, 3, 1]
|
27
|
+
i = 0
|
28
|
+
reversed.each_char do |c|
|
29
|
+
n = c.to_i
|
30
|
+
sum += n * coefficients[i % 3]
|
31
|
+
i += 1
|
32
|
+
end
|
33
|
+
# sum -> nearest full ten - sum
|
34
|
+
check = sum % 10 == 0 ? 0 : ((sum / 10).to_i + 1) * 10 - sum
|
35
|
+
str = str + check.to_s
|
36
|
+
str.to_i
|
37
|
+
end
|
38
|
+
|
39
|
+
# Formats a given number into five digit groups starting from the right
|
40
|
+
#
|
41
|
+
# @param [Number, String] ref The number to format.
|
42
|
+
# @return [String] The formatted string.
|
43
|
+
# @raise [ArgumentError] Raises an error if the ref is less than 4 or more than 20 digits.
|
44
|
+
def format(ref)
|
45
|
+
str = ref.to_s
|
46
|
+
|
47
|
+
if str.length < 4
|
48
|
+
raise ArgumentError.new('The seed must be at least 4 digits.')
|
49
|
+
end
|
50
|
+
|
51
|
+
if str.length > 20
|
52
|
+
raise ArgumentError.new('The seed must not be more than 20 digits.')
|
53
|
+
end
|
54
|
+
|
55
|
+
# organize in five letter groups starting from the right
|
56
|
+
in_groups_of(str.reverse.split(''), 5, false).map { |e| e.join('') }.join(' ').reverse
|
57
|
+
end
|
58
|
+
|
59
|
+
# Checks if a given number is a valid bank reference
|
60
|
+
#
|
61
|
+
# @param [Number, String] ref The number to check.
|
62
|
+
# @return [Boolean] True if the ref is valid, false if not.
|
63
|
+
def valid?(ref)
|
64
|
+
if ref.to_s.length < 4 || ref.to_s.length > 20
|
65
|
+
return false
|
66
|
+
end
|
67
|
+
|
68
|
+
str = ref.to_s[0...-1]
|
69
|
+
calculate(str) == ref.to_i
|
70
|
+
end
|
71
|
+
|
72
|
+
# Generates an almost certainly unique bank reference.
|
73
|
+
# Uses a timestamp (10 digits) and a random number (9 digits)
|
74
|
+
# and adds the check number.
|
75
|
+
#
|
76
|
+
# @return [Integer] the generated bank reference.
|
77
|
+
def generate
|
78
|
+
time = Time.now.utc.to_i
|
79
|
+
random = 9.times.map { rand(10) }
|
80
|
+
calculate(time.to_s + random.to_s)
|
81
|
+
end
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
# borrowed from rails
|
86
|
+
def in_groups_of(array, number, fill_with = nil)
|
87
|
+
if fill_with == false
|
88
|
+
collection = array
|
89
|
+
else
|
90
|
+
# size % number gives how many extra we have;
|
91
|
+
# subtracting from number gives how many to add;
|
92
|
+
# modulo number ensures we don't add group of just fill.
|
93
|
+
padding = (number - array.size % number) % number
|
94
|
+
collection = array.dup.concat([fill_with] * padding)
|
95
|
+
end
|
96
|
+
|
97
|
+
if block_given?
|
98
|
+
collection.each_slice(number) { |slice| yield(slice) }
|
99
|
+
else
|
100
|
+
groups = []
|
101
|
+
collection.each_slice(number) { |group| groups << group }
|
102
|
+
groups
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
|
2
|
+
require File.expand_path(
|
3
|
+
File.join(File.dirname(__FILE__), %w[.. lib viitelaskuri]))
|
4
|
+
|
5
|
+
Spec::Runner.configure do |config|
|
6
|
+
# == Mock Framework
|
7
|
+
#
|
8
|
+
# RSpec uses it's own mocking framework by default. If you prefer to
|
9
|
+
# use mocha, flexmock or RR, uncomment the appropriate line:
|
10
|
+
#
|
11
|
+
# config.mock_with :mocha
|
12
|
+
# config.mock_with :flexmock
|
13
|
+
# config.mock_with :rr
|
14
|
+
end
|
15
|
+
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), %w[spec_helper])
|
2
|
+
|
3
|
+
describe Viitelaskuri do
|
4
|
+
|
5
|
+
context "#calculate" do
|
6
|
+
|
7
|
+
it "calculates bank references" do
|
8
|
+
seeds = [100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 403, 693, 193626, 5926038623]
|
9
|
+
refs = [1009, 1012, 1025, 1038, 1041, 1054, 1067, 1070, 1083, 1096, 4035, 6936, 1936267, 59260386230]
|
10
|
+
seeds.each_with_index do |seed, i|
|
11
|
+
Viitelaskuri.calculate(seed).should == refs[i]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'raises an error if given a number less than 3 digits long' do
|
16
|
+
lambda { Viitelaskuri.calculate(10) }.should raise_error(ArgumentError)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'raises an error if given a number more than 19 digits long' do
|
20
|
+
lambda { Viitelaskuri.calculate(12345678901234567890) }.should raise_error(ArgumentError)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
context "#format" do
|
26
|
+
|
27
|
+
it 'formats bank references' do
|
28
|
+
refs = [[1009, '1009'], [1936267, '19 36267'], [59260386230, '5 92603 86230']]
|
29
|
+
refs.each do |ref|
|
30
|
+
Viitelaskuri.format(ref.first).should == ref.last
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'raises an error if given a number less than 4 digits long' do
|
35
|
+
lambda { Viitelaskuri.format(100) }.should raise_error(ArgumentError)
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'raises an error if given a number more than 20 digits long' do
|
39
|
+
lambda { Viitelaskuri.format(123456789012345678901) }.should raise_error(ArgumentError)
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
context "#valid?" do
|
46
|
+
|
47
|
+
it 'validates references' do
|
48
|
+
refs = [[1009, true], [1008, false], ['19 36267', true], [100, false], [123456789012345678901, false]]
|
49
|
+
refs.each do |ref|
|
50
|
+
Viitelaskuri.valid?(ref.first).should == ref.last
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
context "#generate" do
|
57
|
+
|
58
|
+
it 'generates a random reference with almost certain uniqueness' do
|
59
|
+
ref = Viitelaskuri.generate
|
60
|
+
Viitelaskuri.valid?(ref).should be_true
|
61
|
+
ref.to_s.length.should == 20
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
data/version.txt
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{viitelaskuri}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Toni Tuominen"]
|
9
|
+
s.date = %q{2010-05-30}
|
10
|
+
s.description = %q{A collection of methods for calculating finnish bank references.}
|
11
|
+
s.email = %q{toni@piranhadigital.fi}
|
12
|
+
s.extra_rdoc_files = ["History.txt", "README.txt", "version.txt"]
|
13
|
+
s.files = [".gitignore", "History.txt", "README.txt", "Rakefile", "lib/viitelaskuri.rb", "lib/viitelaskuri/version.rb", "spec/spec.opts", "spec/spec_helper.rb", "spec/viitelaskuri_spec.rb", "version.txt", "viitelaskuri.gemspec"]
|
14
|
+
s.homepage = %q{http://github.com/tjtuom/viitelaskuri}
|
15
|
+
s.rdoc_options = ["--main", "README.txt"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{viitelaskuri}
|
18
|
+
s.rubygems_version = %q{1.3.6}
|
19
|
+
s.summary = %q{A collection of methods for calculating finnish bank references}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
26
|
+
s.add_development_dependency(%q<rspec>, [">= 1.3.0"])
|
27
|
+
s.add_development_dependency(%q<bones>, [">= 3.4.3"])
|
28
|
+
else
|
29
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
30
|
+
s.add_dependency(%q<bones>, [">= 3.4.3"])
|
31
|
+
end
|
32
|
+
else
|
33
|
+
s.add_dependency(%q<rspec>, [">= 1.3.0"])
|
34
|
+
s.add_dependency(%q<bones>, [">= 3.4.3"])
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: viitelaskuri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Toni Tuominen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-05-30 00:00:00 +03:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
version: 1.3.0
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: bones
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 3
|
43
|
+
- 4
|
44
|
+
- 3
|
45
|
+
version: 3.4.3
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
48
|
+
description: A collection of methods for calculating finnish bank references.
|
49
|
+
email: toni@piranhadigital.fi
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions: []
|
53
|
+
|
54
|
+
extra_rdoc_files:
|
55
|
+
- History.txt
|
56
|
+
- README.txt
|
57
|
+
- version.txt
|
58
|
+
files:
|
59
|
+
- .gitignore
|
60
|
+
- History.txt
|
61
|
+
- README.txt
|
62
|
+
- Rakefile
|
63
|
+
- lib/viitelaskuri.rb
|
64
|
+
- lib/viitelaskuri/version.rb
|
65
|
+
- spec/spec.opts
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
- spec/viitelaskuri_spec.rb
|
68
|
+
- version.txt
|
69
|
+
- viitelaskuri.gemspec
|
70
|
+
has_rdoc: true
|
71
|
+
homepage: http://github.com/tjtuom/viitelaskuri
|
72
|
+
licenses: []
|
73
|
+
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options:
|
76
|
+
- --main
|
77
|
+
- README.txt
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project: viitelaskuri
|
97
|
+
rubygems_version: 1.3.6
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: A collection of methods for calculating finnish bank references
|
101
|
+
test_files: []
|
102
|
+
|