Class: Barby::EAN13

Inherits:
Barcode1D show all
Defined in:
lib/barby/barcode/ean_13.rb

Overview

EAN-13, aka UPC-A, barcodes are the ones you can see at your local supermarket, in your house and, well, everywhere..

To use this for a UPC barcode, just add a 0 to the front

Direct Known Subclasses

Bookland, EAN8

Constant Summary collapse

LEFT_ENCODINGS_ODD =
{
  0 => '0001101', 1 => '0011001', 2 => '0010011',
  3 => '0111101', 4 => '0100011', 5 => '0110001',
  6 => '0101111', 7 => '0111011', 8 => '0110111',
  9 => '0001011'
}
LEFT_ENCODINGS_EVEN =
{
  0 => '0100111', 1 => '0110011', 2 => '0011011',
  3 => '0100001', 4 => '0011101', 5 => '0111001',
  6 => '0000101', 7 => '0010001', 8 => '0001001',
  9 => '0010111'
}
RIGHT_ENCODINGS =
{
  0 => '1110010', 1 => '1100110', 2 => '1101100',
  3 => '1000010', 4 => '1011100', 5 => '1001110',
  6 => '1010000', 7 => '1000100', 8 => '1001000',
  9 => '1110100'
}
LEFT_PARITY_MAPS =

Describes whether the left-hand encoding should use LEFT_ENCODINGS_ODD or LEFT_ENCODINGS_EVEN, based on the first digit in the number system (and the barcode as a whole)

{
  0 => [:odd, :odd, :odd, :odd, :odd, :odd],   #UPC-A
  1 => [:odd, :odd, :even, :odd, :even, :even],
  2 => [:odd, :odd, :even, :even, :odd, :even],
  3 => [:odd, :odd, :even, :even, :even, :odd],
  4 => [:odd, :even, :odd, :odd, :even, :even],
  5 => [:odd, :even, :even, :odd, :odd, :even],
  6 => [:odd, :even, :even, :even, :odd, :odd],
  7 => [:odd, :even, :odd, :even, :odd, :even],
  8 => [:odd, :even, :odd, :even, :even, :odd],
  9 => [:odd, :even, :even, :odd, :even, :odd]
}
START =

These are the lines that “stick down” in the graphical representation

'101'
CENTER =
'01010'
STOP =
'101'
FORMAT =

EAN-13 barcodes have 12 digits + check digit

/^\d{12}$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Barcode

#method_missing, #outputter_class_for, #outputter_for, outputters, register_outputter, #two_dimensional?

Constructor Details

#initialize(data) ⇒ EAN13

Returns a new instance of EAN13.

Raises:

  • (ArgumentError)


59
60
61
62
# File 'lib/barby/barcode/ean_13.rb', line 59

def initialize(data)
  self.data = data
  raise ArgumentError, 'data not valid' unless valid?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Barby::Barcode

Instance Attribute Details

#dataObject

Returns the value of attribute data.



56
57
58
# File 'lib/barby/barcode/ean_13.rb', line 56

def data
  @data
end

Instance Method Details

#center_encodingObject



167
168
169
# File 'lib/barby/barcode/ean_13.rb', line 167

def center_encoding
  CENTER
end

#charactersObject



65
66
67
# File 'lib/barby/barcode/ean_13.rb', line 65

def characters
  data.split(//)
end

#checksumObject

Mod10



136
137
138
139
# File 'lib/barby/barcode/ean_13.rb', line 136

def checksum
  mod = weighted_sum % 10
  mod.zero? ? 0 : 10-mod
end

#checksum_encodingObject



141
142
143
# File 'lib/barby/barcode/ean_13.rb', line 141

def checksum_encoding
  RIGHT_ENCODINGS[checksum]
end

#data_with_checksumObject



95
96
97
# File 'lib/barby/barcode/ean_13.rb', line 95

def data_with_checksum
  data + checksum.to_s
end

#encodingObject



118
119
120
# File 'lib/barby/barcode/ean_13.rb', line 118

def encoding
  start_encoding+left_encoding+center_encoding+right_encoding+stop_encoding
end

#left_encodingObject



110
111
112
# File 'lib/barby/barcode/ean_13.rb', line 110

def left_encoding
  left_encodings.join
end

#left_encodingsObject



100
101
102
103
104
# File 'lib/barby/barcode/ean_13.rb', line 100

def left_encodings
  left_parity_map.zip(left_numbers).map do |parity,number|
    parity == :odd ? LEFT_ENCODINGS_ODD[number] : LEFT_ENCODINGS_EVEN[number]
  end
end

#left_numbersObject

Numbers that are encoded to the left of the center The first digit is not included



80
81
82
# File 'lib/barby/barcode/ean_13.rb', line 80

def left_numbers
  numbers[1,6]
end

#left_parity_mapObject

The parities to use for encoding left-hand numbers



124
125
126
# File 'lib/barby/barcode/ean_13.rb', line 124

def left_parity_map
  LEFT_PARITY_MAPS[numbers.first]
end

#numbersObject



69
70
71
# File 'lib/barby/barcode/ean_13.rb', line 69

def numbers
  characters.map{|s| s.to_i }
end

#numbers_with_checksumObject



90
91
92
# File 'lib/barby/barcode/ean_13.rb', line 90

def numbers_with_checksum
  numbers + [checksum]
end

#odd_and_even_numbersObject



73
74
75
76
# File 'lib/barby/barcode/ean_13.rb', line 73

def odd_and_even_numbers
  alternater = false
  numbers.reverse.partition{ alternater = !alternater }
end

#right_encodingObject



114
115
116
# File 'lib/barby/barcode/ean_13.rb', line 114

def right_encoding
  right_encodings.join
end

#right_encodingsObject



106
107
108
# File 'lib/barby/barcode/ean_13.rb', line 106

def right_encodings
  right_numbers.map{|n| RIGHT_ENCODINGS[n] }
end

#right_numbersObject

Numbers that are encoded to the right of the center The checksum is included here



86
87
88
# File 'lib/barby/barcode/ean_13.rb', line 86

def right_numbers
  numbers_with_checksum[7,6]
end

#start_encodingObject



163
164
165
# File 'lib/barby/barcode/ean_13.rb', line 163

def start_encoding
  START
end

#stop_encodingObject



171
172
173
# File 'lib/barby/barcode/ean_13.rb', line 171

def stop_encoding
  STOP
end

#to_sObject



151
152
153
# File 'lib/barby/barcode/ean_13.rb', line 151

def to_s
  data_with_checksum
end

#upc?Boolean

Is this a UPC-A barcode? UPC barcodes are EAN codes that start with 0

Returns:

  • (Boolean)


158
159
160
# File 'lib/barby/barcode/ean_13.rb', line 158

def upc?
  numbers.first.zero?
end

#valid?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/barby/barcode/ean_13.rb', line 146

def valid?
  data =~ FORMAT
end

#weighted_sumObject



129
130
131
132
133
# File 'lib/barby/barcode/ean_13.rb', line 129

def weighted_sum
  odds, evens = odd_and_even_numbers
  odds.map!{|n| n * 3 }
  sum = (odds+evens).inject(0){|s,n| s+n }
end