gtin 0.0.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 +4 -0
- data/Manifest.txt +12 -0
- data/README.rdoc +63 -0
- data/Rakefile +26 -0
- data/lib/gtin.rb +6 -0
- data/lib/gtin/gtin.rb +107 -0
- data/lib/gtin/version.rb +9 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/test/test_gtin.rb +11 -0
- data/test/test_helper.rb +3 -0
- metadata +73 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
= gtin
|
2
|
+
|
3
|
+
* http://github.com/hmasing/gtin
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
The GTIN gem provides very simple functionality for verifying checksums on EAN-13 and UPC-A barcode values.
|
8
|
+
It also does very simple conversion between the two by left-padding with zeroes. This is not intended to
|
9
|
+
be a complex gem, but is certainly functional for applications that require that barcode values be
|
10
|
+
checked for valid checksums and length.
|
11
|
+
|
12
|
+
http://www.gtin.info/
|
13
|
+
|
14
|
+
Inspired by the EAN gem by Pascal Belloncle (nano RAILS)
|
15
|
+
|
16
|
+
== FEATURES/PROBLEMS:
|
17
|
+
|
18
|
+
* ---
|
19
|
+
|
20
|
+
== SYNOPSIS:
|
21
|
+
|
22
|
+
'731346000101'.ean? ==> false
|
23
|
+
'731346000101'.upc? ==> true
|
24
|
+
'731346000101'.to_ean ==> '0731346000101'
|
25
|
+
'731346000101'.to_gtin ==> '000731346000101'
|
26
|
+
|
27
|
+
'0731346000101'.valid_checksum? ==> true
|
28
|
+
'0731346000105'.valid_checksum? ==> false
|
29
|
+
|
30
|
+
Also adds .odd? and .even? to the Integer class
|
31
|
+
|
32
|
+
== REQUIREMENTS:
|
33
|
+
|
34
|
+
none
|
35
|
+
|
36
|
+
== INSTALL:
|
37
|
+
|
38
|
+
sudo gem install gtin
|
39
|
+
|
40
|
+
== LICENSE:
|
41
|
+
|
42
|
+
(The MIT License)
|
43
|
+
|
44
|
+
Copyright (c) 2010 Hans C. Masing & Dragonfly Depot, LLC
|
45
|
+
|
46
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
47
|
+
a copy of this software and associated documentation files (the
|
48
|
+
'Software'), to deal in the Software without restriction, including
|
49
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
50
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
51
|
+
permit persons to whom the Software is furnished to do so, subject to
|
52
|
+
the following conditions:
|
53
|
+
|
54
|
+
The above copyright notice and this permission notice shall be
|
55
|
+
included in all copies or substantial portions of the Software.
|
56
|
+
|
57
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
58
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
59
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
60
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
61
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
62
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
63
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'hoe', '>= 2.1.0'
|
3
|
+
require 'hoe'
|
4
|
+
require 'fileutils'
|
5
|
+
require './lib/gtin'
|
6
|
+
|
7
|
+
Hoe.plugin :newgem
|
8
|
+
# Hoe.plugin :website
|
9
|
+
# Hoe.plugin :cucumberfeatures
|
10
|
+
|
11
|
+
# Generate all the Rake tasks
|
12
|
+
# Run 'rake -T' to see list of generated tasks (from gem root directory)
|
13
|
+
$hoe = Hoe.spec 'gtin' do
|
14
|
+
self.developer 'FIXME full name', 'FIXME email'
|
15
|
+
self.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
|
16
|
+
self.rubyforge_name = self.name # TODO this is default value
|
17
|
+
# self.extra_deps = [['activesupport','>= 2.0.2']]
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'newgem/tasks'
|
22
|
+
Dir['tasks/**/*.rake'].each { |t| load t }
|
23
|
+
|
24
|
+
# TODO - want other tests/tasks run by default? Add them to the list
|
25
|
+
# remove_task :default
|
26
|
+
# task :default => [:spec, :features]
|
data/lib/gtin.rb
ADDED
data/lib/gtin/gtin.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# GTIN-12 (UPC-A): this is a 12-digit number used primarily in North America
|
2
|
+
# GTIN-8 (EAN/UCC-8): this is an 8-digit number used predominately outside of North America
|
3
|
+
# GTIN-13 (EAN/UCC-13): this is a 13-digit number used predominately outside of North America
|
4
|
+
# 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
|
+
module GTIN
|
7
|
+
def generate_check_digit
|
8
|
+
numbers = self.to_s.gsub(/[\D]+/, "").split(//)
|
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
|
25
|
+
end
|
26
|
+
|
27
|
+
def ean?
|
28
|
+
numbers = self.to_s.gsub(/[\D]+/, "").split(//)
|
29
|
+
|
30
|
+
checksum = 0
|
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
|
43
|
+
end
|
44
|
+
|
45
|
+
def upc?
|
46
|
+
value = self.to_s.gsub(/[\D]+/, "").split(//)
|
47
|
+
return false if value.length != 12
|
48
|
+
valid_checksum? value
|
49
|
+
end
|
50
|
+
|
51
|
+
# Determine if a gtin value has a valid checksum
|
52
|
+
def valid_checksum?(value)
|
53
|
+
checksum = 0
|
54
|
+
0.upto(value.length-2) do |i|
|
55
|
+
value%2 == 0 ?
|
56
|
+
(checksum += value[i].to_i * ((i-1)%2*3 +i%2)) :
|
57
|
+
(checksum += numbers[i].to_i * (i%2*3 +(i-1)%2))
|
58
|
+
end
|
59
|
+
value[-1].to_i == (10 - checksum % 10)%10
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
# GTIN-12 (UPC-A): this is a 12-digit number used primarily in North America
|
64
|
+
def to_gtin_12(number)
|
65
|
+
to_gtin(number, 12)
|
66
|
+
end
|
67
|
+
alias :to_upc :to_gtin_12
|
68
|
+
alias :to_upc_a :to_gtin_12
|
69
|
+
|
70
|
+
|
71
|
+
# GTIN-8 (EAN/UCC-8): this is an 8-digit number used predominately outside of North America
|
72
|
+
def to_gtin_8(number)
|
73
|
+
to_gtin(number, 8)
|
74
|
+
end
|
75
|
+
alias :to_ean_8 :to_gtin_8
|
76
|
+
alias :to_ucc_8 :to_gtin_8
|
77
|
+
|
78
|
+
|
79
|
+
# GTIN-13 (EAN/UCC-13): this is a 13-digit number used predominately outside of North America
|
80
|
+
def to_gtin_13(number)
|
81
|
+
to_gtin(number, 13)
|
82
|
+
end
|
83
|
+
alias :to_ean_13 :to_gtin_13
|
84
|
+
alias :to_ucc_13 :to_gtin_13
|
85
|
+
alias :to_ean :to_gtin_13
|
86
|
+
|
87
|
+
|
88
|
+
# 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(number)
|
90
|
+
to_gtin(number, 14)
|
91
|
+
end
|
92
|
+
alias :to_gtin :to_gtin_14
|
93
|
+
|
94
|
+
def to_gtin(number, size)
|
95
|
+
"%0#{size}d" % number.to_s.gsub(/[\D]+/, "")
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Extend String to include these methods
|
100
|
+
class String
|
101
|
+
include GTIN
|
102
|
+
end
|
103
|
+
|
104
|
+
# Extend Numberic to include these methods
|
105
|
+
class Numeric
|
106
|
+
include GTIN
|
107
|
+
end
|
data/lib/gtin/version.rb
ADDED
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/gtin.rb'}"
|
9
|
+
puts "Loading gtin gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
data/test/test_gtin.rb
ADDED
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gtin
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Hans Masing
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2009-04-25 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: GTIN provides basic validation, checksum and conversion functionality for UPC, EAN and GTIN barcode values.
|
22
|
+
email: rubygems@dragonflydepot.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- History.txt
|
31
|
+
- lib/gtin.rb
|
32
|
+
- lib/gtin/gtin.rb
|
33
|
+
- lib/gtin/version.rb
|
34
|
+
- Manifest.txt
|
35
|
+
- Rakefile
|
36
|
+
- README.rdoc
|
37
|
+
- script/console
|
38
|
+
- script/destroy
|
39
|
+
- script/generate
|
40
|
+
- test/test_gtin.rb
|
41
|
+
- test/test_helper.rb
|
42
|
+
has_rdoc: true
|
43
|
+
homepage: http://www.dragonflydepot.org/
|
44
|
+
licenses: []
|
45
|
+
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
|
49
|
+
require_paths:
|
50
|
+
- lib
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
segments:
|
56
|
+
- 0
|
57
|
+
version: "0"
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
version: "0"
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project:
|
68
|
+
rubygems_version: 1.3.6
|
69
|
+
signing_key:
|
70
|
+
specification_version: 3
|
71
|
+
summary: GTIN provides basic validation, checksum and conversion functionality for UPC, EAN and GTIN barcode values.
|
72
|
+
test_files: []
|
73
|
+
|