Class: CueCat
- Inherits:
-
Object
- Object
- CueCat
- Defined in:
- lib/cuecat.rb
Overview
An extra simple class for decoding a cuecat code into its components.
code = CueCat.new(".C3nZC3nZC3n2CNjXCNz0DxnY.cGen.ENr7CNT3Chz3ENj1CG.")
puts code.code_type
puts code.id
puts code.value
This code is based on the PHP version I found here:
http://www.phpbuilder.com/snippet/download.php?type=snippet&id=699
Thanks Adam!
Defined Under Namespace
Classes: Version
Instance Attribute Summary collapse
-
#code_type ⇒ Object
readonly
Returns the value of attribute code_type.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Class Method Summary collapse
- .ean?(code) ⇒ Boolean
- .upc?(code) ⇒ Boolean
-
.valid?(code) ⇒ Boolean
A very basic check to see if the supplied code looks like a cuecat code.
Instance Method Summary collapse
- #ean? ⇒ Boolean
-
#initialize(str) ⇒ CueCat
constructor
process a new cuecat code.
- #upc? ⇒ Boolean
-
#valid? ⇒ Boolean
is the supplied code valid?.
Constructor Details
#initialize(str) ⇒ CueCat
process a new cuecat code
57 58 59 60 61 62 63 64 |
# File 'lib/cuecat.rb', line 57 def initialize(str) @number = str.to_s if valid? swap split_components end end |
Instance Attribute Details
#code_type ⇒ Object (readonly)
Returns the value of attribute code_type.
19 20 21 |
# File 'lib/cuecat.rb', line 19 def code_type @code_type end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
19 20 21 |
# File 'lib/cuecat.rb', line 19 def id @id end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
19 20 21 |
# File 'lib/cuecat.rb', line 19 def value @value end |
Class Method Details
.ean?(code) ⇒ Boolean
46 47 48 |
# File 'lib/cuecat.rb', line 46 def ean?(code) self.new(code).ean? end |
.upc?(code) ⇒ Boolean
50 51 52 |
# File 'lib/cuecat.rb', line 50 def upc?(code) self.new(code).upc? end |
.valid?(code) ⇒ Boolean
A very basic check to see if the supplied code looks like a cuecat code. At this stage I’m not aware of any fullproof way to check.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/cuecat.rb', line 33 def valid?(code) code = code.to_s if code[0,1] != "." false elsif code[-1,1] != "." false elsif code.scan(/\./).size != 4 false else true end end |
Instance Method Details
#ean? ⇒ Boolean
71 72 73 74 |
# File 'lib/cuecat.rb', line 71 def ean? return false if @value.nil? EAN13.valid?(@value) end |
#upc? ⇒ Boolean
76 77 78 79 |
# File 'lib/cuecat.rb', line 76 def upc? return false if @value.nil? UPC.valid?(@value) end |
#valid? ⇒ Boolean
is the supplied code valid?
67 68 69 |
# File 'lib/cuecat.rb', line 67 def valid? CueCat.valid? @number end |