ean8 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/ean8.rb +58 -0
- data/spec/ean8_spec.rb +36 -0
- metadata +67 -0
data/CHANGELOG
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 James Healy
|
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-8's, the 8 digit codes found
|
2
|
+
on many products sold around the world.
|
3
|
+
|
4
|
+
= Installation
|
5
|
+
|
6
|
+
gem install ean8
|
7
|
+
|
8
|
+
= Usage
|
9
|
+
|
10
|
+
EAN8.new("93469647").valid?
|
11
|
+
=> true
|
12
|
+
|
13
|
+
EAN8.valid?("93469647")
|
14
|
+
=> true
|
15
|
+
|
16
|
+
EAN8.valid?("93469646")
|
17
|
+
=> false
|
18
|
+
|
19
|
+
EAN8.complete("9346964")
|
20
|
+
=> "93469647"
|
21
|
+
|
22
|
+
EAN8.new("93469647").to_ean
|
23
|
+
=> "0000093469647"
|
24
|
+
|
25
|
+
EAN8.new("93469647").to_gtin
|
26
|
+
=> "00000093469647"
|
27
|
+
|
28
|
+
= Further Reader
|
29
|
+
|
30
|
+
- http://en.wikipedia.org/wiki/EAN_8
|
31
|
+
|
32
|
+
= Contributing
|
33
|
+
|
34
|
+
Source code is publicly available @ http://github.com/yob/ean8
|
35
|
+
Patches welcome, preferably via a git repo I can pull from.
|
data/lib/ean8.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
class EAN8
|
3
|
+
|
4
|
+
class Version #:nodoc:
|
5
|
+
Major = 1
|
6
|
+
Minor = 0
|
7
|
+
Tiny = 0
|
8
|
+
|
9
|
+
String = [Major, Minor, Tiny].join('.')
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(str)
|
13
|
+
@number = str.to_s
|
14
|
+
end
|
15
|
+
|
16
|
+
def valid?
|
17
|
+
EAN8.valid? @number
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.valid?(ean)
|
21
|
+
ean = ean.to_s
|
22
|
+
ean.length == 8 && ean == EAN8.complete(ean[0,7])
|
23
|
+
end
|
24
|
+
|
25
|
+
# Purely for generating new ean numbers
|
26
|
+
def self.complete(seven)
|
27
|
+
seven = seven.to_s
|
28
|
+
return nil unless seven.length == 7 && seven.match(/\d{7}/)
|
29
|
+
|
30
|
+
arr = (0..6).to_a.collect do |i|
|
31
|
+
if (i+1) % 2 == 0
|
32
|
+
seven[i,1].to_i
|
33
|
+
else
|
34
|
+
seven[i,1].to_i * 3
|
35
|
+
end
|
36
|
+
end
|
37
|
+
sum = arr.inject { |sum, n| sum + n }
|
38
|
+
remainder = sum % 10
|
39
|
+
if remainder == 0
|
40
|
+
check = 0
|
41
|
+
else
|
42
|
+
check = 10 - remainder
|
43
|
+
end
|
44
|
+
|
45
|
+
seven + check.to_s
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_ean
|
49
|
+
return nil unless self.valid?
|
50
|
+
"00000#{@number}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_gtin
|
54
|
+
return nil unless self.valid?
|
55
|
+
"000000#{@number}"
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
data/spec/ean8_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
$LOAD_PATH << File.dirname(__FILE__) + "/../lib"
|
2
|
+
|
3
|
+
require 'spec'
|
4
|
+
require 'ean8'
|
5
|
+
|
6
|
+
describe EAN8 do
|
7
|
+
it "should identify a valid EAN8" do
|
8
|
+
EAN8.new("93469647").valid?.should be_true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should identify a valid EAN8" do
|
12
|
+
EAN8.valid?("93469647").should be_true
|
13
|
+
EAN8.valid?(93469647).should be_true
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should identify an invalid EAN8" do
|
17
|
+
EAN8.valid?(nil).should be_false
|
18
|
+
EAN8.valid?("978184354916").should be_false
|
19
|
+
EAN8.valid?(Array).should be_false
|
20
|
+
EAN8.valid?(Array.new).should be_false
|
21
|
+
EAN8.valid?("93469648").should be_false
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should calculate a EAN8 check digit correctly" do
|
25
|
+
EAN8.complete("9346964").should eql("93469647")
|
26
|
+
EAN8.complete(9346964).should eql("93469647")
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should convert to an EAN-13 correctly" do
|
30
|
+
EAN8.new("93469647").to_ean.should eql("0000093469647")
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should convert to a GTIN-14 correctly" do
|
34
|
+
EAN8.new("93469647").to_gtin.should eql("00000093469647")
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ean8
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 1
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
version: 1.0.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- James Healy
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-07 00:00:00 +10:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: a (very) small library for working with EAN-8 codes
|
22
|
+
email: jimmy@deefa.com
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/ean8.rb
|
31
|
+
- MIT-LICENSE
|
32
|
+
- README.rdoc
|
33
|
+
- CHANGELOG
|
34
|
+
has_rdoc: true
|
35
|
+
homepage: http://github.com/yob/ean8
|
36
|
+
licenses: []
|
37
|
+
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options:
|
40
|
+
- --title
|
41
|
+
- EAN-8
|
42
|
+
- --line-numbers
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
segments:
|
50
|
+
- 0
|
51
|
+
version: "0"
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.6
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: a (very) small library for working with EAN-8 codes
|
66
|
+
test_files:
|
67
|
+
- spec/ean8_spec.rb
|