barkick 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 45f5a7c3383231e5e4107786fb5926919d8ad0a1
4
- data.tar.gz: b8bffcb8014de2797a6002102e9f8abd45188d13
3
+ metadata.gz: 4732796197cb3f499c19776c17606fbdf7cf097a
4
+ data.tar.gz: 650e4caf4c05222f4f15d9c901c7979cff6de510
5
5
  SHA512:
6
- metadata.gz: 95d919d68dfe43b6918655d2522ea9542f05f823d1c16c335c892a784545609e4a6c54a167e6672909b4383817074883e22219d70318cf6d633fde84f991cd08
7
- data.tar.gz: c7a9e605c78b6d821980a3af0352c7fa494e5e0bab24ff0aac8d2a7491a2228c3d1db0028076bc149c41e4169e524afe2e5721042d5406036d06989ecf17f74b
6
+ metadata.gz: 535faa9bb9760eebbbd3ab1a82a9771bb0c3edd9369e2670753ee70ec9b31f1c108a575a62322feaa4f5c300a52e1816da07eb2bb73c98519b363b2537971b02
7
+ data.tar.gz: 28256facb87f0572e287721630326bdcca78cb418a6b593f486faba210b0ef7e079f0ee814d23dafa981153270da43285f67491a46b046910359a625d08ca15a
data/README.md CHANGED
@@ -15,12 +15,13 @@ For PLU codes, check out the [plu gem](https://github.com/ankane/plu)
15
15
 
16
16
  ```ruby
17
17
  gtin = GTIN.new("016000275263")
18
- gtin.valid? # true
19
- gtin.gtin14 # "00016000275263"
20
- gtin.ean13 # "0016000275263"
21
- gtin.upc # "016000275263"
22
- gtin.prefix # "001"
23
- gtin.prefix_name # "GS1 US"
18
+ gtin.valid? # true
19
+ gtin.gtin14 # "00016000275263"
20
+ gtin.ean13 # "0016000275263"
21
+ gtin.upc # "016000275263"
22
+ gtin.prefix # "001"
23
+ gtin.prefix_name # "GS1 US"
24
+ gtin.country_code # "US"
24
25
  ```
25
26
 
26
27
  Variable items
@@ -33,6 +34,13 @@ gtin.price # 8.63
33
34
  gtin.base_gtin14 # "00299265000003"
34
35
  ```
35
36
 
37
+ UPC-E
38
+
39
+ ```ruby
40
+ gtin = GTIN.new("03744806")
41
+ gtin.base_gtin14 # "00037000004486"
42
+ ```
43
+
36
44
  Calculate check digit
37
45
 
38
46
  ```ruby
@@ -1,3 +1,3 @@
1
1
  module Barkick
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/lib/gtin.rb CHANGED
@@ -2,6 +2,29 @@ class GTIN
2
2
 
3
3
  def initialize(number)
4
4
  @number = number.to_s
5
+
6
+ # could be upc-e
7
+ if @number.length == 8 and @number[0] == "0" and @number[1] != "0"
8
+ upc_a =
9
+ case @number[-2]
10
+ when "0"
11
+ "#{@number[1..2]}00000#{@number[3..5]}"
12
+ when "1", "2"
13
+ "#{@number[1..2]}#{@number[-2]}0000#{@number[3..5]}"
14
+ when "3"
15
+ "#{@number[1..3]}00000#{@number[4..5]}"
16
+ when "4"
17
+ "#{@number[1..4]}00000#{@number[5]}"
18
+ else
19
+ "#{@number[1..5]}0000#{@number[-2]}"
20
+ end
21
+
22
+ upc_a = "0#{upc_a}#{@number[-1]}"
23
+
24
+ if self.class.check_digit(upc_a[0..-2]) == @number[-1]
25
+ @number = upc_a
26
+ end
27
+ end
5
28
  end
6
29
 
7
30
  def gtin14
@@ -49,90 +72,147 @@ class GTIN
49
72
  gtin14[1..3]
50
73
  end
51
74
 
52
- # TODO finish prefix list
53
75
  # http://www.gs1.org/barcodes/support/prefix_list
54
76
  def prefix_name
55
77
  case prefix.to_i
56
78
  when 0..19, 30..39, 60..139
57
- "GS1 US"
58
- when 20..29, 40..49, 200..299
59
- "Restricted distribution"
60
- when 50..59
61
- "Coupons"
62
- when 300..379
63
- "GS1 France"
64
- when 380
65
- "GS1 Bulgaria"
66
- when 383
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"
132
- when 978..979
133
- "Bookland"
134
- else
135
- nil
79
+ if @number.length == 8
80
+ nil # GTIN-8
81
+ else
82
+ "GS1 US"
83
+ end
84
+ when 20..29, 40..49, 200..299 then "Restricted distribution"
85
+ when 50..59 then "Coupons"
86
+ when 300..379 then "GS1 France"
87
+ when 380 then "GS1 Bulgaria"
88
+ when 383 then "GS1 Slovenija"
89
+ when 385 then "GS1 Croatia"
90
+ when 387 then "GS1 BIH (Bosnia-Herzegovina)"
91
+ when 389 then "GS1 Montenegro"
92
+ when 400..440 then "GS1 Germany"
93
+ when 450..459, 490..499 then "GS1 Japan"
94
+ when 460..469 then "GS1 Russia"
95
+ when 470 then "GS1 Kyrgyzstan"
96
+ when 471 then "GS1 Taiwan"
97
+ when 474 then "GS1 Estonia"
98
+ when 475 then "GS1 Latvia"
99
+ when 476 then "GS1 Azerbaijan"
100
+ when 477 then "GS1 Lithuania"
101
+ when 478 then "GS1 Uzbekistan"
102
+ when 479 then "GS1 Sri Lanka"
103
+ when 480 then "GS1 Philippines"
104
+ when 481 then "GS1 Belarus"
105
+ when 482 then "GS1 Ukraine"
106
+ when 484 then "GS1 Moldova"
107
+ when 485 then "GS1 Armenia"
108
+ when 486 then "GS1 Georgia"
109
+ when 487 then "GS1 Kazakstan"
110
+ when 488 then "GS1 Tajikistan"
111
+ when 489 then "GS1 Hong Kong"
112
+ when 500..509 then "GS1 UK"
113
+ when 520..521 then "GS1 Association Greece"
114
+ when 528 then "GS1 Lebanon"
115
+ when 529 then "GS1 Cyprus"
116
+ when 530 then "GS1 Albania"
117
+ when 531 then "GS1 MAC (FYR Macedonia)"
118
+ when 535 then "GS1 Malta"
119
+ when 539 then "GS1 Ireland"
120
+ when 540..549 then "GS1 Belgium & Luxembourg"
121
+ when 560 then "GS1 Portugal"
122
+ when 569 then "GS1 Iceland"
123
+ when 570..579 then "GS1 Denmark"
124
+ when 590 then "GS1 Poland"
125
+ when 594 then "GS1 Romania"
126
+ when 599 then "GS1 Hungary"
127
+ when 600..601 then "GS1 South Africa"
128
+ when 603 then "GS1 Ghana"
129
+ when 604 then "GS1 Senegal"
130
+ when 608 then "GS1 Bahrain"
131
+ when 609 then "GS1 Mauritius"
132
+ when 611 then "GS1 Morocco"
133
+ when 613 then "GS1 Algeria"
134
+ when 615 then "GS1 Nigeria"
135
+ when 616 then "GS1 Kenya"
136
+ when 618 then "GS1 Ivory Coast"
137
+ when 619 then "GS1 Tunisia"
138
+ when 620 then "GS1 Tanzania"
139
+ when 621 then "GS1 Syria"
140
+ when 622 then "GS1 Egypt"
141
+ when 623 then "GS1 Brunei"
142
+ when 624 then "GS1 Libya"
143
+ when 625 then "GS1 Jordan"
144
+ when 626 then "GS1 Iran"
145
+ when 627 then "GS1 Kuwait"
146
+ when 628 then "GS1 Saudi Arabia"
147
+ when 629 then "GS1 Emirates"
148
+ when 640..649 then "GS1 Finland"
149
+ when 690..699 then "GS1 China"
150
+ when 700..709 then "GS1 Norway"
151
+ when 729 then "GS1 Israel"
152
+ when 730..739 then "GS1 Sweden"
153
+ when 740 then "GS1 Guatemala"
154
+ when 741 then "GS1 El Salvador"
155
+ when 742 then "GS1 Honduras"
156
+ when 743 then "GS1 Nicaragua"
157
+ when 744 then "GS1 Costa Rica"
158
+ when 745 then "GS1 Panama"
159
+ when 746 then "GS1 Republica Dominicana"
160
+ when 750 then "GS1 Mexico"
161
+ when 754..755 then "GS1 Canada"
162
+ when 759 then "GS1 Venezuela"
163
+ when 760..769 then "GS1 Schweiz, Suisse, Svizzera"
164
+ when 770..771 then "GS1 Colombia"
165
+ when 773 then "GS1 Uruguay"
166
+ when 775 then "GS1 Peru"
167
+ when 777 then "GS1 Bolivia"
168
+ when 778..779 then "GS1 Argentina"
169
+ when 780 then "GS1 Chile"
170
+ when 784 then "GS1 Paraguay"
171
+ when 786 then "GS1 Ecuador"
172
+ when 789..790 then "GS1 Brasil"
173
+ when 800..839 then "GS1 Italy"
174
+ when 840..849 then "GS1 Spain"
175
+ when 850 then "GS1 Cuba"
176
+ when 858 then "GS1 Slovakia"
177
+ when 859 then "GS1 Czech"
178
+ when 860 then "GS1 Serbia"
179
+ when 865 then "GS1 Mongolia"
180
+ when 867 then "GS1 North Korea"
181
+ when 868..869 then "GS1 Turkey"
182
+ when 870..879 then "GS1 Netherlands"
183
+ when 880 then "GS1 South Korea"
184
+ when 884 then "GS1 Cambodia"
185
+ when 885 then "GS1 Thailand"
186
+ when 888 then "GS1 Singapore"
187
+ when 890 then "GS1 India"
188
+ when 893 then "GS1 Vietnam"
189
+ when 896 then "GS1 Pakistan"
190
+ when 899 then "GS1 Indonesia"
191
+ when 900..919 then "GS1 Austria"
192
+ when 930..939 then "GS1 Australia"
193
+ when 940..949 then "GS1 New Zealand"
194
+ when 950 then "GS1 Global Office"
195
+ when 951 then "GS1 Global Office (EPCglobal)"
196
+ when 955 then "GS1 Malaysia"
197
+ when 958 then "GS1 Macau"
198
+ when 960..969 then "Global Office (GTIN-8s)"
199
+ when 977 then "Serial publications (ISSN)"
200
+ when 978..979 then "Bookland"
201
+ when 980 then "Refund receipts"
202
+ when 981..984 then "GS1 coupon identification for common currency areas"
203
+ when 990..999 then "GS1 coupon identification"
204
+ end
205
+ end
206
+
207
+ def country_code
208
+ case prefix_name
209
+ when "GS1 US" then "US"
210
+ when "GS1 UK" then "GB"
211
+ when "GS1 Germany" then "DE"
212
+ when "GS1 Netherlands" then "NL"
213
+ when "GS1 Schweiz, Suisse, Svizzera" then "CH"
214
+ when "GS1 Italy" then "IT"
215
+ when "GS1 France" then "FR"
136
216
  end
137
217
  end
138
218
 
data/test/gtin_test.rb CHANGED
@@ -28,4 +28,21 @@ class TestGTIN < Minitest::Test
28
28
  assert GTIN.new(gtin.base_gtin14).valid?
29
29
  end
30
30
 
31
+ def test_upc_e
32
+ gtin = GTIN.new("03744806")
33
+ assert gtin.valid?
34
+ assert_equal "00037000004486", gtin.base_gtin14
35
+ end
36
+
37
+ def test_coke_upc_e
38
+ gtin = GTIN.new("04963406")
39
+ assert gtin.valid?
40
+ assert_equal "00049000006346", gtin.base_gtin14
41
+ end
42
+
43
+ def test_trader_joes
44
+ gtin = GTIN.new("00511292")
45
+ assert_equal "00000000511292", gtin.gtin14
46
+ end
47
+
31
48
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barkick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
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-29 00:00:00.000000000 Z
11
+ date: 2014-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.3'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.3'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: minitest
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: Barcodes made easy
@@ -59,7 +59,7 @@ executables: []
59
59
  extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
- - .gitignore
62
+ - ".gitignore"
63
63
  - Gemfile
64
64
  - LICENSE.txt
65
65
  - README.md
@@ -80,17 +80,17 @@ require_paths:
80
80
  - lib
81
81
  required_ruby_version: !ruby/object:Gem::Requirement
82
82
  requirements:
83
- - - '>='
83
+ - - ">="
84
84
  - !ruby/object:Gem::Version
85
85
  version: '0'
86
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  requirements:
88
- - - '>='
88
+ - - ">="
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 2.0.0
93
+ rubygems_version: 2.2.0
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Barcodes made easy