Class: ZplRenderer::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/zpl_renderer/decoder.rb

Overview

Independent barcode/QR decoder used by tests and optional QA hooks.

Defined Under Namespace

Classes: Hit

Instance Method Summary collapse

Constructor Details

#initialize(binary_path: nil) ⇒ Decoder

Returns a new instance of Decoder.



11
12
13
# File 'lib/zpl_renderer/decoder.rb', line 11

def initialize(binary_path: nil)
  @binary_path = binary_path
end

Instance Method Details

#decode_png(png_bytes) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/zpl_renderer/decoder.rb', line 15

def decode_png(png_bytes)
  stdout, stderr, status = Open3.capture3(
    binary_path,
    "--decode",
    stdin_data: png_bytes,
    binmode: true
  )

  unless status.success?
    message = stderr.to_s.strip
    message = "decode failed with status #{status.exitstatus}" if message.empty?
    raise RenderError, message
  end

  JSON.parse(stdout).map do |row|
    Hit.new(symbology: row.fetch("format"), text: row.fetch("text"))
  end
rescue Errno::ENOENT
  raise BinaryMissingError, Binary.missing_message
end