ean13 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +2 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +35 -0
- data/lib/ean13.rb +57 -0
- data/spec/ean13_spec.rb +41 -0
- metadata +58 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 Peter Yandell
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
A small class for generating and validating EAN-13's, the 13 digit codes found
|
2
|
+
on many products sold around the world.
|
3
|
+
|
4
|
+
= Installation
|
5
|
+
|
6
|
+
gem install ean13
|
7
|
+
|
8
|
+
= Usage
|
9
|
+
|
10
|
+
EAN13.new("0632737715836").valid?
|
11
|
+
=> true
|
12
|
+
|
13
|
+
EAN13.valid?("0632737715836")
|
14
|
+
=> true
|
15
|
+
|
16
|
+
EAN13.valid?("0632737715837")
|
17
|
+
=> false
|
18
|
+
|
19
|
+
EAN13.complete("063273771583")
|
20
|
+
=> "0632737715836"
|
21
|
+
|
22
|
+
EAN13.new("0632737715836").to_upc
|
23
|
+
=> "632737715836"
|
24
|
+
|
25
|
+
EAN13.new("0632737715836").to_gtin
|
26
|
+
=> "00632737715836"
|
27
|
+
|
28
|
+
= Further Reader
|
29
|
+
|
30
|
+
- http://en.wikipedia.org/wiki/European_Article_Number
|
31
|
+
|
32
|
+
= Contributing
|
33
|
+
|
34
|
+
Source code is publicly available @ http://github.com/yob/ean13/tree/master.
|
35
|
+
Patches welcome, preferably via a git repo I can pull from.
|
data/lib/ean13.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
class EAN13
|
2
|
+
|
3
|
+
class Version #:nodoc:
|
4
|
+
Major = 1
|
5
|
+
Minor = 0
|
6
|
+
Tiny = 0
|
7
|
+
|
8
|
+
String = [Major, Minor, Tiny].join('.')
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(str)
|
12
|
+
@number = str.to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
EAN13.valid? @number
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.valid?(ean)
|
20
|
+
ean = ean.to_s
|
21
|
+
ean.length == 13 && ean == EAN13.complete(ean[0,12])
|
22
|
+
end
|
23
|
+
|
24
|
+
# Purely for generating new ean numbers
|
25
|
+
def self.complete(twelve)
|
26
|
+
twelve = twelve.to_s
|
27
|
+
return nil unless twelve.length == 12 && twelve.match(/\d{11}/)
|
28
|
+
|
29
|
+
arr = (0..11).to_a.collect do |i|
|
30
|
+
if (i+1).even?
|
31
|
+
twelve[i,1].to_i * 3
|
32
|
+
else
|
33
|
+
twelve[i,1].to_i
|
34
|
+
end
|
35
|
+
end
|
36
|
+
sum = arr.inject { |sum, n| sum + n }
|
37
|
+
remainder = sum % 10
|
38
|
+
if remainder == 0
|
39
|
+
check = 0
|
40
|
+
else
|
41
|
+
check = 10 - remainder
|
42
|
+
end
|
43
|
+
|
44
|
+
twelve + check.to_s
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_gtin
|
48
|
+
return nil unless self.valid?
|
49
|
+
"0#{@number}"
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_upc
|
53
|
+
return nil unless self.valid?
|
54
|
+
return nil unless @number[0,1] == "0"
|
55
|
+
@number[1,12]
|
56
|
+
end
|
57
|
+
end
|
data/spec/ean13_spec.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
$LOAD_PATH << File.dirname(__FILE__) + "/../lib"
|
2
|
+
|
3
|
+
require 'spec'
|
4
|
+
require 'ean13'
|
5
|
+
|
6
|
+
describe "The EAN13 class" do
|
7
|
+
it "should identify a valid EAN13" do
|
8
|
+
EAN13.new("9781843549161").valid?.should be_true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should identify a valid EAN13" do
|
12
|
+
EAN13.valid?("9781843549161").should be_true
|
13
|
+
EAN13.valid?(9781843549161).should be_true
|
14
|
+
EAN13.valid?("9790702228727").should be_true
|
15
|
+
EAN13.valid?("9790702228727").should be_true
|
16
|
+
EAN13.valid?("0602498351321").should be_true
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should identify an invalid EAN13" do
|
20
|
+
EAN13.valid?(nil).should be_false
|
21
|
+
EAN13.valid?("978184354916").should be_false
|
22
|
+
EAN13.valid?(Array).should be_false
|
23
|
+
EAN13.valid?(Array.new).should be_false
|
24
|
+
EAN13.valid?("9781843549162").should be_false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should calculate a EAN13 check digit correctly" do
|
28
|
+
EAN13.complete("978184354916").should eql("9781843549161")
|
29
|
+
EAN13.complete(978184354916).should eql("9781843549161")
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should convert to a UPC correctly" do
|
33
|
+
EAN13.new("9781843549161").to_upc.should be_nil
|
34
|
+
EAN13.new("0632737715836").to_upc.should eql("632737715836")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should convert to a GTIN-14 correctly" do
|
38
|
+
EAN13.new("9781843549161").to_gtin.should eql("09781843549161")
|
39
|
+
EAN13.new("0632737715836").to_gtin.should eql("00632737715836")
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ean13
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- James Healy
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-10-22 00:00:00 +11:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: a (very) small library for working with EAN-13 codes
|
17
|
+
email: jimmy@deefa.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/ean13.rb
|
26
|
+
- MIT-LICENSE
|
27
|
+
- README.rdoc
|
28
|
+
- CHANGELOG
|
29
|
+
has_rdoc: true
|
30
|
+
homepage: http://github.com/yob/ean13/tree/master
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options:
|
33
|
+
- --title
|
34
|
+
- EAN-13
|
35
|
+
- --line-numbers
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project: yob-projects
|
53
|
+
rubygems_version: 1.2.0
|
54
|
+
signing_key:
|
55
|
+
specification_version: 2
|
56
|
+
summary: a (very) small library for working with EAN-13 codes
|
57
|
+
test_files:
|
58
|
+
- spec/ean13_spec.rb
|