Class: LabelZoom::ConversionResult
- Inherits:
-
Object
- Object
- LabelZoom::ConversionResult
- 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
-
#bytes ⇒ String
readonly
The response body as binary (ASCII-8BIT), exactly as the server sent it.
-
#content_type ⇒ String?
readonly
The response Content-Type header.
-
#request_id ⇒ String?
readonly
The X-LZ-Request-Id response header.
-
#status ⇒ Integer
readonly
The HTTP status code, always 2xx here.
Instance Method Summary collapse
-
#initialize(bytes:, status:, content_type: nil, request_id: nil) ⇒ ConversionResult
constructor
A new instance of ConversionResult.
-
#save(path) ⇒ Object
Writes #bytes to
path, in binary mode. -
#text ⇒ String
#bytes decoded with the response charset, defaulting to UTF-8.
- #to_s ⇒ Object
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
#bytes ⇒ String (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.
13 14 15 |
# File 'lib/labelzoom/conversion_result.rb', line 13 def bytes @bytes end |
#content_type ⇒ String? (readonly)
Returns the response Content-Type header.
16 17 18 |
# File 'lib/labelzoom/conversion_result.rb', line 16 def content_type @content_type end |
#request_id ⇒ String? (readonly)
Returns 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 |
#status ⇒ Integer (readonly)
Returns 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 |
#text ⇒ String
#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.
40 41 42 |
# File 'lib/labelzoom/conversion_result.rb', line 40 def text @text ||= decode end |
#to_s ⇒ Object
50 |
# File 'lib/labelzoom/conversion_result.rb', line 50 def to_s = text |