Class: Item::UpcBarcode
- Inherits:
-
BaseService
- Object
- BaseService
- Item::UpcBarcode
- 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
- Go to localhost:631, manage printers, select the dymo
- Select "Set Default options"
- Select Media Size 30336, Continuous paper: false, output resolution 300x600, print quality barcode and graphics, media source auto
- 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
Instance Attribute Summary
Attributes inherited from BaseService
Class Method Summary collapse
-
.gtin_checksum(data_digits) ⇒ Object
Mod-10 GTIN check digit: weight 3,1 alternating from the rightmost data digit.
- .identify_type(gtin) ⇒ Object
Instance Method Summary collapse
-
#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.
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger
Constructor Details
This class inherits a constructor from BaseService
Class Method Details
.gtin_checksum(data_digits) ⇒ Object
Mod-10 GTIN check digit: weight 3,1 alternating from the rightmost data
digit. Replaces Barby::EAN13/UPCA#checksum (zint appends the check digit
when encoding but doesn't expose it as a query).
79 80 81 82 |
# File 'app/services/item/upc_barcode.rb', line 79 def self.gtin_checksum(data_digits) sum = data_digits.reverse.chars.each_with_index.sum { |d, i| d.to_i * (i.even? ? 3 : 1) } (10 - (sum % 10)) % 10 end |
.identify_type(gtin) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/services/item/upc_barcode.rb', line 64 def self.identify_type(gtin) return :invalid unless gtin.match?(/\A\d+\z/) # barby rejected non-numeric data; keep that if gtin.size == 13 && gtin_checksum(gtin.first(12)) == gtin.last.to_i :gtin13 elsif gtin.size == 12 && gtin_checksum(gtin.first(11)) == 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
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 55 56 57 58 59 60 61 62 |
# File 'app/services/item/upc_barcode.rb', line 18 def process(gtin, _identifier: nil, file_path: nil, output_format: nil, force_ean_format: false) require 'zint' output_format ||= :png identifier_type ||= :upc raise "Unsupported format: #{output_format}, only png supported at this time" unless output_format == :png # zint computes and appends the check digit from the 12/11 data digits, # exactly as barby did. if gtin.size == 13 identifier_formatted = gtin symbology = Zint::Constants::Symbologies::BARCODE_EANX data = gtin.first(12) identifier_type = :ean elsif gtin.size == 12 if force_ean_format identifier_formatted = "0#{gtin}" symbology = Zint::Constants::Symbologies::BARCODE_EANX data = identifier_formatted.first(12) identifier_type = :ean else identifier_formatted = gtin symbology = Zint::Constants::Symbologies::BARCODE_UPCA data = gtin.first(11) end else raise 'Invalid gtin format neither 12 nor 13 long' end = Zint::Barcode.new(value: data, symbology:) .scale = 2 .show_hrt = 0 # barby rendered bars only; keep it that way = .to_memory_file(extension: '.png') return Result.new(output: , identifier_type:, identifier_formatted:) unless file_path File.open(file_path, 'wb') do |f| f.write f.flush f.fsync end Result.new(output: file_path, identifier_type:, identifier_formatted:) end |