weird 0.0.2
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/lib/weird.rb +73 -0
- data/lib/weird/version.rb +3 -0
- metadata +59 -0
data/lib/weird.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'weird/version'
|
2
|
+
|
3
|
+
module Weird
|
4
|
+
FIRST_WEIGHTING = [3, 2, 7, 6, 5, 4, 3, 2]
|
5
|
+
SECOND_WEIGHTING = [7, 4, 3, 2, 5, 2, 7, 6]
|
6
|
+
def self.version_string
|
7
|
+
"Weird version #{Weird::VERSION}"
|
8
|
+
end
|
9
|
+
|
10
|
+
# Check digit validation
|
11
|
+
# The following steps are to be performed:
|
12
|
+
# - Valid range >10,000,000 & <150,000,000 (This will ensure invalid numbers that will not
|
13
|
+
# be issued for another 10 years cannot be used in error)
|
14
|
+
# - To each of the base number’s eight digits a weight factor is assigned. From left to right
|
15
|
+
# these are: 3, 2, 7, 6, 5, 4, 3, 2.
|
16
|
+
# - Where the base number is seven digits remember there is a leading zero.
|
17
|
+
# - Sum together the products of the weight factors and their associated digits.
|
18
|
+
# - Divide the sum by 11. If the remainder is 0 then the check digit is 0.
|
19
|
+
# - If the remainder is not 0 then subtract this number from 11, giving the check digit (0 - 9
|
20
|
+
# are valid).
|
21
|
+
# - If the resulting check digit is 10, use a secondary set of weight factors and apply steps 2
|
22
|
+
# and 3 to the same input base number. From left to right the factors for an eight digit
|
23
|
+
# base number are: 7, 4, 3, 2, 5, 2, 7, 6.
|
24
|
+
# - Remember there is a leading zero for a seven digit base number. If the new check digit
|
25
|
+
# is again 10 then the IRD number is invalid (0 - 9 is valid).
|
26
|
+
# - Compare the calculated check digit with the check digit on the IRD number. If they
|
27
|
+
# match then the IRD number is valid.
|
28
|
+
def self.valid_ird? input
|
29
|
+
is_integer(input) && valid_range(input) && check_digit_matches(input)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def self.is_integer input
|
35
|
+
!!(input =~ /^[0-9]+$/)
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.valid_range input
|
39
|
+
10000000 < input.to_i && input.to_i < 150000000
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.check_digit_matches input_array
|
43
|
+
ird_array = convert_to_integer_array(input_array)
|
44
|
+
provided_check_digit = ird_array.pop
|
45
|
+
calculate_check_digit(ird_array) == provided_check_digit
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.convert_to_integer_array input
|
49
|
+
ird_array = input.split(//)
|
50
|
+
ird_array.map!{ |x| x.to_i }
|
51
|
+
if ird_array.size == 8
|
52
|
+
ird_array.unshift(0)
|
53
|
+
end
|
54
|
+
ird_array
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.calculate_check_digit input_array, weighting_array=FIRST_WEIGHTING
|
58
|
+
remainder = calculate_remainder input_array, weighting_array
|
59
|
+
return remainder if remainder == 0
|
60
|
+
|
61
|
+
check_digit = 11 - remainder
|
62
|
+
if check_digit == 10 && weighting_array == FIRST_WEIGHTING
|
63
|
+
check_digit = calculate_check_digit input_array, SECOND_WEIGHTING
|
64
|
+
end
|
65
|
+
check_digit
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.calculate_remainder input_array, weighting_array
|
69
|
+
weighted_array = input_array.each_with_index.map { |x,i| x.to_i * weighting_array.at(i) }
|
70
|
+
sum_of_weighted_array = weighted_array.inject{ |x,y| x+y }
|
71
|
+
return sum_of_weighted_array.remainder(11)
|
72
|
+
end
|
73
|
+
end
|
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: weird
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Vydra
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-10-26 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70231797548840 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.7'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70231797548840
|
25
|
+
description: Checksum validation for New Zealand IRD numbers. See http://www.ird.govt.nz/how-to/irdnumbers/
|
26
|
+
for details of what an IRD is.
|
27
|
+
email:
|
28
|
+
- usefulaccount@gmail.com
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- lib/weird/version.rb
|
34
|
+
- lib/weird.rb
|
35
|
+
homepage: http://github.com/dvydra/weird
|
36
|
+
licenses: []
|
37
|
+
post_install_message:
|
38
|
+
rdoc_options: []
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.8.10
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: New Zealand IRD number checksum validation
|
59
|
+
test_files: []
|