gtin 0.0.1 → 0.1.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.
- data/History.txt +5 -0
- data/lib/gtin.rb +2 -2
- data/lib/gtin/gtin.rb +41 -59
- data/lib/gtin/version.rb +1 -1
- metadata +4 -4
data/History.txt
CHANGED
data/lib/gtin.rb
CHANGED
data/lib/gtin/gtin.rb
CHANGED
@@ -1,84 +1,71 @@
|
|
1
|
+
# This will ultimately become a gem in its own right
|
1
2
|
# GTIN-12 (UPC-A): this is a 12-digit number used primarily in North America
|
2
3
|
# GTIN-8 (EAN/UCC-8): this is an 8-digit number used predominately outside of North America
|
3
4
|
# GTIN-13 (EAN/UCC-13): this is a 13-digit number used predominately outside of North America
|
4
5
|
# GTIN-14 (EAN/UCC-14 or ITF-14): this is a 14-digit number used to identify trade items at various packaging levels
|
5
6
|
|
6
|
-
|
7
|
-
def
|
8
|
-
|
9
|
-
|
10
|
-
checksum = 0
|
11
|
-
case numbers.length
|
12
|
-
when 7
|
13
|
-
0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
|
14
|
-
when 11
|
15
|
-
0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
|
16
|
-
when 12
|
17
|
-
0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * (i%2*3 +(i-1)%2) end
|
18
|
-
when 13
|
19
|
-
0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
|
20
|
-
else
|
21
|
-
0
|
22
|
-
end
|
23
|
-
|
24
|
-
return ((10 - checksum % 10)%10).to_s
|
7
|
+
class Integer
|
8
|
+
def odd?
|
9
|
+
self & 1 != 0
|
25
10
|
end
|
11
|
+
|
12
|
+
def even?
|
13
|
+
self & 1 == 0
|
14
|
+
end
|
15
|
+
end
|
26
16
|
|
17
|
+
module GTIN
|
27
18
|
def ean?
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
case numbers.length
|
32
|
-
when 8
|
33
|
-
0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
|
34
|
-
when 13
|
35
|
-
0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * (i%2*3 +(i-1)%2) end
|
36
|
-
when 14
|
37
|
-
0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
|
38
|
-
else
|
39
|
-
return false
|
40
|
-
end
|
41
|
-
|
42
|
-
return numbers[-1].to_i == (10 - checksum % 10)%10
|
19
|
+
# self = self.to_s.gsub(/[\D]+/, "").split(//)
|
20
|
+
return false if self.length != 13
|
21
|
+
valid_checksum?
|
43
22
|
end
|
44
23
|
|
45
24
|
def upc?
|
46
|
-
|
47
|
-
return false if
|
48
|
-
valid_checksum?
|
25
|
+
# self = self.to_s.gsub(/[\D]+/, "").split(//)
|
26
|
+
return false if self.length != 12
|
27
|
+
valid_checksum?
|
49
28
|
end
|
50
29
|
|
30
|
+
# FOR A UPC:
|
31
|
+
# From the right to left, start with odd position, assign the odd/even position to each digit.
|
32
|
+
# Sum all digits in odd position and multiply the result by 3.
|
33
|
+
# Sum all digits in even position.
|
34
|
+
# Sum the results of step 3 and step 4.
|
35
|
+
# divide the result of step 4 by 10. The check digit is the number which adds the remainder to 10.
|
36
|
+
|
51
37
|
# Determine if a gtin value has a valid checksum
|
52
|
-
def valid_checksum?
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
38
|
+
def valid_checksum?
|
39
|
+
number = self.reverse
|
40
|
+
odd = even = 0
|
41
|
+
|
42
|
+
(1..number.length-1).each do |i|
|
43
|
+
i.even? ? (even += number[i].chr.to_i) : (odd += number[i].chr.to_i)
|
58
44
|
end
|
59
|
-
|
45
|
+
|
46
|
+
number[0].chr.to_i == (10 - ((odd * 3) + even) % 10)
|
60
47
|
end
|
61
48
|
|
62
49
|
|
63
50
|
# GTIN-12 (UPC-A): this is a 12-digit number used primarily in North America
|
64
|
-
def to_gtin_12
|
65
|
-
to_gtin(
|
51
|
+
def to_gtin_12
|
52
|
+
to_gtin(12)
|
66
53
|
end
|
67
54
|
alias :to_upc :to_gtin_12
|
68
55
|
alias :to_upc_a :to_gtin_12
|
69
56
|
|
70
57
|
|
71
58
|
# GTIN-8 (EAN/UCC-8): this is an 8-digit number used predominately outside of North America
|
72
|
-
def to_gtin_8
|
73
|
-
to_gtin(
|
59
|
+
def to_gtin_8
|
60
|
+
to_gtin(8)
|
74
61
|
end
|
75
62
|
alias :to_ean_8 :to_gtin_8
|
76
63
|
alias :to_ucc_8 :to_gtin_8
|
77
64
|
|
78
65
|
|
79
66
|
# GTIN-13 (EAN/UCC-13): this is a 13-digit number used predominately outside of North America
|
80
|
-
def to_gtin_13
|
81
|
-
to_gtin(
|
67
|
+
def to_gtin_13
|
68
|
+
to_gtin(13)
|
82
69
|
end
|
83
70
|
alias :to_ean_13 :to_gtin_13
|
84
71
|
alias :to_ucc_13 :to_gtin_13
|
@@ -86,13 +73,13 @@ module GTIN
|
|
86
73
|
|
87
74
|
|
88
75
|
# GTIN-14 (EAN/UCC-14 or ITF-14): this is a 14-digit number used to identify trade items at various packaging levels
|
89
|
-
def to_gtin_14
|
90
|
-
to_gtin(
|
76
|
+
def to_gtin_14
|
77
|
+
to_gtin(14)
|
91
78
|
end
|
92
79
|
alias :to_gtin :to_gtin_14
|
93
80
|
|
94
|
-
def to_gtin(
|
95
|
-
"%0#{size}d" %
|
81
|
+
def to_gtin(size=14)
|
82
|
+
"%0#{size.to_i}d" % self.to_s.gsub(/[\D]+/, "").to_i
|
96
83
|
end
|
97
84
|
end
|
98
85
|
|
@@ -100,8 +87,3 @@ end
|
|
100
87
|
class String
|
101
88
|
include GTIN
|
102
89
|
end
|
103
|
-
|
104
|
-
# Extend Numberic to include these methods
|
105
|
-
class Numeric
|
106
|
-
include GTIN
|
107
|
-
end
|
data/lib/gtin/version.rb
CHANGED
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 1
|
9
|
+
version: 0.1.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Hans Masing
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2009-04-
|
17
|
+
date: 2009-04-26 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -40,7 +40,7 @@ files:
|
|
40
40
|
- test/test_gtin.rb
|
41
41
|
- test/test_helper.rb
|
42
42
|
has_rdoc: true
|
43
|
-
homepage: http://
|
43
|
+
homepage: http://github.com/hmasing/gtin
|
44
44
|
licenses: []
|
45
45
|
|
46
46
|
post_install_message:
|