Class: LabelZoom::ConversionResult

Inherits:
Object
  • Object
show all
Defined in:
lib/labelzoom/conversion_result.rb

Overview

The outcome of a successful conversion.

#bytes is authoritative. PDF, PNG, BMP, GIF and JPEG targets are binary, so treating the response as text would silently corrupt five of the eleven targets -- and EPL and TSPL reach the same hazard through a text/plain response, because their GW and BITMAP commands inline a raw 1-bpp payload.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes:, status:, content_type: nil, request_id: nil) ⇒ ConversionResult

Returns a new instance of ConversionResult.



24
25
26
27
28
29
# File 'lib/labelzoom/conversion_result.rb', line 24

def initialize(bytes:, status:, content_type: nil, request_id: nil)
  @bytes = bytes
  @status = status
  @content_type = content_type
  @request_id = request_id
end

Instance Attribute Details

#bytesString (readonly)

Returns the response body as binary (ASCII-8BIT), exactly as the server sent it. Note this is a String of bytes, not String#bytes' Array of Integers.

Returns:

  • (String)

    the response body as binary (ASCII-8BIT), exactly as the server sent it. Note this is a String of bytes, not String#bytes' Array of Integers.



13
14
15
# File 'lib/labelzoom/conversion_result.rb', line 13

def bytes
  @bytes
end

#content_typeString? (readonly)

Returns the response Content-Type header.

Returns:

  • (String, nil)

    the response Content-Type header.



16
17
18
# File 'lib/labelzoom/conversion_result.rb', line 16

def content_type
  @content_type
end

#request_idString? (readonly)

Returns the X-LZ-Request-Id response header. The support handle.

Returns:

  • (String, nil)

    the X-LZ-Request-Id response header. The support handle.



22
23
24
# File 'lib/labelzoom/conversion_result.rb', line 22

def request_id
  @request_id
end

#statusInteger (readonly)

Returns the HTTP status code, always 2xx here.

Returns:

  • (Integer)

    the HTTP status code, always 2xx here.



19
20
21
# File 'lib/labelzoom/conversion_result.rb', line 19

def status
  @status
end

Instance Method Details

#save(path) ⇒ Object

Writes #bytes to path, in binary mode.



45
46
47
48
# File 'lib/labelzoom/conversion_result.rb', line 45

def save(path)
  File.binwrite(path, bytes)
  path
end

#textString

#bytes decoded with the response charset, defaulting to UTF-8.

Safe for the textual targets. For a binary target, or an EPL/TSPL label that might carry graphics, read #bytes instead.

Memoized rather than computed in the constructor: a 5 MB PNG's bytes should never be run through a decoder just because someone built a result object.

Returns:

  • (String)


40
41
42
# File 'lib/labelzoom/conversion_result.rb', line 40

def text
  @text ||= decode
end

#to_sObject



50
# File 'lib/labelzoom/conversion_result.rb', line 50

def to_s = text