container6346 0.0.1
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 +7 -0
- data/lib/container6346.rb +97 -0
- metadata +42 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 150173051b1f6383c926697c2ca5ee9b8c852022ecdb8b698c9cd45c6b31d7b0
|
4
|
+
data.tar.gz: 5455aae4a7b7f452dd84ae450f48be092baf5e21dfecea03f71d4d3a3a4c408e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c2c07eec267e57dc5924e01afcb5c03bc787f632647c1087d69777f6587122c5c94be0765d533436dbdd38248ca34d34373bd545fb74ccaedc2ff70431a32b98
|
7
|
+
data.tar.gz: adc0a95a8849015cf3477e5d3cfd80fb4ee36e07168757659d956f123f4261e88f73e7e38a825586f452dd5601945eb0337c94853f75bac55dfb9d36d923e9d4
|
@@ -0,0 +1,97 @@
|
|
1
|
+
module Container6346
|
2
|
+
|
3
|
+
MAPPING = {
|
4
|
+
"A" => 10,
|
5
|
+
"B" => 12,
|
6
|
+
"C" => 13,
|
7
|
+
"D" => 14,
|
8
|
+
"E" => 15,
|
9
|
+
"F" => 16,
|
10
|
+
"G" => 17,
|
11
|
+
"H" => 18,
|
12
|
+
"I" => 19,
|
13
|
+
"J" => 20,
|
14
|
+
"K" => 21,
|
15
|
+
"L" => 23,
|
16
|
+
"M" => 24,
|
17
|
+
"N" => 25,
|
18
|
+
"O" => 26,
|
19
|
+
"P" => 27,
|
20
|
+
"Q" => 28,
|
21
|
+
"R" => 29,
|
22
|
+
"S" => 30,
|
23
|
+
"T" => 31,
|
24
|
+
"U" => 32,
|
25
|
+
"V" => 34,
|
26
|
+
"W" => 35,
|
27
|
+
"X" => 36,
|
28
|
+
"Y" => 37,
|
29
|
+
"Z" => 38
|
30
|
+
}
|
31
|
+
|
32
|
+
|
33
|
+
def self.validate_container?(container)
|
34
|
+
regex = /^[a-z]{3}[ujz]\d{7}/i
|
35
|
+
if not regex.match(container)
|
36
|
+
raise "Invalid container #{container}"
|
37
|
+
end
|
38
|
+
|
39
|
+
owner_code = container[0...3]
|
40
|
+
category_identifier = container[3]
|
41
|
+
serial_number = container[4...10]
|
42
|
+
check_digit = container[10]
|
43
|
+
|
44
|
+
self.validate?(owner_code, category_identifier, serial_number, check_digit)
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
def self.validate?(owner_code, category_identifier, serial_number, check_digit)
|
50
|
+
if not owner_code.length == 3
|
51
|
+
raise "Invalid owner code"
|
52
|
+
end
|
53
|
+
owner_code
|
54
|
+
if not category_identifier.length == 1 # todo ...
|
55
|
+
raise "Invalid category identifier "
|
56
|
+
end
|
57
|
+
if not self.only_digits?(serial_number) or serial_number.length != 6
|
58
|
+
raise 'Invalid serial number'
|
59
|
+
end
|
60
|
+
if not self.only_digits?(check_digit) or check_digit.length != 1
|
61
|
+
raise 'Invalid Check digit'
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
list = []
|
66
|
+
|
67
|
+
owner_code.each_char do |c|
|
68
|
+
list.push(MAPPING[c])
|
69
|
+
end
|
70
|
+
|
71
|
+
list.push(MAPPING[category_identifier])
|
72
|
+
|
73
|
+
serial_number.each_char do |c|
|
74
|
+
list.push(c.to_i)
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
sum = 0
|
79
|
+
list.each_with_index do |value, index|
|
80
|
+
sum += value * 2**index
|
81
|
+
end
|
82
|
+
|
83
|
+
generated_digit = sum - ((sum/11)*11)
|
84
|
+
if generated_digit == 10 then
|
85
|
+
generated_digit = 0
|
86
|
+
end
|
87
|
+
|
88
|
+
|
89
|
+
check_digit == "#{generated_digit}"
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
|
94
|
+
def self.only_digits?(string)
|
95
|
+
string !~ /\D/
|
96
|
+
end
|
97
|
+
end
|
metadata
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: container6346
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- bimmy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email:
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/container6346.rb
|
20
|
+
homepage:
|
21
|
+
licenses: []
|
22
|
+
metadata: {}
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: '0'
|
32
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - ">="
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
requirements: []
|
38
|
+
rubygems_version: 3.2.5
|
39
|
+
signing_key:
|
40
|
+
specification_version: 4
|
41
|
+
summary: Perform basic validations for ISO 6346 intermodal containers
|
42
|
+
test_files: []
|