Class: EAN13
- Inherits:
-
Object
- Object
- EAN13
- Defined in:
- lib/ean13.rb
Defined Under Namespace
Classes: Version
Class Method Summary collapse
-
.complete(twelve) ⇒ Object
Purely for generating new ean numbers.
- .valid?(ean) ⇒ Boolean
Instance Method Summary collapse
- #bookland? ⇒ Boolean
-
#initialize(str) ⇒ EAN13
constructor
A new instance of EAN13.
-
#san? ⇒ Boolean
Returns true if this EAN has an embedded SAN.
- #to_gtin ⇒ Object
-
#to_san ⇒ Object
convert this EAN to a SAN.
- #to_upc ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(str) ⇒ EAN13
Returns a new instance of EAN13.
13 14 15 |
# File 'lib/ean13.rb', line 13 def initialize(str) @number = str.to_s end |
Class Method Details
.complete(twelve) ⇒ Object
Purely for generating new ean numbers
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ean13.rb', line 31 def self.complete(twelve) twelve = twelve.to_s return nil unless twelve.length == 12 && twelve.match(/\d{11}/) arr = (0..11).to_a.collect do |i| if (i+1).even? twelve[i,1].to_i * 3 else twelve[i,1].to_i end end sum = arr.inject { |sum, n| sum + n } remainder = sum % 10 if remainder == 0 check = 0 else check = 10 - remainder end twelve + check.to_s end |
Instance Method Details
#bookland? ⇒ Boolean
21 22 23 |
# File 'lib/ean13.rb', line 21 def bookland? valid? && (@number[0,3] == "978" || @number[0,3] == "979") end |
#san? ⇒ Boolean
Returns true if this EAN has an embedded SAN
For more info on SANs, see http://www.bowker.com/index.php/component/content/article/3
58 59 60 |
# File 'lib/ean13.rb', line 58 def san? $stderr.puts "EAN13.san? was removed in ean13 v2.0" end |
#to_gtin ⇒ Object
72 73 74 75 |
# File 'lib/ean13.rb', line 72 def to_gtin return nil unless self.valid? "0#{@number}" end |
#to_san ⇒ Object
convert this EAN to a SAN. returns nil if the EAN doesn't contain an embedded SAN.
requires the SAN library to be loaded or available. Will raise an error if it's not
68 69 70 |
# File 'lib/ean13.rb', line 68 def to_san $stderr.puts "EAN13.to_san was removed in ean13 v2.0" end |
#to_upc ⇒ Object
77 78 79 80 81 |
# File 'lib/ean13.rb', line 77 def to_upc return nil unless self.valid? return nil unless @number[0,1] == "0" @number[1,12] end |