cuecat 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG ADDED
@@ -0,0 +1,2 @@
1
+ v1.0 (1st November 2008)
2
+ - Initial Release
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,34 @@
1
+ A small class for decoding the values returned when scanning a barcode with
2
+ the cheaply available cuecat scanner.
3
+
4
+ = Installation
5
+
6
+ gem install cuecat
7
+
8
+ = Usage
9
+
10
+ puts CueCat.valid?(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.")
11
+ => true
12
+
13
+ puts CueCat.ean?(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.")
14
+ => true
15
+
16
+ code = CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.")
17
+ puts code.code_type
18
+
19
+ => "IBN"
20
+ puts code.id
21
+
22
+ => "000000005112157601"
23
+ puts code.value
24
+ => "978184354916"
25
+
26
+ = Further Reader
27
+
28
+ - http://en.wikipedia.org/wiki/Cuecat
29
+ - http://www.librarything.com/wiki/index.php/CueCat
30
+
31
+ = Contributing
32
+
33
+ Source code is publicly available @ http://github.com/yob/cuecat/tree/master.
34
+ Patches welcome, preferably via a git repo I can pull from.
data/lib/cuecat.rb ADDED
@@ -0,0 +1,112 @@
1
+ require 'base64'
2
+ require 'ean13'
3
+
4
+ # An extra simple class for decoding a cuecat code into
5
+ # its components.
6
+ #
7
+ # code = CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.")
8
+ # puts code.code_type
9
+ # puts code.id
10
+ # puts code.value
11
+ #
12
+ # This code is based on the PHP version I found here:
13
+ # http://www.phpbuilder.com/snippet/download.php?type=snippet&id=699
14
+ # Thanks Adam!
15
+ #
16
+ class CueCat
17
+
18
+ attr_reader :code_type, :id, :value
19
+
20
+ class Version #:nodoc:
21
+ Major = 1
22
+ Minor = 0
23
+ Tiny = 0
24
+
25
+ String = [Major, Minor, Tiny].join('.')
26
+ end
27
+
28
+ class << self
29
+ # A very basic check to see if the supplied code looks like a cuecat code.
30
+ # At this stage I'm not aware of any fullproof way to check.
31
+ #
32
+ def valid?(code)
33
+ code = code.to_s
34
+ if code[0,1] != "."
35
+ false
36
+ elsif code[-1,1] != "."
37
+ false
38
+ elsif code.scan(/\./).size != 4
39
+ false
40
+ else
41
+ true
42
+ end
43
+ end
44
+
45
+ def ean?(code)
46
+ self.new(code).ean?
47
+ end
48
+ end
49
+
50
+ # process a new cuecat code
51
+ #
52
+ def initialize(str)
53
+ @number = str.to_s
54
+
55
+ if valid?
56
+ swap
57
+ split_components
58
+ end
59
+ end
60
+
61
+ # is the supplied code valid?
62
+ def valid?
63
+ CueCat.valid? @number
64
+ end
65
+
66
+ def ean?
67
+ return false if @value.nil?
68
+ EAN13.valid?(@value)
69
+ end
70
+
71
+ private
72
+
73
+ # Swap letters with their upper or lower case equivilants.
74
+ #
75
+ # Go go gadget hardcore encryption.
76
+ #
77
+ def swap
78
+ arr = @number.unpack("C*").collect do |c|
79
+ if c > 64 && c < 91
80
+ c + 32
81
+ elsif c > 96 && c < 123
82
+ c - 32
83
+ else
84
+ c
85
+ end
86
+ end
87
+ @number.replace arr.pack("C*")
88
+ end
89
+
90
+ # Split the code by the full stops, and base64 decode each part.
91
+ #
92
+ # If the code is an EAN, calculate its check digit
93
+ #
94
+ def split_components
95
+ @number.gsub!("-","/")
96
+ something, id, code_type, value = *@number.split(".")
97
+ @id = bitwise_op(Base64.decode64(id))
98
+ @code_type = bitwise_op(Base64.decode64(code_type))
99
+ @value = bitwise_op(Base64.decode64(value))
100
+
101
+ if @code_type == "IBN" && @value.size == 12
102
+ @value = EAN13.complete(@value)
103
+ end
104
+ end
105
+
106
+ # bitwise XOR each value with 67. The final step to enlightenment.
107
+ #
108
+ def bitwise_op(value)
109
+ arr = value.unpack("C*").collect { |c| c ^ 67 }
110
+ arr.pack("C*")
111
+ end
112
+ end
@@ -0,0 +1,47 @@
1
+ $LOAD_PATH << File.dirname(__FILE__) + "/../lib"
2
+
3
+ require 'spec'
4
+ require 'cuecat'
5
+
6
+ describe "The CueCat class" do
7
+
8
+ it "should correctly translate a cuecat code to its component parts" do
9
+ code = CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.")
10
+ code.code_type.should eql("IBN")
11
+ code.id.should eql("000000005112157601")
12
+ code.value.should eql("9781843549161")
13
+
14
+ code = CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7C3T1E3DZDxPWCW.")
15
+ code.code_type.should eql("IBN")
16
+ code.id.should eql("000000005112157601")
17
+ code.value.should eql("9780868406930")
18
+
19
+ code = CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGf2.ENr7C3j3C3f7Dxr7DxzYDNnZ.")
20
+ code.code_type.should eql("IB5")
21
+ code.id.should eql("000000005112157601")
22
+ code.value.should eql("978014028678651500")
23
+ end
24
+
25
+ it "should correctly detect valid codes" do
26
+ CueCat.valid?(".C3nZC3nZC3n2CNjXCNz0DxnY.cGf2.ENr7C3j3C3f7Dxr7DxzYDNnZ.").should be_true
27
+ CueCat.valid?(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7C3T1E3DZDxPWCW.").should be_true
28
+ CueCat.valid?(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.").should be_true
29
+
30
+ CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.").valid?.should be_true
31
+ end
32
+
33
+ it "should correctly detect invalid codes" do
34
+ CueCat.valid?(nil).should be_false
35
+ CueCat.valid?(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG").should be_false
36
+ CueCat.valid?(1).should be_false
37
+ CueCat.valid?([0,1,2]).should be_false
38
+ end
39
+
40
+ it "should correctly detect EAN13 codes" do
41
+ CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.").ean?.should be_true
42
+ CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGf2.ENr7C3j3C3f7Dxr7DxzYDNnZ.").ean?.should be_false
43
+
44
+ CueCat.ean?(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.").should be_true
45
+ end
46
+
47
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cuecat
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-11-03 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: ean13
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "1.0"
24
+ version:
25
+ description: a (very) small library for working with cuecat codes
26
+ email: jimmy@deefa.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - lib/cuecat.rb
35
+ - MIT-LICENSE
36
+ - README.rdoc
37
+ - CHANGELOG
38
+ has_rdoc: true
39
+ homepage: http://github.com/yob/cuecat/tree/master
40
+ post_install_message:
41
+ rdoc_options:
42
+ - --title
43
+ - CueCat
44
+ - --line-numbers
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project: yob-projects
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: a (very) small library for working with cuecat codes
66
+ test_files:
67
+ - spec/cuecat_spec.rb