upc 1.0.0 → 1.1.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 +7 -0
- data/CHANGELOG +3 -0
- data/README.markdown +33 -0
- data/lib/upc.rb +7 -11
- data/spec/upc_spec.rb +17 -18
- metadata +59 -37
- data/README.rdoc +0 -23
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 135f75ca08f67bb5b5921f3639a5f4100c959f3f
|
4
|
+
data.tar.gz: 51642467ce7afd5a201a733a93e148fd1f5ac3a6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4234e9ab8c67dd52e4252b22c905d947cfdf4068f14f323e51321f8eaeb82aca37e3e14e33872ae75ede85f4e19b0c911e02b2198995f5724fd79ca4779f50e4
|
7
|
+
data.tar.gz: 9330871134ac4a13c6fe28ba2ca70d9f911b1e3e0ebf00252bca02c9a2b925135fc54af1c34ed2f64f72f315c2b4bac88631ec86c623f829f4a82f0f7eb5032d
|
data/CHANGELOG
CHANGED
data/README.markdown
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
A small class for generating and validating Universal Product Codes (UPCs),
|
2
|
+
the 12 digit codes found on many older US products.
|
3
|
+
[](https://travis-ci.org/yob/upc)
|
4
|
+
|
5
|
+
# Installation
|
6
|
+
|
7
|
+
gem install upc
|
8
|
+
|
9
|
+
# Usage
|
10
|
+
|
11
|
+
UPC.new("632737715836").valid?
|
12
|
+
=> true
|
13
|
+
|
14
|
+
UPC.valid?("632737715836")
|
15
|
+
=> true
|
16
|
+
|
17
|
+
UPC.valid?("632737715837")
|
18
|
+
=> false
|
19
|
+
|
20
|
+
UPC.complete("63273771583")
|
21
|
+
=> "632737715836"
|
22
|
+
|
23
|
+
UPC.new("632737715836").to_ean
|
24
|
+
=> "0632737715836"
|
25
|
+
|
26
|
+
# Further Reader
|
27
|
+
|
28
|
+
* http://en.wikipedia.org/wiki/Universal_Product_Code
|
29
|
+
|
30
|
+
# Contributing
|
31
|
+
|
32
|
+
Source code is publicly available @ http://github.com/yob/upc. Patches
|
33
|
+
welcome, preferably via a git repo I can pull from.
|
data/lib/upc.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
class UPC
|
2
2
|
|
3
3
|
class Version #:nodoc:
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
MAJOR = 1
|
5
|
+
MINOR = 1
|
6
|
+
TINY = 0
|
7
7
|
|
8
|
-
String = [
|
8
|
+
String = [MAJOR, MINOR, TINY].join('.')
|
9
9
|
end
|
10
10
|
|
11
11
|
def initialize(str)
|
@@ -18,7 +18,7 @@ class UPC
|
|
18
18
|
|
19
19
|
def self.valid?(upc)
|
20
20
|
upc = upc.to_s
|
21
|
-
upc.length == 12 && upc == UPC.complete(upc[0,11])
|
21
|
+
upc.length == 12 && upc == UPC.complete(upc[0, 11])
|
22
22
|
end
|
23
23
|
|
24
24
|
# Purely for generating new UPC numbers
|
@@ -35,17 +35,13 @@ class UPC
|
|
35
35
|
end
|
36
36
|
sum = arr.inject { |sum, n| sum + n }
|
37
37
|
remainder = sum % 10
|
38
|
-
|
39
|
-
check = 0
|
40
|
-
else
|
41
|
-
check = 10 - remainder
|
42
|
-
end
|
38
|
+
(remainder == 0) ? check = 0 : check = 10 - remainder
|
43
39
|
|
44
40
|
eleven + check.to_s
|
45
41
|
end
|
46
42
|
|
47
43
|
def to_ean
|
48
|
-
return nil unless
|
44
|
+
return nil unless valid?
|
49
45
|
"0#{@number}"
|
50
46
|
end
|
51
47
|
end
|
data/spec/upc_spec.rb
CHANGED
@@ -1,36 +1,35 @@
|
|
1
1
|
$LOAD_PATH << File.dirname(__FILE__) + "/../lib"
|
2
2
|
|
3
|
-
require 'spec'
|
4
3
|
require 'upc'
|
5
4
|
|
6
|
-
describe
|
5
|
+
RSpec.describe UPC do
|
7
6
|
it "should identify a valid UPC" do
|
8
|
-
UPC.new("632737715836").valid
|
7
|
+
expect(UPC.new("632737715836").valid?).to be true
|
9
8
|
end
|
10
9
|
|
11
10
|
it "should identify a valid UPC" do
|
12
|
-
UPC.valid?("632737715836").
|
13
|
-
UPC.valid?(632737715836).
|
11
|
+
expect(UPC.valid?("632737715836")).to be true
|
12
|
+
expect(UPC.valid?(632737715836)).to be true
|
14
13
|
end
|
15
|
-
|
14
|
+
|
16
15
|
it "should identify an invalid UPC" do
|
17
|
-
UPC.valid?(nil).
|
18
|
-
UPC.valid?("902865").
|
19
|
-
UPC.valid?(Array).
|
20
|
-
UPC.valid?(Array.new).
|
21
|
-
UPC.valid?("632737715837").
|
22
|
-
UPC.valid?("63273771583").
|
16
|
+
expect(UPC.valid?(nil)).to be false
|
17
|
+
expect(UPC.valid?("902865")).to be false
|
18
|
+
expect(UPC.valid?(Array)).to be false
|
19
|
+
expect(UPC.valid?(Array.new)).to be false
|
20
|
+
expect(UPC.valid?("632737715837")).to be false
|
21
|
+
expect(UPC.valid?("63273771583")).to be false
|
23
22
|
end
|
24
23
|
|
25
24
|
it "should calculate a UPC check digit correctly" do
|
26
|
-
UPC.complete("63273771583").
|
27
|
-
UPC.complete(63273771583).
|
28
|
-
UPC.complete("63273720503").
|
29
|
-
UPC.complete("63273712223").
|
25
|
+
expect(UPC.complete("63273771583")).to eql("632737715836")
|
26
|
+
expect(UPC.complete(63273771583)).to eql("632737715836")
|
27
|
+
expect(UPC.complete("63273720503")).to eql("632737205030")
|
28
|
+
expect(UPC.complete("63273712223")).to eql("632737122238")
|
30
29
|
end
|
31
30
|
|
32
31
|
it "should convert to an EAN correctly" do
|
33
|
-
UPC.new("632737715836").to_ean.
|
34
|
-
UPC.new(632737715836).to_ean.
|
32
|
+
expect(UPC.new("632737715836").to_ean).to eql("0632737715836")
|
33
|
+
expect(UPC.new(632737715836).to_ean).to eql("0632737715836")
|
35
34
|
end
|
36
35
|
end
|
metadata
CHANGED
@@ -1,58 +1,80 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: upc
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
|
-
authors:
|
6
|
+
authors:
|
7
7
|
- James Healy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
11
|
+
date: 2017-07-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rake
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rspec
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
16
41
|
description: a (very) small library for working with Universal Product Codes
|
17
42
|
email: jimmy@deefa.com
|
18
43
|
executables: []
|
19
|
-
|
20
44
|
extensions: []
|
21
|
-
|
22
45
|
extra_rdoc_files: []
|
23
|
-
|
24
|
-
files:
|
25
|
-
- lib/upc.rb
|
26
|
-
- MIT-LICENSE
|
27
|
-
- README.rdoc
|
46
|
+
files:
|
28
47
|
- CHANGELOG
|
29
|
-
|
30
|
-
|
48
|
+
- MIT-LICENSE
|
49
|
+
- README.markdown
|
50
|
+
- lib/upc.rb
|
51
|
+
- spec/upc_spec.rb
|
52
|
+
homepage: http://github.com/yob/upc
|
53
|
+
licenses:
|
54
|
+
- MIT
|
55
|
+
metadata: {}
|
31
56
|
post_install_message:
|
32
|
-
rdoc_options:
|
33
|
-
- --title
|
57
|
+
rdoc_options:
|
58
|
+
- "--title"
|
34
59
|
- UPC
|
35
|
-
- --line-numbers
|
36
|
-
require_paths:
|
60
|
+
- "--line-numbers"
|
61
|
+
require_paths:
|
37
62
|
- lib
|
38
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
-
requirements:
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
40
65
|
- - ">="
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
version:
|
43
|
-
|
44
|
-
|
45
|
-
requirements:
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: 1.9.3
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
46
70
|
- - ">="
|
47
|
-
- !ruby/object:Gem::Version
|
48
|
-
version:
|
49
|
-
version:
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
50
73
|
requirements: []
|
51
|
-
|
52
|
-
|
53
|
-
rubygems_version: 1.2.0
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.6.11
|
54
76
|
signing_key:
|
55
|
-
specification_version:
|
77
|
+
specification_version: 4
|
56
78
|
summary: a (very) small library for working with Universal Product Codes
|
57
|
-
test_files:
|
79
|
+
test_files:
|
58
80
|
- spec/upc_spec.rb
|
data/README.rdoc
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
A small class for generating and validating Universal Product Codes (UPCs),
|
2
|
-
the 12 digit codes found on many older US products.
|
3
|
-
|
4
|
-
= Usage
|
5
|
-
|
6
|
-
UPC.new("632737715836").valid?
|
7
|
-
=> true
|
8
|
-
|
9
|
-
UPC.valid?("632737715836")
|
10
|
-
=> true
|
11
|
-
|
12
|
-
UPC.valid?("632737715837")
|
13
|
-
=> false
|
14
|
-
|
15
|
-
UPC.complete("63273771583")
|
16
|
-
=> "632737715836"
|
17
|
-
|
18
|
-
UPC.new("632737715836").to_ean
|
19
|
-
=> "0632737715836"
|
20
|
-
|
21
|
-
= Further Reader
|
22
|
-
|
23
|
-
- http://en.wikipedia.org/wiki/Universal_Product_Code
|