ean 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data.tar.gz.sig ADDED
Binary file
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 0.1.0 / 2008-02-23
2
+
3
+ * First release
4
+ * validates 8. 12. 13. 14 GTIN/UPC/EAN identifiers
5
+ * generates 8. 12. 13. 14 GTIN/UPC/EAN identifiers
6
+
data/Licence.txt ADDED
@@ -0,0 +1,22 @@
1
+ (The MIT License)
2
+
3
+ Copyright (c) 2008 Pascal Belloncle (http://blog.nanorails.com)
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ 'Software'), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Manifest.txt ADDED
@@ -0,0 +1,16 @@
1
+ History.txt
2
+ Licence.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ TODO
7
+ bin/ean
8
+ lib/ean.rb
9
+ lib/ean/ean.rb
10
+ lib/ean/tasks.rb
11
+ lib/ean/upcdatabase.rb
12
+ lib/ean/version.rb
13
+ spec/ean_spec.rb
14
+ spec/spec.opts
15
+ spec/spec_helper.rb
16
+ tasks/rspec.rake
data/README.txt ADDED
@@ -0,0 +1,35 @@
1
+ = ean
2
+
3
+ * http://blog.nanorails.com/ean-upc
4
+ * http://rubyforge.org/projects/ean/
5
+
6
+ == DESCRIPTION:
7
+
8
+ Provide validation/generation for UPC/EAN/GTIN numbers
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * Validate UPC/EAN/GTIN identifiers
13
+
14
+ * Generate check number for UPC/EAN/GTIN identifiers
15
+
16
+ == SYNOPSIS:
17
+
18
+ require "rubygems"
19
+ require "ean"
20
+
21
+ "1234567890128".to_gtin
22
+
23
+ "1-234567-890128".ean?
24
+
25
+ "1234567890128".to_gtin.ean?
26
+
27
+ "784794001602".generate_check_digit
28
+
29
+ == REQUIREMENTS:
30
+
31
+ * hoe 1.5 (to rebuild)
32
+
33
+ == INSTALL:
34
+
35
+ * sudo gem install ean
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/ean.rb'
6
+ require './lib/ean/tasks'
7
+
8
+ Hoe.new('ean', EAN::VERSION::STRING) do |p|
9
+ p.rubyforge_name = 'ean'
10
+ p.author = 'Pascal Belloncle (nano RAILS)'
11
+ p.email = 'psq@nanorails.com'
12
+ # p.extra_deps << ['main', '>= 2.8.0']
13
+ # p.description = p.paragraphs_of('README.txt', 2..5).join("\n\n")
14
+ # p.url = p.paragraphs_of('README.txt', 0).first.split(/\n/)[1..-1]
15
+ # p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
16
+ end
17
+
18
+ # vim: syntax=Ruby
data/TODO ADDED
@@ -0,0 +1,3 @@
1
+ * Integration with upcdatabase.com (either via XML-RPC or downloaded filed, need to decide what's needed first)
2
+
3
+ * Support more formats. Which one is the real question. Will take contributions (with specs!)
data/bin/ean ADDED
File without changes
data/lib/ean.rb ADDED
@@ -0,0 +1,5 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require "ean/version"
4
+
5
+ require "ean/ean"
data/lib/ean/ean.rb ADDED
@@ -0,0 +1,77 @@
1
+ # UPC-A
2
+ # 12 numeric values
3
+ # GTIN-12
4
+
5
+ # EAN-13
6
+ # 13 numeric values
7
+ # GTIN-13
8
+
9
+ # UPC-E
10
+ # 12 numeric values, suppressed 0s
11
+ # GTIN-12
12
+
13
+ # EAN-8
14
+ # 8 numeric values
15
+
16
+ module EAN
17
+ def generate_check_digit
18
+ numbers = self.to_s.gsub(/[\D]+/, "").split(//)
19
+
20
+ checksum = 0
21
+ case numbers.length
22
+ when 7
23
+ 0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
24
+ when 11
25
+ 0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
26
+ when 12
27
+ 0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * (i%2*3 +(i-1)%2) end
28
+ when 13
29
+ 0.upto(numbers.length-1) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
30
+ else
31
+ 0
32
+ end
33
+
34
+ return ((10 - checksum % 10)%10).to_s
35
+ end
36
+
37
+ def ean?
38
+ numbers = self.to_s.gsub(/[\D]+/, "").split(//)
39
+
40
+ checksum = 0
41
+ case numbers.length
42
+ when 8
43
+ 0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
44
+ when 12
45
+ 0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
46
+ when 13
47
+ 0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * (i%2*3 +(i-1)%2) end
48
+ when 14
49
+ 0.upto(numbers.length-2) do |i| checksum += numbers[i].to_i * ((i-1)%2*3 +i%2) end
50
+ else
51
+ 0
52
+ end
53
+
54
+ return numbers[-1].to_i == (10 - checksum % 10)%10
55
+ end
56
+
57
+ def to_gtin
58
+ numbers = self.to_s.gsub(/[\D]+/, "")
59
+ case numbers.length
60
+ when 8
61
+ '000000'+numbers
62
+ when 12
63
+ '00'+numbers
64
+ when 13
65
+ '0'+numbers
66
+ when 14
67
+ numbers
68
+ else
69
+ '00000000000000'
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ class String
76
+ include EAN
77
+ end
data/lib/ean/tasks.rb ADDED
@@ -0,0 +1 @@
1
+ Dir[File.join(File.dirname(__FILE__), %w[.. .. tasks], '**/*.rake')].each { |rake| load rake }
@@ -0,0 +1,11 @@
1
+ #nothing useful here yet. For future version
2
+ require 'xmlrpc/client'
3
+
4
+ server = XMLRPC::Client.new2('http://www.upcdatabase.com/rpc')
5
+ @item = server.call('lookupUPC', '720642442524')
6
+
7
+ # puts @item
8
+ # => {"issuerCountry"=>"United States", "message"=>"Database entry found", "size"=>"CD", "pendingUpdates"=>0, "lastModified"=>"2001-06-24 15:17:47", "found"=>true, "mfr_comment"=>"Manufacturer is not known", "issuerCountryCode"=>"us", "description"=>"Nirvana - Nevermind", "ean"=>"0720642442524", "upc"=>"720642442524", "isCoupon"=>false}
9
+
10
+ # puts "Description :: " + @item["description"]
11
+ # puts "Type :: " + @item["size"]
@@ -0,0 +1,9 @@
1
+ module EAN #:nodoc:
2
+ module VERSION #:nodoc:
3
+ MAJOR = 0
4
+ MINOR = 1
5
+ TINY = 0
6
+
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ end
9
+ end
data/spec/ean_spec.rb ADDED
@@ -0,0 +1,101 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe "EAN::Ean" do
4
+ include EAN::SpecHelpers
5
+
6
+ before(:each) do
7
+ end
8
+
9
+ after(:each) do
10
+ end
11
+
12
+ it "should generate for all zeros" do
13
+ '0-00000-00000'.generate_check_digit.should == '0'
14
+ end
15
+
16
+ it "should generate for single 1s" do
17
+ '1-00000-00000'.generate_check_digit.should == '7'
18
+ '0-10000-00000'.generate_check_digit.should == '9'
19
+ '0-01000-00000'.generate_check_digit.should == '7'
20
+ '0-00100-00000'.generate_check_digit.should == '9'
21
+ '0-00010-00000'.generate_check_digit.should == '7'
22
+ '0-00001-00000'.generate_check_digit.should == '9'
23
+ '0-00000-10000'.generate_check_digit.should == '7'
24
+ '0-00000-01000'.generate_check_digit.should == '9'
25
+ '0-00000-00100'.generate_check_digit.should == '7'
26
+ '0-00000-00010'.generate_check_digit.should == '9'
27
+ '0-00000-00001'.generate_check_digit.should == '7'
28
+ end
29
+
30
+ it "should generate for real numbers" do
31
+ samples_values.each do |value|
32
+ (value.chop+value.chop.generate_check_digit).should == value
33
+ end
34
+ end
35
+
36
+ it "should validate for GTIN-8" do
37
+ "1234555".generate_check_digit.should == "7"
38
+ end
39
+
40
+ it "should generate for GTIN-13" do
41
+ "123456789012".generate_check_digit.should == "8"
42
+ end
43
+
44
+ it "should generate for GTIN-14" do
45
+ "1234567890123".generate_check_digit.should == "1"
46
+ end
47
+
48
+ it "should ignore non numbers" do
49
+ "7sdasdasda8sdsa47asdsa9asda4as0adsa0asda1adsa60asdasdas!${%^&*}2".ean?.should == true
50
+ end
51
+
52
+ it "should validate for all zeros" do
53
+ '0-00000-00000-0'.generate_check_digit.should == '0'
54
+ end
55
+
56
+ it "should validate for single 1s" do
57
+ '1-00000-00000-7'.ean?.should == true
58
+ '0-10000-00000-9'.ean?.should == true
59
+ '0-01000-00000-7'.ean?.should == true
60
+ '0-00100-00000-9'.ean?.should == true
61
+ '0-00010-00000-7'.ean?.should == true
62
+ '0-00001-00000-9'.ean?.should == true
63
+ '0-00000-10000-7'.ean?.should == true
64
+ '0-00000-01000-9'.ean?.should == true
65
+ '0-00000-00100-7'.ean?.should == true
66
+ '0-00000-00010-9'.ean?.should == true
67
+ '0-00000-00001-7'.ean?.should == true
68
+ end
69
+
70
+ it "should validate for real numbers" do
71
+ samples_values.each do |value|
72
+ value.ean?.should == true
73
+ end
74
+ end
75
+
76
+ it "should validate for GTIN-8" do
77
+ "12345557".ean?.should == true
78
+ end
79
+
80
+ it "should validate for GTIN-13" do
81
+ "1234567890128".ean?.should == true
82
+ end
83
+
84
+ it "should validate for GTIN-14" do
85
+ "12345678901231".ean?.should == true
86
+ end
87
+
88
+ it "should convert all 4 cases to gtin" do
89
+ "12345557".to_gtin.should == "00000012345557"
90
+ "784794001602".to_gtin.should == "00784794001602"
91
+ "1234567890128".to_gtin.should == "01234567890128"
92
+ "12345678901231".to_gtin.should == "12345678901231"
93
+ end
94
+
95
+ it "should validate converted gtin-14" do
96
+ "12345557".to_gtin.ean?.should == true
97
+ "784794001602".to_gtin.ean?.should == true
98
+ "1234567890128".to_gtin.ean?.should == true
99
+ "12345678901231".to_gtin.ean?.should == true
100
+ end
101
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1,3 @@
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
@@ -0,0 +1,118 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+
8
+ dir = File.dirname(__FILE__)
9
+ lib_path = File.expand_path("#{dir}/../lib")
10
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
11
+
12
+ require 'ean'
13
+
14
+ require "logger"
15
+ require "fileutils"
16
+
17
+ $logger = Logger.new($stderr)
18
+ $logger.level = Logger::INFO unless $DEBUG
19
+
20
+ module EAN
21
+ module SpecHelpers
22
+ def logger
23
+ $logger
24
+ end
25
+
26
+ # 0-98893-01020-3 6?
27
+ # 0-87396-9943107
28
+ # 0-867850-78133-4
29
+ # 7-55517-20042-5 8?
30
+ # 0-80887-49478-2 9?
31
+
32
+
33
+ def samples_values
34
+ %w(
35
+ 7-84794-00160-2
36
+ 0-85798-02872-6
37
+ 8-92675-00010-8
38
+ 0-97546-10200-8
39
+ 7-29469-63104-3
40
+ 7-26299-10020-6
41
+ 0-87447-21872-4
42
+ 0-88593-12002-2
43
+ 0-80696-09642-6
44
+ 7-33952-99710-3
45
+ 0-87110-31277-3
46
+ 0-13357-80199-2
47
+ 6-64228-48662-8
48
+ 0-29679-36042-0
49
+ 0-83085-30096-8
50
+ 0-85000-01163-8
51
+ 7-26299-10020-6
52
+ 8-53739-00002-1
53
+ 6-52935-10001-2
54
+ 0-83085-90701-3
55
+ 0-89819-79688-5
56
+ 0-10986-00303-2
57
+ 0-85000-01213-0
58
+ 8-43700-203230
59
+ 1-88985-00013-7
60
+ 8-437004-998163
61
+ 0-87396-00410-2
62
+ 8-435053-384036
63
+ 0-82896-78040-2
64
+ 0-89636-46000-6
65
+ 0-85000-01211-6
66
+ 0-18138-20902-8
67
+ 0-87512-92005-7
68
+ 0-80887-49374-4
69
+ 0-80732-24701-0
70
+ 0-85000-01054-9
71
+ 0-83300-09551-8
72
+ 0-85000-0103702
73
+ 0-88534-00143-4
74
+ 0-81308-83747-5
75
+ 0-31259-01849-2
76
+ 8-92186-00025-3
77
+ 0-88593-10005-5
78
+ 8-51718-00002-4
79
+ 0-83837-00616-2
80
+ 0-83085-20022-0
81
+ 0-40426-99251-0
82
+ 8-53763-00102-5
83
+ 0-88553-09349-6
84
+ 0-96619-66555-6
85
+ 7-26319-10001-8
86
+ 0-87356-40512-3
87
+ 0-00579-00005-0
88
+ 0-71570-02009-5
89
+ 0-81753-05020-7
90
+ 0-85155-00003-7
91
+ 7-28691-44674-6
92
+ 0-21130-51506-6
93
+ 0-87512-92005-7
94
+ 0-12894-84511-7
95
+ 7-15126-00001-7
96
+ 0-18138-20902-8
97
+ 0-80887-49385-0
98
+ 0-12354-08770-5
99
+ 0-12354-08765-1
100
+ 7-24826-02125-3
101
+ 0-89121-18852-1
102
+ 0-80696-05750-2
103
+ 0-98652-90000-7
104
+ 8-58517-00043-9
105
+ 8-58517-00048-4
106
+ 0-18138-20903-5
107
+ 7-18742-00005-8
108
+ 0-00579-00005-0
109
+ 0-88232-00261-0
110
+ 0-99988-07109-6
111
+ 0-88534-00143-4
112
+ 8-19960-00021-6
113
+ 7-17888-79112-7
114
+
115
+ )
116
+ end
117
+ end
118
+ end
data/tasks/rspec.rake ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'spec'
3
+ rescue LoadError
4
+ require 'rubygems'
5
+ require 'spec'
6
+ end
7
+ begin
8
+ require 'spec/rake/spectask'
9
+ rescue LoadError
10
+ puts <<-EOS
11
+ To use rspec for testing you must install rspec gem:
12
+ gem install rspec
13
+ EOS
14
+ exit(0)
15
+ end
16
+
17
+ desc "Run the specs under spec"
18
+ Spec::Rake::SpecTask.new do |t|
19
+ t.spec_opts = ['--options', "spec/spec.opts"]
20
+ t.spec_files = FileList['spec/*_spec.rb','spec/commands/*_spec.rb']
21
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ean
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pascal Belloncle (nano RAILS)
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIDMDCCAhigAwIBAgIBADANBgkqhkiG9w0BAQUFADA+MQwwCgYDVQQDDANwc3Ex
14
+ GTAXBgoJkiaJk/IsZAEZFgluYW5vcmFpbHMxEzARBgoJkiaJk/IsZAEZFgNjb20w
15
+ HhcNMDgwMjA0MDgzOTU3WhcNMDkwMjAzMDgzOTU3WjA+MQwwCgYDVQQDDANwc3Ex
16
+ GTAXBgoJkiaJk/IsZAEZFgluYW5vcmFpbHMxEzARBgoJkiaJk/IsZAEZFgNjb20w
17
+ ggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC9ArQhN6DW+QBDgRR4dYn1
18
+ F6Y4O4Gmetew3JHLuNHPuTHZxYoallfj+Kq/9GBh+gaxCWECem621M4DcHug2qzR
19
+ y28ojmdCwIs3vVp8HSVpPgsTCYU1hTazcf6m1n1GJzK7/Cn5tVpKtmpggMg3KRet
20
+ B/gLZ7V+Wmobwc9hMOq7M1EJWGGu6RO4O3AVATJbhJJrxik7fudIIxVsQXHXfSbM
21
+ 4I+ckDg8rXQFLT05ZaeXluykbgpaBbqukEkmXdcRq7r+xHQiifyukW91/hOcI0VI
22
+ uymCxquaoBe6BbJyCGAqS/LAx9ghUeauiupXk7MQ2SJZR52EsAgsLwk6S6sdnmVl
23
+ AgMBAAGjOTA3MAkGA1UdEwQCMAAwCwYDVR0PBAQDAgSwMB0GA1UdDgQWBBQqXXgt
24
+ NiMYFw60w5BVh9ZfZ35vYzANBgkqhkiG9w0BAQUFAAOCAQEABp1bJbQA89xE2eUQ
25
+ P70MsQxPARP9WDZ3K8/U3iwWxFBwqBkN3rA1huu9ffDSGKuReLt1peHrITUuTGwX
26
+ XH1kvHzCs+GMxWYvvz07NNhM+Y+mAgqpKrZnLhoWrrQyz0TEUpCmoUgDzc1evn6V
27
+ 1pyu7zUQ6TFX20kYzOupVAwLfhaUv9Smj1YI/YI8lkqNJqpJLbm6Ib0Gr/sLkY/E
28
+ o1mnIpaqkxNMBS+u8/ToejKdRYOtoXb5134I8WSbkVH2mTHlVgLPVRR2uqxxyQTD
29
+ bYaWAJcLlC+oPUfD5uc2DkmnjUyAWSIEVQcL20dwU8WbYWp/oFmiRpvgYk4LLFs2
30
+ TCvXyw==
31
+ -----END CERTIFICATE-----
32
+
33
+ date: 2008-02-23 00:00:00 -08:00
34
+ default_executable:
35
+ dependencies:
36
+ - !ruby/object:Gem::Dependency
37
+ name: hoe
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 1.5.0
44
+ version:
45
+ description: Provide validation/generation for UPC/EAN/GTIN numbers
46
+ email: psq@nanorails.com
47
+ executables:
48
+ - ean
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - History.txt
53
+ - Licence.txt
54
+ - Manifest.txt
55
+ - README.txt
56
+ files:
57
+ - History.txt
58
+ - Licence.txt
59
+ - Manifest.txt
60
+ - README.txt
61
+ - Rakefile
62
+ - TODO
63
+ - bin/ean
64
+ - lib/ean.rb
65
+ - lib/ean/ean.rb
66
+ - lib/ean/tasks.rb
67
+ - lib/ean/upcdatabase.rb
68
+ - lib/ean/version.rb
69
+ - spec/ean_spec.rb
70
+ - spec/spec.opts
71
+ - spec/spec_helper.rb
72
+ - tasks/rspec.rake
73
+ has_rdoc: true
74
+ homepage: http://blog.nanorails.com/ean-upc
75
+ post_install_message:
76
+ rdoc_options:
77
+ - --main
78
+ - README.txt
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: "0"
86
+ version:
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: "0"
92
+ version:
93
+ requirements: []
94
+
95
+ rubyforge_project: ean
96
+ rubygems_version: 1.0.0
97
+ signing_key:
98
+ specification_version: 2
99
+ summary: Provide validation/generation for UPC/EAN/GTIN numbers
100
+ test_files: []
101
+
metadata.gz.sig ADDED
Binary file