barkick 0.0.1 → 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.
- checksums.yaml +4 -4
- data/README.md +7 -1
- data/lib/barkick/version.rb +1 -1
- data/lib/gtin.rb +83 -11
- data/test/gtin_test.rb +2 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45f5a7c3383231e5e4107786fb5926919d8ad0a1
|
4
|
+
data.tar.gz: b8bffcb8014de2797a6002102e9f8abd45188d13
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 95d919d68dfe43b6918655d2522ea9542f05f823d1c16c335c892a784545609e4a6c54a167e6672909b4383817074883e22219d70318cf6d633fde84f991cd08
|
7
|
+
data.tar.gz: c7a9e605c78b6d821980a3af0352c7fa494e5e0bab24ff0aac8d2a7491a2228c3d1db0028076bc149c41e4169e524afe2e5721042d5406036d06989ecf17f74b
|
data/README.md
CHANGED
@@ -30,7 +30,13 @@ gtin = GTIN.new("299265108631")
|
|
30
30
|
gtin.variable? # true
|
31
31
|
gtin.restricted? # true
|
32
32
|
gtin.price # 8.63
|
33
|
-
gtin.base_gtin14 # "
|
33
|
+
gtin.base_gtin14 # "00299265000003"
|
34
|
+
```
|
35
|
+
|
36
|
+
Calculate check digit
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
GTIN.check_digit("01600027526") # "3"
|
34
40
|
```
|
35
41
|
|
36
42
|
## Installation
|
data/lib/barkick/version.rb
CHANGED
data/lib/gtin.rb
CHANGED
@@ -28,22 +28,19 @@ class GTIN
|
|
28
28
|
|
29
29
|
def valid?
|
30
30
|
if gtin14
|
31
|
-
|
32
|
-
digits = gtin14.split("").map(&:to_i)
|
33
|
-
# digit at position 0 is odd (first digit) for the purpose of this calculation
|
34
|
-
odd_digits, even_digits = digits.partition.each_with_index{|digit, i| i.even? }
|
35
|
-
(10 - (sum(odd_digits) * 3 + sum(even_digits)) % 10) % 10 != check_digit
|
31
|
+
self.class.check_digit(gtin14[0..-2]) == check_digit
|
36
32
|
else
|
37
33
|
false
|
38
34
|
end
|
39
35
|
end
|
40
36
|
|
41
37
|
def base_gtin14
|
42
|
-
|
43
|
-
|
44
|
-
base
|
38
|
+
if variable?
|
39
|
+
base = gtin14[0..-7] + "00000"
|
40
|
+
base + self.class.check_digit(base)
|
41
|
+
else
|
42
|
+
gtin14
|
45
43
|
end
|
46
|
-
base
|
47
44
|
end
|
48
45
|
|
49
46
|
# prefix
|
@@ -68,6 +65,70 @@ class GTIN
|
|
68
65
|
"GS1 Bulgaria"
|
69
66
|
when 383
|
70
67
|
"GS1 Slovenija"
|
68
|
+
when 385
|
69
|
+
"GS1 Croatia"
|
70
|
+
when 387
|
71
|
+
"GS1 BIH (Bosnia-Herzegovina)"
|
72
|
+
when 389
|
73
|
+
"GS1 Montenegro"
|
74
|
+
when 400..440
|
75
|
+
"GS1 Germany"
|
76
|
+
when 450..459, 490..499
|
77
|
+
"GS1 Japan"
|
78
|
+
when 460..469
|
79
|
+
"GS1 Russia"
|
80
|
+
when 470
|
81
|
+
"GS1 Kyrgyzstan"
|
82
|
+
when 471
|
83
|
+
"GS1 Taiwan"
|
84
|
+
when 474
|
85
|
+
"GS1 Estonia"
|
86
|
+
when 475
|
87
|
+
"GS1 Latvia"
|
88
|
+
when 476
|
89
|
+
"GS1 Azerbaijan"
|
90
|
+
when 477
|
91
|
+
"GS1 Lithuania"
|
92
|
+
when 478
|
93
|
+
"GS1 Uzbekistan"
|
94
|
+
when 479
|
95
|
+
"GS1 Sri Lanka"
|
96
|
+
when 480
|
97
|
+
"GS1 Philippines"
|
98
|
+
when 481
|
99
|
+
"GS1 Belarus"
|
100
|
+
when 482
|
101
|
+
"GS1 Ukraine"
|
102
|
+
when 484
|
103
|
+
"GS1 Moldova"
|
104
|
+
when 485
|
105
|
+
"GS1 Armenia"
|
106
|
+
when 486
|
107
|
+
"GS1 Georgia"
|
108
|
+
when 487
|
109
|
+
"GS1 Kazakstan"
|
110
|
+
when 488
|
111
|
+
"GS1 Tajikistan"
|
112
|
+
when 489
|
113
|
+
"GS1 Hong Kong"
|
114
|
+
when 500..509
|
115
|
+
"GS1 UK"
|
116
|
+
when 520..521
|
117
|
+
"GS1 Association Greece"
|
118
|
+
when 528
|
119
|
+
"GS1 Lebanon"
|
120
|
+
when 529
|
121
|
+
"GS1 Cyprus"
|
122
|
+
when 530
|
123
|
+
"GS1 Albania"
|
124
|
+
when 531
|
125
|
+
"GS1 MAC (FYR Macedonia)"
|
126
|
+
when 535
|
127
|
+
"GS1 Malta"
|
128
|
+
when 690..699
|
129
|
+
"GS1 China"
|
130
|
+
when 729
|
131
|
+
"GS1 Israel"
|
71
132
|
when 978..979
|
72
133
|
"Bookland"
|
73
134
|
else
|
@@ -97,9 +158,20 @@ class GTIN
|
|
97
158
|
end
|
98
159
|
end
|
99
160
|
|
100
|
-
|
161
|
+
def self.check_digit(number)
|
162
|
+
number = number.to_s
|
163
|
+
if [7, 11, 12, 13].include?(number.length)
|
164
|
+
# http://www.gs1.org/barcodes/support/check_digit_calculator#how
|
165
|
+
digits = number.rjust(13, "0").split("").map(&:to_i)
|
166
|
+
# digit at position 0 is odd (first digit) for the purpose of this calculation
|
167
|
+
odd_digits, even_digits = digits.partition.each_with_index{|digit, i| i.even? }
|
168
|
+
((10 - (sum(odd_digits) * 3 + sum(even_digits)) % 10) % 10).to_s
|
169
|
+
else
|
170
|
+
nil
|
171
|
+
end
|
172
|
+
end
|
101
173
|
|
102
|
-
def sum(arr)
|
174
|
+
def self.sum(arr)
|
103
175
|
arr.inject{|sum,x| sum + x }
|
104
176
|
end
|
105
177
|
|
data/test/gtin_test.rb
CHANGED
@@ -24,7 +24,8 @@ class TestGTIN < Minitest::Test
|
|
24
24
|
assert gtin.variable?
|
25
25
|
assert gtin.restricted?
|
26
26
|
assert_equal 8.63, gtin.price
|
27
|
-
assert_equal "
|
27
|
+
assert_equal "00299265000003", gtin.base_gtin14
|
28
|
+
assert GTIN.new(gtin.base_gtin14).valid?
|
28
29
|
end
|
29
30
|
|
30
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: barkick
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-09-
|
11
|
+
date: 2013-09-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|