Class Barby::EAN13
In: lib/barby/barcode/ean_13.rb
Parent: Barcode1D
EAN13 Bookland EAN8 Barcode1D Code128 Code25 Code93 Code39 Barcode Barcode2D QrCode Pdf417 Code128A Code128B Code128C GS1128 Outputter CairoOutputter PngOutputter ASCIIOutputter PDFWriterOutputter RmagickOutputter PrawnOutputter SvgOutputter Code25IATA Code25Interleaved lib/barby/barcode/gs1_128.rb lib/barby/outputter/png_outputter.rb lib/barby/outputter/svg_outputter.rb lib/barby/outputter/rmagick_outputter.rb lib/barby/barcode.rb lib/barby/outputter/ascii_outputter.rb lib/barby/outputter.rb lib/barby/barcode/code_128.rb lib/barby/barcode/code_39.rb lib/barby/outputter/pdfwriter_outputter.rb lib/barby/barcode/code_93.rb lib/barby/outputter/prawn_outputter.rb lib/barby/barcode/pdf_417.rb lib/barby/barcode/ean_8.rb lib/barby/barcode/code_25_iata.rb lib/barby/outputter/cairo_outputter.rb lib/barby/barcode/ean_13.rb lib/barby/barcode/code_25.rb lib/barby/barcode/code_25_interleaved.rb lib/barby/barcode/qr_code.rb lib/barby/barcode/bookland.rb VERSION Barby dot/m_22_0.png

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

Methods

Constants

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 = { 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]   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)
START = '101'   These are the lines that "stick down" in the graphical representation
CENTER = '01010'
STOP = '101'
FORMAT = /^\d{12}$/   EAN-13 barcodes have 12 digits + check digit

Attributes

data  [RW] 

Public Class methods

[Source]

    # File lib/barby/barcode/ean_13.rb, line 59
59:     def initialize(data)
60:       self.data = data
61:       raise ArgumentError, 'data not valid' unless valid?
62:     end

Public Instance methods

[Source]

     # File lib/barby/barcode/ean_13.rb, line 167
167:     def center_encoding
168:       CENTER
169:     end

[Source]

    # File lib/barby/barcode/ean_13.rb, line 65
65:     def characters
66:       data.split(//)
67:     end

Mod10

[Source]

     # File lib/barby/barcode/ean_13.rb, line 136
136:     def checksum
137:       mod = weighted_sum % 10
138:       mod.zero? ? 0 : 10-mod
139:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 141
141:     def checksum_encoding
142:       RIGHT_ENCODINGS[checksum]
143:     end

[Source]

    # File lib/barby/barcode/ean_13.rb, line 95
95:     def data_with_checksum
96:       data + checksum.to_s
97:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 118
118:     def encoding
119:       start_encoding+left_encoding+center_encoding+right_encoding+stop_encoding
120:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 110
110:     def left_encoding
111:       left_encodings.join
112:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 100
100:     def left_encodings
101:       left_parity_map.zip(left_numbers).map do |parity,number|
102:         parity == :odd ? LEFT_ENCODINGS_ODD[number] : LEFT_ENCODINGS_EVEN[number]
103:       end
104:     end

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

[Source]

    # File lib/barby/barcode/ean_13.rb, line 80
80:     def left_numbers
81:       numbers[1,6]
82:     end

The parities to use for encoding left-hand numbers

[Source]

     # File lib/barby/barcode/ean_13.rb, line 124
124:     def left_parity_map
125:       LEFT_PARITY_MAPS[numbers.first]
126:     end

[Source]

    # File lib/barby/barcode/ean_13.rb, line 69
69:     def numbers
70:       characters.map{|s| s.to_i }
71:     end

[Source]

    # File lib/barby/barcode/ean_13.rb, line 90
90:     def numbers_with_checksum
91:       numbers + [checksum]
92:     end

[Source]

    # File lib/barby/barcode/ean_13.rb, line 73
73:     def odd_and_even_numbers
74:       alternater = false
75:       numbers.reverse.partition{ alternater = !alternater }
76:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 114
114:     def right_encoding
115:       right_encodings.join
116:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 106
106:     def right_encodings
107:       right_numbers.map{|n| RIGHT_ENCODINGS[n] }
108:     end

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

[Source]

    # File lib/barby/barcode/ean_13.rb, line 86
86:     def right_numbers
87:       numbers_with_checksum[7,6]
88:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 163
163:     def start_encoding
164:       START
165:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 171
171:     def stop_encoding
172:       STOP
173:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 151
151:     def to_s
152:       data_with_checksum
153:     end

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

[Source]

     # File lib/barby/barcode/ean_13.rb, line 158
158:     def upc?
159:       numbers.first.zero?
160:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 146
146:     def valid?
147:       data =~ FORMAT
148:     end

[Source]

     # File lib/barby/barcode/ean_13.rb, line 129
129:     def weighted_sum
130:       odds, evens = odd_and_even_numbers
131:       odds.map!{|n| n * 3 }
132:       sum = (odds+evens).inject(0){|s,n| s+n }
133:     end

[Validate]