Module: LabelZoom::Formats

Defined in:
lib/labelzoom/formats.rb

Overview

The format metadata table, defined once.

Deliberately the only place a format's media type appears. The superseded .NET design had a builder class per format, each independently knowing this mapping, and they drifted -- one of them emitted the source type as the target.

Constant Summary collapse

SOURCE_FORMATS =

Every accepted source, in the contract's order. :jpg is an input spelling that normalizes to :jpeg on the wire; :url tells the server to go fetch a document rather than naming a format.

%i[zpl epl tspl dpl xml json pdf png bmp gif jpeg jpg url].freeze
TARGET_FORMATS =

Every accepted target. There is no :url -- it is a fetch instruction, not an output format -- and no :jpg, which is a source-side spelling only.

The printer languages round-trip: :epl, :tspl and :dpl became targets in contract 1.1.0, when the printer-language writers shipped.

%i[zpl epl tspl dpl xml json pdf png bmp gif jpeg].freeze
MEDIA_TYPES =
{
  zpl: "text/plain",
  epl: "text/plain",
  tspl: "text/plain",
  dpl: "text/plain",
  xml: "application/xml",
  json: "application/json",
  pdf: "application/pdf",
  png: "image/png",
  bmp: "image/bmp",
  gif: "image/gif",
  jpeg: "image/jpeg",
  jpg: "image/jpeg",
  # The body is the URL itself.
  url: "text/plain"
}.freeze
COLOR_MODES =

Colour reduction. Server default GRAYSCALE.

%w[BW GRAYSCALE COLOR].freeze
PDF_CONVERSION_MODES =

How a PDF source is interpreted. Server default IMAGE.

%w[IMAGE NATIVE].freeze
ZPL_IMAGE_COMPRESSIONS =

Encoding of images embedded in ZPL output. Server default Z64.

%w[Z64 COMPRESSED_HEX].freeze

Class Method Summary collapse

Class Method Details

.media_type(format) ⇒ Object

The request Content-Type for a source.



81
# File 'lib/labelzoom/formats.rb', line 81

def media_type(format) = MEDIA_TYPES.fetch(format)

.source!(format) ⇒ Symbol

Returns the validated source format.

Returns:

  • (Symbol)

    the validated source format.

Raises:



52
53
54
55
56
57
58
59
# File 'lib/labelzoom/formats.rb', line 52

def source!(format)
  symbol = format.to_s.downcase.to_sym
  return symbol if SOURCE_FORMATS.include?(symbol)

  raise ValidationError.new("source",
                            "#{format.inspect} is not a source format the LabelZoom API " \
                            "accepts. Expected one of: #{SOURCE_FORMATS.join(", ")}.")
end

.source_token(format) ⇒ Object

The path segment for a source. :jpg normalizes to "jpeg" (rule A2).



75
# File 'lib/labelzoom/formats.rb', line 75

def source_token(format) = format == :jpg ? "jpeg" : format.to_s

.target!(format) ⇒ Symbol

Returns the validated target format.

Returns:

  • (Symbol)

    the validated target format.

Raises:

  • (ValidationError)

    if it is not a target the API produces. This is where Ruby enforces what the statically typed SDKs enforce at compile time -- passing :url or :jpg as a target is caught here.



65
66
67
68
69
70
71
72
# File 'lib/labelzoom/formats.rb', line 65

def target!(format)
  symbol = format.to_s.downcase.to_sym
  return symbol if TARGET_FORMATS.include?(symbol)

  raise ValidationError.new("target",
                            "#{format.inspect} is not a target format the LabelZoom API " \
                            "produces. Expected one of: #{TARGET_FORMATS.join(", ")}.")
end

.target_token(format) ⇒ Object

The path segment for a target. Targets need no normalization.



78
# File 'lib/labelzoom/formats.rb', line 78

def target_token(format) = format.to_s