upc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/CHANGELOG +2 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +23 -0
  4. data/lib/upc.rb +51 -0
  5. data/spec/upc_spec.rb +36 -0
  6. metadata +58 -0
@@ -0,0 +1,2 @@
1
+ v1.0 (26th September 2008)
2
+ - Initial Release
@@ -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.
@@ -0,0 +1,23 @@
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
@@ -0,0 +1,51 @@
1
+ class UPC
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
+ UPC.valid? @number
17
+ end
18
+
19
+ def self.valid?(upc)
20
+ upc = upc.to_s
21
+ upc.length == 12 && upc == UPC.complete(upc[0,11])
22
+ end
23
+
24
+ # Purely for generating new UPC numbers
25
+ def self.complete(eleven)
26
+ eleven = eleven.to_s
27
+ return nil unless eleven.length == 11 && eleven.match(/\d{11}/)
28
+
29
+ arr = (0..10).to_a.collect do |i|
30
+ if (i+1).odd?
31
+ eleven[i,1].to_i * 3
32
+ else
33
+ eleven[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
+ eleven + check.to_s
45
+ end
46
+
47
+ def to_ean
48
+ return nil unless self.valid?
49
+ "0#{@number}"
50
+ end
51
+ end
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
2
+
3
+ require 'spec'
4
+ require 'upc'
5
+
6
+ describe "The UPC class" do
7
+ it "should identify a valid UPC" do
8
+ UPC.new("632737715836").valid?.should be_true
9
+ end
10
+
11
+ it "should identify a valid UPC" do
12
+ UPC.valid?("632737715836").should be_true
13
+ UPC.valid?(632737715836).should be_true
14
+ end
15
+
16
+ it "should identify an invalid UPC" do
17
+ UPC.valid?(nil).should be_false
18
+ UPC.valid?("902865").should be_false
19
+ UPC.valid?(Array).should be_false
20
+ UPC.valid?(Array.new).should be_false
21
+ UPC.valid?("632737715837").should be_false
22
+ UPC.valid?("63273771583").should be_false
23
+ end
24
+
25
+ it "should calculate a UPC check digit correctly" do
26
+ UPC.complete("63273771583").should eql("632737715836")
27
+ UPC.complete(63273771583).should eql("632737715836")
28
+ UPC.complete("63273720503").should eql("632737205030")
29
+ UPC.complete("63273712223").should eql("632737122238")
30
+ end
31
+
32
+ it "should convert to an EAN correctly" do
33
+ UPC.new("632737715836").to_ean.should eql("0632737715836")
34
+ UPC.new(632737715836).to_ean.should eql("0632737715836")
35
+ end
36
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: upc
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-09-26 00:00:00 +10:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: a (very) small library for working with Universal Product Codes
17
+ email: jimmy@deefa.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/upc.rb
26
+ - MIT-LICENSE
27
+ - README.rdoc
28
+ - CHANGELOG
29
+ has_rdoc: true
30
+ homepage: http://github.com/yob/upc/tree/master
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --title
34
+ - UPC
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 Universal Product Codes
57
+ test_files:
58
+ - spec/upc_spec.rb