Class: Item::UpcBarcode

Inherits:
BaseService show all
Defined in:
app/services/item/upc_barcode.rb

Overview

This will generate a UPC or EAN barcode, right now it is fine tuned to print on
30336 compatible Dymo labels (2.125" x 1") - This is paper size 'w72h154' in PrintNode
To make this work you will need a Dymo labelwriter 400 series with the right label and the PrintNode server running

  1. Go to localhost:631, manage printers, select the dymo
  2. Select "Set Default options"
  3. Select Media Size 30336, Continuous paper: false, output resolution 300x600, print quality barcode and graphics, media source auto
  4. In print node options, next to the printer, page width set to 1.0, page height set to 2.13

Defined Under Namespace

Classes: Result

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Class Method Details

.identify_type(gtin) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/services/item/upc_barcode.rb', line 56

def self.identify_type(gtin)
  require 'barby'
  require 'barby/barcode/ean_13'

  if gtin.size == 13 && (barcode = begin
    Barby::EAN13.new(gtin.first(12))
  rescue StandardError
    nil
  end) && barcode.checksum == gtin.last.to_i
    :gtin13
  elsif gtin.size == 12 && (barcode = begin
    Barby::UPCA.new(gtin.first(11))
  rescue StandardError
    nil
  end) && barcode.checksum == gtin.last.to_i
    :gtin12
  else
    :invalid
  end
end

Instance Method Details

#process(gtin, identifier: nil, file_path: nil, output_format: nil, force_ean_format: false) ⇒ Object

force_ean_format will force an ean barcode output (13 digits) 1,6,6 even for UPC (basically prepending a zero)
http://www.officialeancode.com/spanish/ean-guide/gtin-13-barcode.html



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/services/item/upc_barcode.rb', line 17

def process(gtin, identifier: nil, file_path: nil, output_format: nil, force_ean_format: false)
  require 'barby'
  require 'barby/barcode/ean_13'
  require 'barby/outputter/png_outputter'

  output_format ||= :png
  identifier_type ||= :upc

  raise "Unsupported format: #{output_format}, only png supported at this time" unless output_format == :png

  if gtin.size == 13
    identifier_formatted = gtin13 = gtin
    barcode = Barby::EAN13.new(gtin13.first(12))
    identifier_type = :ean
  elsif gtin.size == 12
    if force_ean_format
      identifier_formatted = gtin13 = "0#{gtin}"
      barcode = Barby::EAN13.new(gtin13.first(12))
      identifier_type = :ean
    else
      identifier_formatted = gtin12 = gtin
      barcode = Barby::UPCA.new(gtin12.first(11))
    end
  else
    raise 'Invalid gtin format neither 12 nor 13 long'
  end

  barcode_output = barcode.to_png(xdim: 2)

  return Result.new(output: barcode_output, identifier_type:, identifier_formatted:) unless file_path

  File.open(file_path, 'wb') do |f|
    f.write barcode_output
    f.flush
    f.fsync
  end
  Result.new(output: file_path, identifier_type:, identifier_formatted:)
end