labelzoom 1.0.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +312 -94
- data/lib/labelzoom/client.rb +195 -30
- data/lib/labelzoom/conversion_result.rb +4 -3
- data/lib/labelzoom/errors.rb +23 -3
- data/lib/labelzoom/formats.rb +43 -4
- data/lib/labelzoom/options.rb +122 -3
- data/lib/labelzoom/printing.rb +205 -0
- data/lib/labelzoom/version.rb +1 -1
- data/lib/labelzoom.rb +8 -2
- metadata +5 -3
|
@@ -0,0 +1,205 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module LabelZoom
|
|
7
|
+
# A print job the server accepted, returned by {Client#print}.
|
|
8
|
+
#
|
|
9
|
+
# @!attribute [r] job_id
|
|
10
|
+
# @return [String] the job's id. Pass it to +client.jobs.get+ to follow the job.
|
|
11
|
+
# @!attribute [r] status
|
|
12
|
+
# @return [String] +"dispatched"+ when an online agent took the job, +"queued"+ when none
|
|
13
|
+
# is online. Kept as a String: the set of statuses can grow, and a new one must not
|
|
14
|
+
# fail the call.
|
|
15
|
+
# @!attribute [r] copies
|
|
16
|
+
# @return [Integer, nil] the copies requested, or nil when the request set none.
|
|
17
|
+
# @!attribute [r] idempotent
|
|
18
|
+
# @return [Boolean] true when the request reused an Idempotency-Key: this is the
|
|
19
|
+
# original job, and nothing printed again.
|
|
20
|
+
PrintJob = Struct.new(:job_id, :status, :copies, :idempotent, keyword_init: true) do
|
|
21
|
+
def idempotent? = idempotent == true
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The jobs a template print created, one per record, returned by {Client#print_template}.
|
|
25
|
+
#
|
|
26
|
+
# @!attribute [r] jobs
|
|
27
|
+
# @return [Array<PrintJob>]
|
|
28
|
+
# @!attribute [r] remaining
|
|
29
|
+
# @return [Integer] records at the end of the batch that were not submitted. Send the
|
|
30
|
+
# same request with the same idempotency key to submit them; records already submitted
|
|
31
|
+
# replay without printing again.
|
|
32
|
+
# @!attribute [r] message
|
|
33
|
+
# @return [String, nil] the server's explanation when +remaining+ is not 0.
|
|
34
|
+
PrintBatch = Struct.new(:jobs, :remaining, :message, keyword_init: true)
|
|
35
|
+
|
|
36
|
+
# A print job's current state, returned by +client.jobs.get+. The wire is snake_case, so
|
|
37
|
+
# the members are named exactly as the API names them.
|
|
38
|
+
#
|
|
39
|
+
# @!attribute [r] status
|
|
40
|
+
# @return [String] +queued+, +dispatched+, +printing+, +completed+ or +failed+. Kept as a
|
|
41
|
+
# String, because the set of statuses can grow.
|
|
42
|
+
# @!attribute [r] error
|
|
43
|
+
# @return [String, nil] why the job failed.
|
|
44
|
+
# @!attribute [r] source_format
|
|
45
|
+
# @return [String, nil] the format the job was stored in: the printer's language after
|
|
46
|
+
# any conversion.
|
|
47
|
+
# The four +*_date+ members are +Time+ objects, each nil when unset.
|
|
48
|
+
PrintJobDetails = Struct.new(:id, :printer_id, :agent_id, :source_format, :copies, :status,
|
|
49
|
+
:error, :retry_count, :created_date, :dispatched_date,
|
|
50
|
+
:completed_date, :retried_date, keyword_init: true)
|
|
51
|
+
|
|
52
|
+
# One counted quota. +pending+ is set for agents only, and +reset_at+ (a +Time+) for jobs
|
|
53
|
+
# today only.
|
|
54
|
+
PrintQuotaCounter = Struct.new(:used, :limit, :remaining, :pending, :reset_at,
|
|
55
|
+
keyword_init: true)
|
|
56
|
+
|
|
57
|
+
# The size limits on a print request.
|
|
58
|
+
PrintLimits = Struct.new(:max_payload_bytes, :max_stored_payload_bytes, :max_batch_size,
|
|
59
|
+
keyword_init: true)
|
|
60
|
+
|
|
61
|
+
# The account's Cloud Print limits and today's usage, returned by {Client#print_quota}.
|
|
62
|
+
# +jobs_today+ resets at +jobs_today.reset_at+, 00:00 UTC.
|
|
63
|
+
PrintQuota = Struct.new(:plan, :agents, :printers, :jobs_today, :limits, keyword_init: true)
|
|
64
|
+
|
|
65
|
+
# Print jobs, from +client.jobs+. Shaped as its own object so that +client.printers+ and
|
|
66
|
+
# +client.templates+ can sit beside it later without changing anything here.
|
|
67
|
+
class PrintJobs
|
|
68
|
+
# @api private -- use +client.jobs+.
|
|
69
|
+
def initialize(fetch)
|
|
70
|
+
@fetch = fetch
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Gets a job's current state. Poll it to follow a job to +completed+ or +failed+.
|
|
74
|
+
#
|
|
75
|
+
# @param job_id [String] the +job_id+ of a {PrintJob}.
|
|
76
|
+
# @return [PrintJobDetails]
|
|
77
|
+
# @raise [ValidationError] if +job_id+ is empty, before any network call.
|
|
78
|
+
# @raise [NotFoundError] if the job does not exist.
|
|
79
|
+
def get(job_id) = @fetch.call(job_id)
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# The print calls' validation and response reading, written once for every print call.
|
|
83
|
+
#
|
|
84
|
+
# @api private
|
|
85
|
+
module Printing
|
|
86
|
+
MIN_COPIES = 1
|
|
87
|
+
MAX_COPIES = 9999
|
|
88
|
+
|
|
89
|
+
module_function
|
|
90
|
+
|
|
91
|
+
# @return [String] the id, unchanged.
|
|
92
|
+
# @raise [ValidationError] named +parameter+, when the id is missing or blank.
|
|
93
|
+
def require_id!(value, parameter)
|
|
94
|
+
return value if value.is_a?(String) && !value.strip.empty?
|
|
95
|
+
|
|
96
|
+
raise ValidationError.new(parameter, "#{parameter} cannot be empty.")
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @return [Integer, nil] the copies, or nil when the caller set none.
|
|
100
|
+
def copies!(copies)
|
|
101
|
+
return nil if copies.nil?
|
|
102
|
+
return copies if copies.is_a?(Integer) && copies.between?(MIN_COPIES, MAX_COPIES)
|
|
103
|
+
|
|
104
|
+
raise ValidationError.new("copies",
|
|
105
|
+
"copies must be a whole number from #{MIN_COPIES} to " \
|
|
106
|
+
"#{MAX_COPIES}, but was #{copies.inspect}.")
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The Idempotency-Key for one call. Fixed before the first attempt, so every retry of
|
|
110
|
+
# the call sends the same key (contract P4): without it, a retry after a lost response
|
|
111
|
+
# could print the label twice.
|
|
112
|
+
#
|
|
113
|
+
# @return [String] the caller's key exactly as given, else a random lowercase UUID v4.
|
|
114
|
+
def key_for_call(key)
|
|
115
|
+
return SecureRandom.uuid if key.nil?
|
|
116
|
+
return key if key.is_a?(String) && !key.strip.empty?
|
|
117
|
+
|
|
118
|
+
raise ValidationError.new("idempotencyKey",
|
|
119
|
+
"An idempotency key cannot be blank. Omit it to have the SDK " \
|
|
120
|
+
"generate one.")
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# The JSON body of a template print: one record as that object, several as an array,
|
|
124
|
+
# and none as an empty object (contract P3).
|
|
125
|
+
def template_body(records)
|
|
126
|
+
payload =
|
|
127
|
+
case records.length
|
|
128
|
+
when 0 then {}
|
|
129
|
+
when 1 then records.first
|
|
130
|
+
else records
|
|
131
|
+
end
|
|
132
|
+
JSON.generate(payload)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def read_job(json)
|
|
136
|
+
PrintJob.new(job_id: json_string(json, "jobId"), status: json_string(json, "status"),
|
|
137
|
+
copies: json_integer(json, "copies"), idempotent: json["idempotent"] == true)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# One record comes back as a bare job and several as +{"jobs": [...]}+. Callers get one
|
|
141
|
+
# shape either way.
|
|
142
|
+
def read_batch(json)
|
|
143
|
+
unless json["jobs"].is_a?(Array)
|
|
144
|
+
return PrintBatch.new(jobs: [read_job(json)], remaining: 0, message: nil)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
jobs = json["jobs"].map { |job| read_job(job.is_a?(Hash) ? job : {}) }
|
|
148
|
+
PrintBatch.new(jobs: jobs, remaining: json_integer(json, "remaining") || 0,
|
|
149
|
+
message: json_string(json, "message"))
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def read_job_details(json)
|
|
153
|
+
PrintJobDetails.new(
|
|
154
|
+
id: json_string(json, "id"),
|
|
155
|
+
printer_id: json_string(json, "printer_id"),
|
|
156
|
+
agent_id: json_string(json, "agent_id"),
|
|
157
|
+
source_format: json_string(json, "source_format"),
|
|
158
|
+
copies: json_integer(json, "copies"),
|
|
159
|
+
status: json_string(json, "status"),
|
|
160
|
+
error: json_string(json, "error"),
|
|
161
|
+
retry_count: json_integer(json, "retry_count") || 0,
|
|
162
|
+
created_date: json_instant(json, "created_date"),
|
|
163
|
+
dispatched_date: json_instant(json, "dispatched_date"),
|
|
164
|
+
completed_date: json_instant(json, "completed_date"),
|
|
165
|
+
retried_date: json_instant(json, "retried_date")
|
|
166
|
+
)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
def read_quota(json)
|
|
170
|
+
limits = json_object(json, "limits")
|
|
171
|
+
PrintQuota.new(
|
|
172
|
+
plan: json_string(json, "plan"),
|
|
173
|
+
agents: read_counter(json_object(json, "agents")),
|
|
174
|
+
printers: read_counter(json_object(json, "printers")),
|
|
175
|
+
jobs_today: read_counter(json_object(json, "jobsToday")),
|
|
176
|
+
limits: PrintLimits.new(
|
|
177
|
+
max_payload_bytes: json_integer(limits, "maxPayloadBytes"),
|
|
178
|
+
max_stored_payload_bytes: json_integer(limits, "maxStoredPayloadBytes"),
|
|
179
|
+
max_batch_size: json_integer(limits, "maxBatchSize")
|
|
180
|
+
)
|
|
181
|
+
)
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def read_counter(json)
|
|
185
|
+
PrintQuotaCounter.new(used: json_integer(json, "used"), limit: json_integer(json, "limit"),
|
|
186
|
+
remaining: json_integer(json, "remaining"),
|
|
187
|
+
pending: json_integer(json, "pending"),
|
|
188
|
+
reset_at: json_instant(json, "resetAt"))
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def json_string(json, name) = json[name].is_a?(String) ? json[name] : nil
|
|
192
|
+
|
|
193
|
+
def json_integer(json, name) = json[name].is_a?(Integer) ? json[name] : nil
|
|
194
|
+
|
|
195
|
+
def json_object(json, name) = json[name].is_a?(Hash) ? json[name] : {}
|
|
196
|
+
|
|
197
|
+
def json_instant(json, name)
|
|
198
|
+
text = json_string(json, name)
|
|
199
|
+
text.nil? ? nil : Time.iso8601(text)
|
|
200
|
+
rescue ArgumentError
|
|
201
|
+
# An unparseable timestamp is not worth failing an otherwise good response.
|
|
202
|
+
nil
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
end
|
data/lib/labelzoom/version.rb
CHANGED
data/lib/labelzoom.rb
CHANGED
|
@@ -5,11 +5,12 @@ require_relative "labelzoom/errors"
|
|
|
5
5
|
require_relative "labelzoom/formats"
|
|
6
6
|
require_relative "labelzoom/options"
|
|
7
7
|
require_relative "labelzoom/conversion_result"
|
|
8
|
+
require_relative "labelzoom/printing"
|
|
8
9
|
require_relative "labelzoom/client"
|
|
9
10
|
|
|
10
11
|
# Official Ruby client for the LabelZoom API.
|
|
11
12
|
#
|
|
12
|
-
# LabelZoom converts barcode labels between printer languages (ZPL, EPL, TSPL, DPL),
|
|
13
|
+
# LabelZoom converts barcode labels between printer languages (ZPL, EPL, IPL, TSPL, DPL, SBPL),
|
|
13
14
|
# LabelZoom's own XML/JSON model, PDF, and raster images. Almost everything the API does
|
|
14
15
|
# happens at one endpoint:
|
|
15
16
|
#
|
|
@@ -20,7 +21,12 @@ require_relative "labelzoom/client"
|
|
|
20
21
|
# label: { width: 4, height: 6 })
|
|
21
22
|
# result.save("label.png")
|
|
22
23
|
#
|
|
23
|
-
#
|
|
24
|
+
# Cloud Print sends a document, or a published template, to a printer:
|
|
25
|
+
#
|
|
26
|
+
# job = client.print(:zpl, printer_id, zpl, copies: 2)
|
|
27
|
+
# client.jobs.get(job.job_id).status
|
|
28
|
+
#
|
|
29
|
+
# The behavior of every LabelZoom SDK is specified in docs/API_CONTRACT.md and checked by
|
|
24
30
|
# the shared fixtures in conformance/, which this gem's spec suite executes.
|
|
25
31
|
module LabelZoom
|
|
26
32
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: labelzoom
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 2.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- RJF Technology Solutions LLC
|
|
@@ -9,8 +9,9 @@ bindir: bin
|
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
11
|
dependencies: []
|
|
12
|
-
description: Converts barcode labels between ZPL, EPL, TSPL, DPL,
|
|
13
|
-
and
|
|
12
|
+
description: Converts barcode labels between ZPL, EPL, IPL, TSPL, DPL, SBPL, CPCL,
|
|
13
|
+
PDF, images, and LabelZoom XML/JSON; reads ESC/Label, Fingerprint, EZPL, TPCL, NiceLabel,
|
|
14
|
+
BarTender, and DYMO files; and prints them with Cloud Print.
|
|
14
15
|
email:
|
|
15
16
|
- support@labelzoom.com
|
|
16
17
|
executables: []
|
|
@@ -25,6 +26,7 @@ files:
|
|
|
25
26
|
- lib/labelzoom/errors.rb
|
|
26
27
|
- lib/labelzoom/formats.rb
|
|
27
28
|
- lib/labelzoom/options.rb
|
|
29
|
+
- lib/labelzoom/printing.rb
|
|
28
30
|
- lib/labelzoom/version.rb
|
|
29
31
|
homepage: https://www.labelzoom.com
|
|
30
32
|
licenses:
|