barkick 0.0.4 → 0.2.0

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
- SHA1:
3
- metadata.gz: 8c29847e11c73e474a7998ca499365027fa0125d
4
- data.tar.gz: a16f502702d4044a49952b62815ca3e92af35df3
2
+ SHA256:
3
+ metadata.gz: 01e8eea64a01cbd499ee4a39cba88f262b6a43954b7fda2010ff5e31ece1ead6
4
+ data.tar.gz: ba076bab4f6703027d48312d0ab427a69b97bbe68dc7aa3d5c5bac8ae4e11d56
5
5
  SHA512:
6
- metadata.gz: 05cc22474b6487531adb8f91d152f939159f6a674b4bca8ebd5f23219c2ebf6a783bab41bd177c3e0bf9d52ea866b41b577acab12eb69a67a2a10e58f7909ed1
7
- data.tar.gz: bde73c58c85fb516c9cd1911e96473a43ff70992a6a45fe9e894ba30c36615a13b7f46ff4371fc1c30ae77033c25e4b14360e5f10a96fca7ab2a86830da8b1c4
6
+ metadata.gz: 55d5d5b24587097bf264f8ab56ce731dee81cbfd54b1fe481e0ca86c026e73494c6225d1f45ae55b05c43abfda553bd64a0586240ae69db0cb9953c3329a29dd
7
+ data.tar.gz: adc1eab9da02f55bc98765613042be0240e066d3ab17c697876f719aa145372efca0d57a4bc9b154c49474d33ac207a32e5843386ab2d7c44f6c9da9bc670742
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## 0.2.0 (2022-10-09)
2
+
3
+ - Dropped support for Ruby < 2.7
4
+
5
+ ## 0.1.0 (2018-07-27)
6
+
7
+ - `GTIN` is now `Barkick::GTIN`
8
+ - 8-digit codes now raise an `ArgumentError` if `type` is not specified
9
+ - Added `type` option
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Andrew Kane
1
+ Copyright (c) 2013-2022 Andrew Kane
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -4,17 +4,27 @@ Barcodes made easy
4
4
 
5
5
  Works with:
6
6
 
7
- - [UPC](http://en.wikipedia.org/wiki/Universal_Product_Code)
8
- - [EAN](http://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29)
9
- - [GTIN](http://en.wikipedia.org/wiki/Global_Trade_Item_Number)
10
- - [ISBN](http://en.wikipedia.org/wiki/International_Standard_Book_Number)
7
+ - [UPC](https://en.wikipedia.org/wiki/Universal_Product_Code) (UPC-A and UPC-E)
8
+ - [EAN](https://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29) (EAN-13 and EAN-8)
9
+ - [GTIN](https://en.wikipedia.org/wiki/Global_Trade_Item_Number)
10
+ - [ISBN](https://en.wikipedia.org/wiki/International_Standard_Book_Number)
11
11
 
12
12
  For PLU codes, check out the [plu gem](https://github.com/ankane/plu)
13
13
 
14
+ [![Build Status](https://github.com/ankane/barkick/workflows/build/badge.svg?branch=master)](https://github.com/ankane/barkick/actions)
15
+
16
+ ## Installation
17
+
18
+ Add this line to your Gemfile:
19
+
20
+ ```ruby
21
+ gem "barkick"
22
+ ```
23
+
14
24
  ## How To Use
15
25
 
16
26
  ```ruby
17
- gtin = GTIN.new("016000275263")
27
+ gtin = Barkick::GTIN.new("016000275263")
18
28
  gtin.valid? # true
19
29
  gtin.gtin14 # "00016000275263"
20
30
  gtin.ean13 # "0016000275263"
@@ -27,7 +37,7 @@ gtin.country_code # "US"
27
37
  Variable items
28
38
 
29
39
  ```ruby
30
- gtin = GTIN.new("299265108631")
40
+ gtin = Barkick::GTIN.new("299265108631")
31
41
  gtin.variable? # true
32
42
  gtin.restricted? # true
33
43
  gtin.price # 8.63
@@ -37,34 +47,56 @@ gtin.base_gtin14 # "00299265000003"
37
47
  UPC-E
38
48
 
39
49
  ```ruby
40
- gtin = GTIN.new("03744806")
50
+ gtin = Barkick::GTIN.new("03744806", type: :upc_e)
41
51
  gtin.base_gtin14 # "00037000004486"
42
52
  ```
43
53
 
44
- Calculate check digit
54
+ EAN-8
45
55
 
46
56
  ```ruby
47
- GTIN.check_digit("01600027526") # "3"
57
+ gtin = Barkick::GTIN.new("01234565", type: :ean8)
58
+ gtin.base_gtin14 # "00000001234565"
48
59
  ```
49
60
 
50
- ## Installation
51
-
52
- Add this line to your Gemfile:
61
+ Calculate check digit
53
62
 
54
63
  ```ruby
55
- gem "barkick"
64
+ Barkick::GTIN.check_digit("01600027526") # "3"
56
65
  ```
57
66
 
58
- And run:
67
+ > For UPC-E, convert to UPC-A before passing to this method
59
68
 
60
- ```sh
61
- bundle
62
- ```
69
+ ## Resources
70
+
71
+ - [GS1 General Specifications](https://www.gs1.org/docs/barcodes/GS1_General_Specifications.pdf)
72
+
73
+ ## Upgrading
74
+
75
+ ### 0.1.0
76
+
77
+ There a few breaking changes to be aware of:
78
+
79
+ - `GTIN` is now `Barkick::GTIN`
80
+ - 8-digit codes now raise an `ArgumentError` if `type` is not specified
81
+
82
+ ## History
83
+
84
+ View the [changelog](https://github.com/ankane/barkick/blob/master/CHANGELOG.md)
63
85
 
64
86
  ## Contributing
65
87
 
66
- 1. Fork it
67
- 2. Create your feature branch (`git checkout -b my-new-feature`)
68
- 3. Commit your changes (`git commit -am 'Add some feature'`)
69
- 4. Push to the branch (`git push origin my-new-feature`)
70
- 5. Create new Pull Request
88
+ Everyone is encouraged to help improve this project. Here are a few ways you can help:
89
+
90
+ - [Report bugs](https://github.com/ankane/barkick/issues)
91
+ - Fix bugs and [submit pull requests](https://github.com/ankane/barkick/pulls)
92
+ - Write, clarify, or fix documentation
93
+ - Suggest or add new features
94
+
95
+ To get started with development:
96
+
97
+ ```sh
98
+ git clone https://github.com/ankane/barkick.git
99
+ cd barkick
100
+ bundle install
101
+ bundle exec rake test
102
+ ```
@@ -0,0 +1,255 @@
1
+ module Barkick
2
+ class GTIN
3
+ def initialize(number, type: nil)
4
+ @number = number.to_s
5
+
6
+ if @number.length == 8
7
+ raise ArgumentError, "Must specify type for 8-digit codes" unless type
8
+
9
+ if type == :upc_e
10
+ upc_a =
11
+ case @number[-2]
12
+ when "0"
13
+ "#{@number[1..2]}00000#{@number[3..5]}"
14
+ when "1", "2"
15
+ "#{@number[1..2]}#{@number[-2]}0000#{@number[3..5]}"
16
+ when "3"
17
+ "#{@number[1..3]}00000#{@number[4..5]}"
18
+ when "4"
19
+ "#{@number[1..4]}00000#{@number[5]}"
20
+ else
21
+ "#{@number[1..5]}0000#{@number[-2]}"
22
+ end
23
+
24
+ upc_a = "0#{upc_a}#{@number[-1]}"
25
+
26
+ if self.class.check_digit(upc_a[0..-2]) == @number[-1]
27
+ @number = upc_a
28
+ end
29
+ end
30
+ end
31
+
32
+ @type = type
33
+ end
34
+
35
+ def gtin14
36
+ if valid?
37
+ @number.rjust(14, "0")
38
+ else
39
+ nil
40
+ end
41
+ end
42
+
43
+ def gtin13
44
+ gtin14[0] == "0" ? gtin14[1..-1] : nil
45
+ end
46
+ alias_method :ean13, :gtin13
47
+
48
+ def gtin12
49
+ gtin14[0..1] == "00" ? gtin14[2..-1] : nil
50
+ end
51
+ alias_method :upc, :gtin12
52
+
53
+ def check_digit
54
+ gtin14[-1]
55
+ end
56
+
57
+ def valid?
58
+ @number =~ /\A\d{8}(\d{4,6})?\z/ && self.class.check_digit(@number.rjust(14, "0")[0..-2]) == @number[-1]
59
+ end
60
+
61
+ def base_gtin14
62
+ if variable?
63
+ base = gtin14[0..-7] + "00000"
64
+ base + self.class.check_digit(base)
65
+ else
66
+ gtin14
67
+ end
68
+ end
69
+
70
+ # prefix
71
+
72
+ def prefix
73
+ gtin14[1..3]
74
+ end
75
+
76
+ # https://www.gs1.org/barcodes/support/prefix_list
77
+ def prefix_name
78
+ case prefix.to_i
79
+ when 0..19, 30..39, 60..139
80
+ if @type == :ean8
81
+ nil # GTIN-8
82
+ else
83
+ "GS1 US"
84
+ end
85
+ when 20..29, 40..49, 200..299 then "Restricted distribution"
86
+ when 50..59 then "Coupons"
87
+ when 300..379 then "GS1 France"
88
+ when 380 then "GS1 Bulgaria"
89
+ when 383 then "GS1 Slovenija"
90
+ when 385 then "GS1 Croatia"
91
+ when 387 then "GS1 BIH (Bosnia-Herzegovina)"
92
+ when 389 then "GS1 Montenegro"
93
+ when 400..440 then "GS1 Germany"
94
+ when 450..459, 490..499 then "GS1 Japan"
95
+ when 460..469 then "GS1 Russia"
96
+ when 470 then "GS1 Kyrgyzstan"
97
+ when 471 then "GS1 Taiwan"
98
+ when 474 then "GS1 Estonia"
99
+ when 475 then "GS1 Latvia"
100
+ when 476 then "GS1 Azerbaijan"
101
+ when 477 then "GS1 Lithuania"
102
+ when 478 then "GS1 Uzbekistan"
103
+ when 479 then "GS1 Sri Lanka"
104
+ when 480 then "GS1 Philippines"
105
+ when 481 then "GS1 Belarus"
106
+ when 482 then "GS1 Ukraine"
107
+ when 484 then "GS1 Moldova"
108
+ when 485 then "GS1 Armenia"
109
+ when 486 then "GS1 Georgia"
110
+ when 487 then "GS1 Kazakstan"
111
+ when 488 then "GS1 Tajikistan"
112
+ when 489 then "GS1 Hong Kong"
113
+ when 500..509 then "GS1 UK"
114
+ when 520..521 then "GS1 Association Greece"
115
+ when 528 then "GS1 Lebanon"
116
+ when 529 then "GS1 Cyprus"
117
+ when 530 then "GS1 Albania"
118
+ when 531 then "GS1 MAC (FYR Macedonia)"
119
+ when 535 then "GS1 Malta"
120
+ when 539 then "GS1 Ireland"
121
+ when 540..549 then "GS1 Belgium & Luxembourg"
122
+ when 560 then "GS1 Portugal"
123
+ when 569 then "GS1 Iceland"
124
+ when 570..579 then "GS1 Denmark"
125
+ when 590 then "GS1 Poland"
126
+ when 594 then "GS1 Romania"
127
+ when 599 then "GS1 Hungary"
128
+ when 600..601 then "GS1 South Africa"
129
+ when 603 then "GS1 Ghana"
130
+ when 604 then "GS1 Senegal"
131
+ when 608 then "GS1 Bahrain"
132
+ when 609 then "GS1 Mauritius"
133
+ when 611 then "GS1 Morocco"
134
+ when 613 then "GS1 Algeria"
135
+ when 615 then "GS1 Nigeria"
136
+ when 616 then "GS1 Kenya"
137
+ when 618 then "GS1 Ivory Coast"
138
+ when 619 then "GS1 Tunisia"
139
+ when 620 then "GS1 Tanzania"
140
+ when 621 then "GS1 Syria"
141
+ when 622 then "GS1 Egypt"
142
+ when 623 then "GS1 Brunei"
143
+ when 624 then "GS1 Libya"
144
+ when 625 then "GS1 Jordan"
145
+ when 626 then "GS1 Iran"
146
+ when 627 then "GS1 Kuwait"
147
+ when 628 then "GS1 Saudi Arabia"
148
+ when 629 then "GS1 Emirates"
149
+ when 640..649 then "GS1 Finland"
150
+ when 690..699 then "GS1 China"
151
+ when 700..709 then "GS1 Norway"
152
+ when 729 then "GS1 Israel"
153
+ when 730..739 then "GS1 Sweden"
154
+ when 740 then "GS1 Guatemala"
155
+ when 741 then "GS1 El Salvador"
156
+ when 742 then "GS1 Honduras"
157
+ when 743 then "GS1 Nicaragua"
158
+ when 744 then "GS1 Costa Rica"
159
+ when 745 then "GS1 Panama"
160
+ when 746 then "GS1 Republica Dominicana"
161
+ when 750 then "GS1 Mexico"
162
+ when 754..755 then "GS1 Canada"
163
+ when 759 then "GS1 Venezuela"
164
+ when 760..769 then "GS1 Schweiz, Suisse, Svizzera"
165
+ when 770..771 then "GS1 Colombia"
166
+ when 773 then "GS1 Uruguay"
167
+ when 775 then "GS1 Peru"
168
+ when 777 then "GS1 Bolivia"
169
+ when 778..779 then "GS1 Argentina"
170
+ when 780 then "GS1 Chile"
171
+ when 784 then "GS1 Paraguay"
172
+ when 786 then "GS1 Ecuador"
173
+ when 789..790 then "GS1 Brasil"
174
+ when 800..839 then "GS1 Italy"
175
+ when 840..849 then "GS1 Spain"
176
+ when 850 then "GS1 Cuba"
177
+ when 858 then "GS1 Slovakia"
178
+ when 859 then "GS1 Czech"
179
+ when 860 then "GS1 Serbia"
180
+ when 865 then "GS1 Mongolia"
181
+ when 867 then "GS1 North Korea"
182
+ when 868..869 then "GS1 Turkey"
183
+ when 870..879 then "GS1 Netherlands"
184
+ when 880 then "GS1 South Korea"
185
+ when 884 then "GS1 Cambodia"
186
+ when 885 then "GS1 Thailand"
187
+ when 888 then "GS1 Singapore"
188
+ when 890 then "GS1 India"
189
+ when 893 then "GS1 Vietnam"
190
+ when 896 then "GS1 Pakistan"
191
+ when 899 then "GS1 Indonesia"
192
+ when 900..919 then "GS1 Austria"
193
+ when 930..939 then "GS1 Australia"
194
+ when 940..949 then "GS1 New Zealand"
195
+ when 950 then "GS1 Global Office"
196
+ when 951 then "GS1 Global Office (EPCglobal)"
197
+ when 955 then "GS1 Malaysia"
198
+ when 958 then "GS1 Macau"
199
+ when 960..969 then "Global Office (GTIN-8s)"
200
+ when 977 then "Serial publications (ISSN)"
201
+ when 978..979 then "Bookland"
202
+ when 980 then "Refund receipts"
203
+ when 981..984 then "GS1 coupon identification for common currency areas"
204
+ when 990..999 then "GS1 coupon identification"
205
+ end
206
+ end
207
+
208
+ def country_code
209
+ case prefix_name
210
+ when "GS1 US" then "US"
211
+ when "GS1 UK" then "GB"
212
+ when "GS1 Germany" then "DE"
213
+ when "GS1 Netherlands" then "NL"
214
+ when "GS1 Schweiz, Suisse, Svizzera" then "CH"
215
+ when "GS1 Italy" then "IT"
216
+ when "GS1 France" then "FR"
217
+ end
218
+ end
219
+
220
+ def book?
221
+ prefix_name == "Bookland"
222
+ end
223
+
224
+ def restricted?
225
+ prefix_name == "Restricted distribution"
226
+ end
227
+
228
+ # variable weight
229
+
230
+ def variable?
231
+ (20..29).cover?(prefix.to_i)
232
+ end
233
+
234
+ def price
235
+ if variable?
236
+ gtin14[-5..-2].to_f / 100
237
+ else
238
+ nil
239
+ end
240
+ end
241
+
242
+ def self.check_digit(number)
243
+ number = number.to_s
244
+ if [7, 11, 12, 13].include?(number.length)
245
+ # https://www.gs1.org/barcodes/support/check_digit_calculator#how
246
+ digits = number.rjust(13, "0").split("").map(&:to_i)
247
+ # digit at position 0 is odd (first digit) for the purpose of this calculation
248
+ odd_digits, even_digits = digits.partition.each_with_index { |_digit, i| i.even? }
249
+ ((10 - (odd_digits.sum * 3 + even_digits.sum) % 10) % 10).to_s
250
+ else
251
+ nil
252
+ end
253
+ end
254
+ end
255
+ end
@@ -1,3 +1,3 @@
1
1
  module Barkick
2
- VERSION = "0.0.4"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/barkick.rb CHANGED
@@ -1,2 +1,2 @@
1
+ require "barkick/gtin"
1
2
  require "barkick/version"
2
- require "gtin"
metadata CHANGED
@@ -1,80 +1,32 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: barkick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-18 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '1.3'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '1.3'
27
- - !ruby/object:Gem::Dependency
28
- name: rake
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :development
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: minitest
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- description: Barcodes made easy
56
- email:
57
- - andrew@chartkick.com
11
+ date: 2022-10-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: andrew@ankane.org
58
15
  executables: []
59
16
  extensions: []
60
17
  extra_rdoc_files: []
61
18
  files:
62
- - ".gitignore"
63
- - Gemfile
19
+ - CHANGELOG.md
64
20
  - LICENSE.txt
65
21
  - README.md
66
- - Rakefile
67
- - barkick.gemspec
68
22
  - lib/barkick.rb
23
+ - lib/barkick/gtin.rb
69
24
  - lib/barkick/version.rb
70
- - lib/gtin.rb
71
- - test/gtin_test.rb
72
- - test/test_helper.rb
73
25
  homepage: https://github.com/ankane/barkick
74
26
  licenses:
75
27
  - MIT
76
28
  metadata: {}
77
- post_install_message:
29
+ post_install_message:
78
30
  rdoc_options: []
79
31
  require_paths:
80
32
  - lib
@@ -82,18 +34,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
34
  requirements:
83
35
  - - ">="
84
36
  - !ruby/object:Gem::Version
85
- version: '0'
37
+ version: '2.7'
86
38
  required_rubygems_version: !ruby/object:Gem::Requirement
87
39
  requirements:
88
40
  - - ">="
89
41
  - !ruby/object:Gem::Version
90
42
  version: '0'
91
43
  requirements: []
92
- rubyforge_project:
93
- rubygems_version: 2.2.2
94
- signing_key:
44
+ rubygems_version: 3.3.7
45
+ signing_key:
95
46
  specification_version: 4
96
47
  summary: Barcodes made easy
97
- test_files:
98
- - test/gtin_test.rb
99
- - test/test_helper.rb
48
+ test_files: []
data/.gitignore DELETED
@@ -1,17 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in barkick.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- task :default => :test
5
- Rake::TestTask.new do |t|
6
- t.libs << "test"
7
- t.pattern = "test/**/*_test.rb"
8
- end
data/barkick.gemspec DELETED
@@ -1,24 +0,0 @@
1
- # coding: utf-8
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'barkick/version'
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "barkick"
8
- spec.version = Barkick::VERSION
9
- spec.authors = ["Andrew Kane"]
10
- spec.email = ["andrew@chartkick.com"]
11
- spec.description = %q{Barcodes made easy}
12
- spec.summary = %q{Barcodes made easy}
13
- spec.homepage = "https://github.com/ankane/barkick"
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
-
21
- spec.add_development_dependency "bundler", "~> 1.3"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "minitest"
24
- end
data/lib/gtin.rb DELETED
@@ -1,254 +0,0 @@
1
- class GTIN
2
-
3
- def initialize(number)
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
28
- end
29
-
30
- def gtin14
31
- if valid?
32
- @number.rjust(14, "0")
33
- else
34
- nil
35
- end
36
- end
37
-
38
- def gtin13
39
- gtin14[0] == "0" ? gtin14[1..-1] : nil
40
- end
41
- alias_method :ean13, :gtin13
42
-
43
- def gtin12
44
- gtin14[0..1] == "00" ? gtin14[2..-1] : nil
45
- end
46
- alias_method :upc, :gtin12
47
-
48
- def check_digit
49
- gtin14[-1]
50
- end
51
-
52
- def valid?
53
- @number =~ /\A\d{8}(\d{4,6})?\z/ && self.class.check_digit(@number.rjust(14, "0")[0..-2]) == @number[-1]
54
- end
55
-
56
- def base_gtin14
57
- if variable?
58
- base = gtin14[0..-7] + "00000"
59
- base + self.class.check_digit(base)
60
- else
61
- gtin14
62
- end
63
- end
64
-
65
- # prefix
66
-
67
- def prefix
68
- gtin14[1..3]
69
- end
70
-
71
- # http://www.gs1.org/barcodes/support/prefix_list
72
- def prefix_name
73
- case prefix.to_i
74
- when 0..19, 30..39, 60..139
75
- if @number.length == 8
76
- nil # GTIN-8
77
- else
78
- "GS1 US"
79
- end
80
- when 20..29, 40..49, 200..299 then "Restricted distribution"
81
- when 50..59 then "Coupons"
82
- when 300..379 then "GS1 France"
83
- when 380 then "GS1 Bulgaria"
84
- when 383 then "GS1 Slovenija"
85
- when 385 then "GS1 Croatia"
86
- when 387 then "GS1 BIH (Bosnia-Herzegovina)"
87
- when 389 then "GS1 Montenegro"
88
- when 400..440 then "GS1 Germany"
89
- when 450..459, 490..499 then "GS1 Japan"
90
- when 460..469 then "GS1 Russia"
91
- when 470 then "GS1 Kyrgyzstan"
92
- when 471 then "GS1 Taiwan"
93
- when 474 then "GS1 Estonia"
94
- when 475 then "GS1 Latvia"
95
- when 476 then "GS1 Azerbaijan"
96
- when 477 then "GS1 Lithuania"
97
- when 478 then "GS1 Uzbekistan"
98
- when 479 then "GS1 Sri Lanka"
99
- when 480 then "GS1 Philippines"
100
- when 481 then "GS1 Belarus"
101
- when 482 then "GS1 Ukraine"
102
- when 484 then "GS1 Moldova"
103
- when 485 then "GS1 Armenia"
104
- when 486 then "GS1 Georgia"
105
- when 487 then "GS1 Kazakstan"
106
- when 488 then "GS1 Tajikistan"
107
- when 489 then "GS1 Hong Kong"
108
- when 500..509 then "GS1 UK"
109
- when 520..521 then "GS1 Association Greece"
110
- when 528 then "GS1 Lebanon"
111
- when 529 then "GS1 Cyprus"
112
- when 530 then "GS1 Albania"
113
- when 531 then "GS1 MAC (FYR Macedonia)"
114
- when 535 then "GS1 Malta"
115
- when 539 then "GS1 Ireland"
116
- when 540..549 then "GS1 Belgium & Luxembourg"
117
- when 560 then "GS1 Portugal"
118
- when 569 then "GS1 Iceland"
119
- when 570..579 then "GS1 Denmark"
120
- when 590 then "GS1 Poland"
121
- when 594 then "GS1 Romania"
122
- when 599 then "GS1 Hungary"
123
- when 600..601 then "GS1 South Africa"
124
- when 603 then "GS1 Ghana"
125
- when 604 then "GS1 Senegal"
126
- when 608 then "GS1 Bahrain"
127
- when 609 then "GS1 Mauritius"
128
- when 611 then "GS1 Morocco"
129
- when 613 then "GS1 Algeria"
130
- when 615 then "GS1 Nigeria"
131
- when 616 then "GS1 Kenya"
132
- when 618 then "GS1 Ivory Coast"
133
- when 619 then "GS1 Tunisia"
134
- when 620 then "GS1 Tanzania"
135
- when 621 then "GS1 Syria"
136
- when 622 then "GS1 Egypt"
137
- when 623 then "GS1 Brunei"
138
- when 624 then "GS1 Libya"
139
- when 625 then "GS1 Jordan"
140
- when 626 then "GS1 Iran"
141
- when 627 then "GS1 Kuwait"
142
- when 628 then "GS1 Saudi Arabia"
143
- when 629 then "GS1 Emirates"
144
- when 640..649 then "GS1 Finland"
145
- when 690..699 then "GS1 China"
146
- when 700..709 then "GS1 Norway"
147
- when 729 then "GS1 Israel"
148
- when 730..739 then "GS1 Sweden"
149
- when 740 then "GS1 Guatemala"
150
- when 741 then "GS1 El Salvador"
151
- when 742 then "GS1 Honduras"
152
- when 743 then "GS1 Nicaragua"
153
- when 744 then "GS1 Costa Rica"
154
- when 745 then "GS1 Panama"
155
- when 746 then "GS1 Republica Dominicana"
156
- when 750 then "GS1 Mexico"
157
- when 754..755 then "GS1 Canada"
158
- when 759 then "GS1 Venezuela"
159
- when 760..769 then "GS1 Schweiz, Suisse, Svizzera"
160
- when 770..771 then "GS1 Colombia"
161
- when 773 then "GS1 Uruguay"
162
- when 775 then "GS1 Peru"
163
- when 777 then "GS1 Bolivia"
164
- when 778..779 then "GS1 Argentina"
165
- when 780 then "GS1 Chile"
166
- when 784 then "GS1 Paraguay"
167
- when 786 then "GS1 Ecuador"
168
- when 789..790 then "GS1 Brasil"
169
- when 800..839 then "GS1 Italy"
170
- when 840..849 then "GS1 Spain"
171
- when 850 then "GS1 Cuba"
172
- when 858 then "GS1 Slovakia"
173
- when 859 then "GS1 Czech"
174
- when 860 then "GS1 Serbia"
175
- when 865 then "GS1 Mongolia"
176
- when 867 then "GS1 North Korea"
177
- when 868..869 then "GS1 Turkey"
178
- when 870..879 then "GS1 Netherlands"
179
- when 880 then "GS1 South Korea"
180
- when 884 then "GS1 Cambodia"
181
- when 885 then "GS1 Thailand"
182
- when 888 then "GS1 Singapore"
183
- when 890 then "GS1 India"
184
- when 893 then "GS1 Vietnam"
185
- when 896 then "GS1 Pakistan"
186
- when 899 then "GS1 Indonesia"
187
- when 900..919 then "GS1 Austria"
188
- when 930..939 then "GS1 Australia"
189
- when 940..949 then "GS1 New Zealand"
190
- when 950 then "GS1 Global Office"
191
- when 951 then "GS1 Global Office (EPCglobal)"
192
- when 955 then "GS1 Malaysia"
193
- when 958 then "GS1 Macau"
194
- when 960..969 then "Global Office (GTIN-8s)"
195
- when 977 then "Serial publications (ISSN)"
196
- when 978..979 then "Bookland"
197
- when 980 then "Refund receipts"
198
- when 981..984 then "GS1 coupon identification for common currency areas"
199
- when 990..999 then "GS1 coupon identification"
200
- end
201
- end
202
-
203
- def country_code
204
- case prefix_name
205
- when "GS1 US" then "US"
206
- when "GS1 UK" then "GB"
207
- when "GS1 Germany" then "DE"
208
- when "GS1 Netherlands" then "NL"
209
- when "GS1 Schweiz, Suisse, Svizzera" then "CH"
210
- when "GS1 Italy" then "IT"
211
- when "GS1 France" then "FR"
212
- end
213
- end
214
-
215
- def book?
216
- prefix_name == "Bookland"
217
- end
218
-
219
- def restricted?
220
- prefix_name == "Restricted distribution"
221
- end
222
-
223
- # variable weight
224
-
225
- def variable?
226
- (20..29).cover?(prefix.to_i)
227
- end
228
-
229
- def price
230
- if variable?
231
- gtin14[-5..-2].to_f / 100
232
- else
233
- nil
234
- end
235
- end
236
-
237
- def self.check_digit(number)
238
- number = number.to_s
239
- if [7, 11, 12, 13].include?(number.length)
240
- # http://www.gs1.org/barcodes/support/check_digit_calculator#how
241
- digits = number.rjust(13, "0").split("").map(&:to_i)
242
- # digit at position 0 is odd (first digit) for the purpose of this calculation
243
- odd_digits, even_digits = digits.partition.each_with_index{|digit, i| i.even? }
244
- ((10 - (sum(odd_digits) * 3 + sum(even_digits)) % 10) % 10).to_s
245
- else
246
- nil
247
- end
248
- end
249
-
250
- def self.sum(arr)
251
- arr.inject{|sum,x| sum + x }
252
- end
253
-
254
- end
data/test/gtin_test.rb DELETED
@@ -1,49 +0,0 @@
1
- require_relative "test_helper"
2
-
3
- class TestGTIN < Minitest::Test
4
-
5
- def test_gtin
6
- gtin = GTIN.new("016000275263")
7
- assert gtin.valid?
8
- assert_equal "00016000275263", gtin.gtin14
9
- assert_equal "0016000275263", gtin.gtin13
10
- assert_equal "016000275263", gtin.gtin12
11
- assert_equal "0016000275263", gtin.ean13
12
- assert_equal "016000275263", gtin.upc
13
- assert_equal "001", gtin.prefix
14
- assert_equal "GS1 US", gtin.prefix_name
15
- end
16
-
17
- def test_invalid
18
- assert !GTIN.new("1").valid?
19
- assert !GTIN.new(" 016000275263").valid?
20
- end
21
-
22
- def test_variable
23
- gtin = GTIN.new("299265108631")
24
- assert gtin.valid?
25
- assert gtin.variable?
26
- assert gtin.restricted?
27
- assert_equal 8.63, gtin.price
28
- assert_equal "00299265000003", gtin.base_gtin14
29
- assert GTIN.new(gtin.base_gtin14).valid?
30
- end
31
-
32
- def test_upc_e
33
- gtin = GTIN.new("03744806")
34
- assert gtin.valid?
35
- assert_equal "00037000004486", gtin.base_gtin14
36
- end
37
-
38
- def test_coke_upc_e
39
- gtin = GTIN.new("04963406")
40
- assert gtin.valid?
41
- assert_equal "00049000006346", gtin.base_gtin14
42
- end
43
-
44
- def test_trader_joes
45
- gtin = GTIN.new("00511292")
46
- assert_equal "00000000511292", gtin.gtin14
47
- end
48
-
49
- end
data/test/test_helper.rb DELETED
@@ -1,4 +0,0 @@
1
- require "bundler/setup"
2
- Bundler.require(:default)
3
- require "minitest/autorun"
4
- require "minitest/pride"