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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 80455151c5d513f6c2a8ffce89112c4597da3fd4b9729a5580c6f10b802c450f
|
|
4
|
+
data.tar.gz: 9ec51b32abda1ab62ebeee4f0bd1d1b4df9966bd3f1d2c57679f9bd0cec49e4c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4776c99818681d01403d4d7cfccb65fab618a05aaa36acbfa7ab68e1b57708728da31a3b6f9063e5a72680bee119d8d224e95951bd450beadd7b93d286c7a756
|
|
7
|
+
data.tar.gz: b2b6a9ce3be1ad21ea0a023b2f94713b047e53e78c2c50c9b2afd5d2d0c41045f36d430615ac126cabb758af66e854552525017e006f7f2ac8acec8c9a007997
|
data/README.md
CHANGED
|
@@ -2,145 +2,342 @@
|
|
|
2
2
|
|
|
3
3
|
# LabelZoom Ruby SDK
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
The `labelzoom` gem is the official Ruby client for the [LabelZoom API](https://api.labelzoom.com).
|
|
6
|
+
Use it to convert barcode labels between printer languages, PDF, images, and label design files,
|
|
7
|
+
and to print them on any printer connected through the LabelZoom Print Agent.
|
|
8
|
+
|
|
9
|
+
## What the SDK handles
|
|
10
|
+
|
|
11
|
+
- Retries `429` responses, `5xx` responses, and network failures, and waits as long as a
|
|
12
|
+
`Retry-After` header asks.
|
|
13
|
+
- Raises a typed error for every non-2xx response, with the status, message, error `code`, and
|
|
14
|
+
request ID.
|
|
15
|
+
- Returns every result as binary-safe bytes, so PDF, image, and printer-language output arrives
|
|
16
|
+
intact.
|
|
17
|
+
- Sends an `Idempotency-Key` with every print, so a retry never prints a label twice.
|
|
18
|
+
- Works without an API key, on the watermarked free tier. See
|
|
19
|
+
[Plans and limits](https://docs.labelzoom.com/reference/rate-limits-and-plans/).
|
|
20
|
+
- Needs no other gems: it uses `net/http`, `json`, `uri`, and `openssl` from the standard library.
|
|
10
21
|
|
|
11
22
|
## Install
|
|
12
23
|
|
|
24
|
+
The gem requires Ruby 3.1 or later.
|
|
25
|
+
|
|
13
26
|
```sh
|
|
14
27
|
gem install labelzoom
|
|
15
28
|
```
|
|
16
29
|
|
|
17
|
-
|
|
18
|
-
gem "labelzoom", "~> 1.0"
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
<details>
|
|
22
|
-
<summary>Build from source</summary>
|
|
30
|
+
If you use Bundler, add the gem to your `Gemfile` instead:
|
|
23
31
|
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
cd labelzoom-sdk/ruby
|
|
27
|
-
bundle install && bundle exec rake
|
|
32
|
+
```ruby
|
|
33
|
+
gem "labelzoom", "~> 2.0"
|
|
28
34
|
```
|
|
29
|
-
</details>
|
|
30
35
|
|
|
31
|
-
##
|
|
36
|
+
## Convert a label
|
|
32
37
|
|
|
33
|
-
|
|
34
|
-
|
|
38
|
+
A conversion is a pipeline: choose the source format, set how the source is read, choose the target
|
|
39
|
+
format, set what is written, and then run it. In Ruby, one `convert` call takes the source format,
|
|
40
|
+
the target format, and the document, and each option is a keyword argument.
|
|
35
41
|
|
|
36
42
|
```ruby
|
|
37
43
|
require "labelzoom"
|
|
38
44
|
|
|
39
|
-
client = LabelZoom::Client.new
|
|
45
|
+
client = LabelZoom::Client.new # reads LABELZOOM_API_KEY; works without one
|
|
40
46
|
|
|
41
|
-
result = client.convert(
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
result = client.convert(
|
|
48
|
+
:pdf, :zpl, File.binread("shipping-label.pdf"),
|
|
49
|
+
pdf: { page_number: 0 }, # source: read the first page (0-based)
|
|
50
|
+
label: { width: 4, height: 6 }, # source: the label size, in inches
|
|
51
|
+
target_dpi: 203 # target: write ZPL for a 203 dpi printer
|
|
52
|
+
)
|
|
44
53
|
|
|
45
|
-
result.save("label.
|
|
54
|
+
result.save("label.zpl")
|
|
46
55
|
```
|
|
47
56
|
|
|
48
|
-
|
|
57
|
+
Source options set how the document is read, such as its label size, its page, its resolution, and
|
|
58
|
+
the records that fill its fields. Target options set what is written, such as the output
|
|
59
|
+
resolution, the rotation, and ZPL image compression. Both kinds are keyword arguments on the same
|
|
60
|
+
call; [Options by stage](#options-by-stage) lists which is which and the formats each one acts on.
|
|
61
|
+
|
|
62
|
+
The SDK sends only the options you pass and fills in no defaults of its own, so the server's
|
|
63
|
+
defaults apply to the rest. `watermark: false` is sent, because you set it.
|
|
64
|
+
|
|
65
|
+
### Set the API key
|
|
66
|
+
|
|
67
|
+
The client reads your API key from the `LABELZOOM_API_KEY` environment variable. To pass a key, use
|
|
68
|
+
`LabelZoom::Client.new(api_key: "lz_live_...")`. To force the free tier, pass `api_key: nil` or an
|
|
69
|
+
empty string; the client then ignores the environment variable. `client.authenticated?` returns
|
|
70
|
+
whether the client has a key.
|
|
71
|
+
|
|
72
|
+
Without a key, the API serves the free tier: watermarked output, the first label only, a 1 MB
|
|
73
|
+
request limit, and no multi-page, JSON-target, or image-to-image conversion.
|
|
74
|
+
|
|
75
|
+
### Read the result
|
|
76
|
+
|
|
77
|
+
`convert` returns a `LabelZoom::ConversionResult`:
|
|
78
|
+
|
|
79
|
+
| Method | Returns |
|
|
80
|
+
| --- | --- |
|
|
81
|
+
| `bytes` | The response body as a binary (`ASCII-8BIT`) String. This is a String of bytes, not an Array of Integers. |
|
|
82
|
+
| `text` | The body decoded with the response's charset, or UTF-8 when it names none. |
|
|
83
|
+
| `save(path)` | Writes `bytes` to `path` in binary mode, and returns `path`. |
|
|
84
|
+
| `content_type` | The response `Content-Type`. |
|
|
85
|
+
| `status` | The HTTP status, always 2xx. |
|
|
86
|
+
| `request_id` | The `X-LZ-Request-Id` response header. Give it to LabelZoom support. |
|
|
87
|
+
|
|
88
|
+
Write PDF and image output with `save` or `File.binwrite`, never with `File.write`. Printer-language
|
|
89
|
+
output is `text/plain`, but EPL's `GW`, TSPL's `BITMAP`, IPL graphics, and SBPL graphics embed raw
|
|
90
|
+
binary, so read `bytes` rather than `text` whenever a label might carry graphics. CPCL writes
|
|
91
|
+
graphics as hex, so its output is plain text.
|
|
92
|
+
|
|
93
|
+
## Print a label
|
|
94
|
+
|
|
95
|
+
A printer is a target. `print` takes the same arguments as `convert`, with the printer ID where the
|
|
96
|
+
target format goes. The printer's own configuration sets its language, and LabelZoom converts the
|
|
97
|
+
document to it, so a PDF prints on a ZPL printer. Printing needs an API key with the `print` scope.
|
|
98
|
+
|
|
99
|
+
### Print a document
|
|
49
100
|
|
|
50
101
|
```ruby
|
|
51
102
|
client = LabelZoom::Client.new(api_key: "lz_live_...")
|
|
103
|
+
|
|
104
|
+
job = client.print(
|
|
105
|
+
:pdf, printer_id, File.binread("label.pdf"),
|
|
106
|
+
pdf: { page_number: 0 }, # any source option
|
|
107
|
+
rotation: 90, # the one target option a printer takes
|
|
108
|
+
copies: 2,
|
|
109
|
+
idempotency_key: order_number
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
job.job_id # also job.status, job.copies, and job.idempotent?
|
|
52
113
|
```
|
|
53
114
|
|
|
54
|
-
|
|
55
|
-
|
|
115
|
+
`print` returns a `LabelZoom::PrintJob`. Its `status` is `"dispatched"` when an online agent took
|
|
116
|
+
the job, and `"queued"` when no agent is online.
|
|
56
117
|
|
|
57
|
-
|
|
118
|
+
- Every source format prints, including NiceLabel, BarTender, and DYMO files, except a URL and
|
|
119
|
+
base64 text. Fetch or decode the document and print its bytes. `:url` and `base64_text: true`
|
|
120
|
+
raise `LabelZoom::ValidationError`.
|
|
121
|
+
- `print` takes every source option, and `rotation:` is the only target option it takes. The
|
|
122
|
+
other target options, such as `target_dpi:`, depend on the output language, which the printer
|
|
123
|
+
sets, so they raise `LabelZoom::ValidationError`. Of the `zpl:` keys, `print` takes only
|
|
124
|
+
`commands_to_ignore:`.
|
|
125
|
+
- `copies:` sends the whole job that many times, from 1 to 9999, and multiplies any quantity in the
|
|
126
|
+
document: `^PQ3` with 5 copies prints 15 labels.
|
|
127
|
+
- Every print carries an `Idempotency-Key`. To make a repeated call return the original job
|
|
128
|
+
instead of printing again, pass your own `idempotency_key:`, such as an order number. Without
|
|
129
|
+
one, the SDK generates a key for each call and reuses it on that call's retries, so a retry
|
|
130
|
+
can't print twice. `job.idempotent?` is true when the server returned the original job.
|
|
58
131
|
|
|
59
|
-
|
|
60
|
-
`:jpg` `:url`
|
|
132
|
+
### Print a template
|
|
61
133
|
|
|
62
|
-
|
|
134
|
+
A template published from LabelZoom Studio or the web designer is a source of its own. It takes
|
|
135
|
+
merge data instead of source options, one record per label.
|
|
63
136
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
137
|
+
```ruby
|
|
138
|
+
batch = client.print_template(
|
|
139
|
+
template_id, printer_id,
|
|
140
|
+
data: [{ orderNumber: "SO-1001", sku: "A-100" }],
|
|
141
|
+
validate: true, # check every record for missing fields before printing
|
|
142
|
+
copies: 1,
|
|
143
|
+
idempotency_key: "SO-1001"
|
|
144
|
+
)
|
|
68
145
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
146
|
+
batch.jobs # one LabelZoom::PrintJob per record
|
|
147
|
+
batch.remaining # records not yet submitted
|
|
148
|
+
```
|
|
72
149
|
|
|
73
|
-
|
|
150
|
+
`print_template` returns a `LabelZoom::PrintBatch` for any number of records. A single Hash in
|
|
151
|
+
`data:` is one label; omit `data:` for a template with no merge fields. If `remaining` isn't 0, the
|
|
152
|
+
batch ran out of time, and `message` says why: send the same request with the same key to submit
|
|
153
|
+
the rest. The records already submitted don't print again.
|
|
74
154
|
|
|
75
|
-
|
|
76
|
-
never fills in a client-side default, so a change to a server default reaches you without a gem
|
|
77
|
-
upgrade.
|
|
155
|
+
### Follow a job
|
|
78
156
|
|
|
79
157
|
```ruby
|
|
80
|
-
client.
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
color_mode: "GRAYSCALE",
|
|
85
|
-
darkness: 60, # 0-100 luminance threshold
|
|
86
|
-
position: { x: 5, y: 15 },
|
|
87
|
-
watermark: false, # an explicit false IS sent
|
|
88
|
-
label: { width: 4.0, height: 6.0 },
|
|
89
|
-
pdf: { conversion_mode: "IMAGE", page_number: 0 },
|
|
90
|
-
zpl: { commands_to_ignore: ["^PQ"], image_compression: "Z64" },
|
|
91
|
-
data: [{ sku: "1234" }])
|
|
158
|
+
details = client.jobs.get(job.job_id)
|
|
159
|
+
|
|
160
|
+
details.status # queued, dispatched, printing, completed, or failed
|
|
161
|
+
details.error # why the job failed, or nil
|
|
92
162
|
```
|
|
93
163
|
|
|
94
|
-
|
|
164
|
+
`jobs.get` returns a `LabelZoom::PrintJobDetails` with `id`, `printer_id`, `agent_id`,
|
|
165
|
+
`source_format`, `copies`, `status`, `error`, `retry_count`, and the `created_date`,
|
|
166
|
+
`dispatched_date`, `completed_date`, and `retried_date` timestamps as `Time` objects. Each value is
|
|
167
|
+
`nil` when it isn't set. To follow a job to the end, call `jobs.get` until `status` is `completed`
|
|
168
|
+
or `failed`. Treat `status` as an open set of strings.
|
|
169
|
+
|
|
170
|
+
### Check the quota
|
|
95
171
|
|
|
96
|
-
|
|
97
|
-
|
|
172
|
+
```ruby
|
|
173
|
+
quota = client.print_quota
|
|
98
174
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
175
|
+
quota.jobs_today.remaining # prints left today
|
|
176
|
+
quota.jobs_today.reset_at # 00:00 UTC, as a Time
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
`print_quota` returns a `LabelZoom::PrintQuota` with `plan`; `agents`, `printers`, and `jobs_today`,
|
|
180
|
+
each with `used`, `limit`, and `remaining`; and `limits`, with `max_payload_bytes`,
|
|
181
|
+
`max_stored_payload_bytes`, and `max_batch_size`. `agents` also has `pending`.
|
|
182
|
+
|
|
183
|
+
When the daily job limit is used up, a print raises `LabelZoom::PrintQuotaExceededError`. The SDK
|
|
184
|
+
never retries it, because the limit resets at 00:00 UTC. Check `print_quota` to stop before you
|
|
185
|
+
reach it.
|
|
186
|
+
|
|
187
|
+
## Formats
|
|
102
188
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
189
|
+
| Role | Formats |
|
|
190
|
+
| --- | --- |
|
|
191
|
+
| Sources | `:zpl` `:epl` `:ipl` `:tspl` `:dpl` `:sbpl` `:esclabel` `:fingerprint` `:ezpl` `:cpcl` `:tpcl` `:nlbl` `:lbl` `:btw` `:dymo` `:xml` `:json` `:pdf` `:png` `:bmp` `:gif` `:jpeg` `:jpg` `:url` |
|
|
192
|
+
| Targets | `:zpl` `:epl` `:ipl` `:tspl` `:dpl` `:sbpl` `:cpcl` `:xml` `:json` `:pdf` `:png` `:bmp` `:gif` `:jpeg` |
|
|
193
|
+
| Print sources | Every source except `:url` |
|
|
194
|
+
|
|
195
|
+
The printer languages ESC/Label, Fingerprint, EZPL, and TPCL, and the design-file formats NiceLabel
|
|
196
|
+
(`:nlbl`, `:lbl`), BarTender (`:btw`), and DYMO (`:dymo`), are source-only. Send NiceLabel and
|
|
197
|
+
BarTender files as bytes, read with `File.binread`.
|
|
198
|
+
|
|
199
|
+
`:jpg` is an input spelling that becomes `jpeg` on the wire, and `:url` makes the server fetch the
|
|
200
|
+
document whose URL is the body, so neither is a target. A format that isn't a valid source or
|
|
201
|
+
target raises `LabelZoom::ValidationError` before any request is sent.
|
|
202
|
+
|
|
203
|
+
The printer languages round-trip: `pdf` to `epl` and `zpl` to `tspl` are real conversions.
|
|
204
|
+
|
|
205
|
+
## Options by stage
|
|
206
|
+
|
|
207
|
+
Each option is a keyword argument to `convert`. Grouped options are nested hashes, such as
|
|
208
|
+
`label: { width: 4, height: 6 }`. A misspelled keyword, such as `taget_dpi:`, or a misspelled
|
|
209
|
+
key inside a nested hash raises `LabelZoom::ValidationError` before anything is sent. To send a
|
|
210
|
+
parameter the SDK doesn't model, use `extra:`.
|
|
211
|
+
|
|
212
|
+
### Source options
|
|
213
|
+
|
|
214
|
+
| Keyword | Source formats | What it does |
|
|
215
|
+
| --- | --- | --- |
|
|
216
|
+
| `label: { width:, height: }` | All | The label size, in inches, not dots. It replaces the size the label code declares. Omit it to use the declared size, or the size of the content. |
|
|
217
|
+
| `label_index:` | All | Converts only this label, 0-based, on every plan. For image or PDF to ZPL, it counts the labels after `split:`, page by page. |
|
|
218
|
+
| `data:` | All | Merge-field values, one Hash per output label. A single Hash is one label. |
|
|
219
|
+
| `source_dpi:` | Printer languages and images | The resolution the source was made for. A PDF, design-file, XML, or JSON source returns `400`. |
|
|
220
|
+
| `pdf: { page_number: }` | PDF | Converts one page, 0-based. Omit it to convert every page. |
|
|
221
|
+
| `pdf: { conversion_mode: }` | PDF | `"IMAGE"` (the default) rasterizes each page; `"NATIVE"` extracts the text and vector content. |
|
|
222
|
+
| `zpl: { commands_to_ignore: }` | ZPL and ESC/Label | ZPL commands to skip, such as `["^PQ"]`. |
|
|
223
|
+
| `split: { rows:, columns:, width:, height:, left:, top:, column_gap:, row_gap:, skip_blank: }` | Image or PDF to ZPL | Cuts each page into a grid of labels. Lengths are in inches. Omit `width:` and `height:` to divide the page equally. |
|
|
224
|
+
| `scaling:` | Image and PDF | The size the picture is rendered at, in percent. The server default is 100. |
|
|
225
|
+
| `position: { x:, y: }` | Image and PDF | The picture's offset on the label, in output dots from the top-left corner. A negative value crops it. |
|
|
226
|
+
| `color_mode:` | Image and PDF | `"BW"`, `"GRAYSCALE"` (the default), or `"COLOR"`. |
|
|
227
|
+
| `darkness:` | Image and PDF | The black-and-white threshold, 0 to 100. |
|
|
228
|
+
|
|
229
|
+
### Target options
|
|
230
|
+
|
|
231
|
+
| Keyword | Target formats | What it does |
|
|
232
|
+
| --- | --- | --- |
|
|
233
|
+
| `target_dpi:` | Every target except PDF | The output resolution. It defaults to the source's resolution, else 203. A PDF target returns `400`. |
|
|
234
|
+
| `rotation:` | All, and printers | Turns the finished label. It must be a multiple of 90. |
|
|
235
|
+
| `watermark:` | All | `true` adds a watermark. |
|
|
236
|
+
| `dialect:` | ZPL | The output dialect, such as `"moca"`. Paid plans only. |
|
|
237
|
+
| `zpl: { image_compression: }` | ZPL | `"Z64"` (the default) or `"COMPRESSED_HEX"`. |
|
|
238
|
+
| `zpl: { collapse_copies: true }` | Image or PDF to ZPL | Writes each run of identical labels as one label with a `^PQ` count. |
|
|
239
|
+
| `layout: { across:, gap:, fill: }` | Image or PDF to ZPL | Places labels side by side in each printed row. `gap:` is in inches. `fill:` is `"SEQUENTIAL"` (the default) or `"REPEAT"`. |
|
|
240
|
+
| `color_mode:`, `darkness:` | PNG, BMP, GIF, and JPEG | `color_mode: "BW"` returns a black-and-white image at the `darkness:` threshold. |
|
|
241
|
+
|
|
242
|
+
`color_mode:` and `darkness:` are one keyword each, so a value you set acts on an image or PDF
|
|
243
|
+
source and on an image target alike. `zpl:` holds keys for both stages.
|
|
244
|
+
|
|
245
|
+
`split:`, `layout:`, and `zpl: { collapse_copies: }` need a PDF in `IMAGE` mode, the default. With
|
|
246
|
+
`NATIVE`, a split grid or more than 1 across returns `400`, and `collapse_copies:` has no effect.
|
|
247
|
+
|
|
248
|
+
### Other keywords
|
|
249
|
+
|
|
250
|
+
| Keyword | What it does |
|
|
251
|
+
| --- | --- |
|
|
252
|
+
| `base64_text: true` | Sends the body as base64 text with `Content-Type: text/plain`, for the binary sources: PDF, images, NiceLabel, and BarTender. `print` doesn't take it. |
|
|
253
|
+
| `extra:` | A Hash of parameters the SDK doesn't model, merged into the request as is. The server ignores keys it doesn't recognize. |
|
|
254
|
+
|
|
255
|
+
The SDK raises `LabelZoom::ValidationError` before sending a request when `rotation:` isn't a
|
|
256
|
+
multiple of 90, `source_dpi:` or `target_dpi:` isn't a positive whole number, `darkness:` is
|
|
257
|
+
outside 0 to 100, or the body is empty.
|
|
106
258
|
|
|
107
259
|
## Errors
|
|
108
260
|
|
|
109
|
-
Every non-2xx response
|
|
110
|
-
|
|
261
|
+
Every non-2xx response raises a subclass of `LabelZoom::APIError`, so one `rescue` catches them
|
|
262
|
+
all:
|
|
263
|
+
|
|
264
|
+
| Error | Raised for |
|
|
265
|
+
| --- | --- |
|
|
266
|
+
| `BadRequestError` | `400`: a malformed request or an invalid conversion path. |
|
|
267
|
+
| `UnauthorizedError` | `401`: the API key was rejected. |
|
|
268
|
+
| `ForbiddenError` | `403`: the key isn't entitled to the operation. `paid_feature?` is true when the operation needs a paid plan. |
|
|
269
|
+
| `NotFoundError` | `404`, including a `label_index:` past the last label. |
|
|
270
|
+
| `PayloadTooLargeError` | `413`: the body is over the limit. |
|
|
271
|
+
| `UnprocessableError` | `422`: the request is well formed but can't be carried out. |
|
|
272
|
+
| `RateLimitedError` | `429`. `retry_after_seconds` holds the `Retry-After` value. |
|
|
273
|
+
| `PrintQuotaExceededError` | `429` with code `daily_job_limit_reached`. A subclass of `RateLimitedError`. |
|
|
274
|
+
| `ServerError` | `5xx`, after the retries run out. |
|
|
275
|
+
| `APIError` | Any other non-2xx status, such as `406` or `409`. |
|
|
276
|
+
|
|
277
|
+
Each one carries `status`, `message`, `code`, `request_id`, and `raw_body`. `code` is the error's
|
|
278
|
+
stable code, such as `"agent_update_required"`, or `nil` when the response has none, so branch on
|
|
279
|
+
`code` rather than on the message. `message` is capped at 512 characters; `raw_body` is the whole
|
|
280
|
+
body. Conversion responses carry a request ID; Cloud Print responses don't, so `request_id` is
|
|
281
|
+
`nil` for a print.
|
|
111
282
|
|
|
112
283
|
```ruby
|
|
113
284
|
begin
|
|
114
|
-
client.
|
|
115
|
-
rescue LabelZoom::
|
|
116
|
-
warn "
|
|
285
|
+
client.print(:zpl, printer_id, zpl, copies: 3)
|
|
286
|
+
rescue LabelZoom::PrintQuotaExceededError
|
|
287
|
+
warn "The daily print limit is used up. It resets at 00:00 UTC."
|
|
288
|
+
rescue LabelZoom::UnprocessableError => e
|
|
289
|
+
warn "Update the Print Agent." if e.code == "agent_update_required"
|
|
117
290
|
rescue LabelZoom::APIError => e
|
|
118
|
-
warn "
|
|
291
|
+
warn "Request #{e.request_id} failed with #{e.status} (#{e.code}): #{e.message}"
|
|
119
292
|
end
|
|
120
293
|
```
|
|
121
294
|
|
|
122
|
-
|
|
123
|
-
`RateLimitedError` and `ServerError` all descend from `LabelZoom::APIError`, so one `rescue`
|
|
124
|
-
catches the lot.
|
|
295
|
+
Two errors aren't `APIError`s:
|
|
125
296
|
|
|
126
|
-
`LabelZoom::
|
|
127
|
-
|
|
128
|
-
|
|
297
|
+
- `LabelZoom::TransportError` means a request got no response after every retry. It descends from
|
|
298
|
+
`LabelZoom::Error`, the base of every error the gem raises.
|
|
299
|
+
- `LabelZoom::ValidationError` means the SDK rejected the request before sending it, which is a
|
|
300
|
+
bug in the calling code. It descends from `ArgumentError`, so a `rescue LabelZoom::APIError` for
|
|
301
|
+
a fallback doesn't swallow it. `parameter` names the option at fault.
|
|
129
302
|
|
|
130
303
|
## Retries
|
|
131
304
|
|
|
132
|
-
|
|
133
|
-
attempts
|
|
134
|
-
|
|
305
|
+
The client retries `429` responses, `5xx` responses, and network failures twice by default, for
|
|
306
|
+
three attempts in all. It waits 1, 2, and then 4 seconds with full jitter, and waits longer when a
|
|
307
|
+
`Retry-After` header asks for more. The daily print limit's `429` is raised at once. No other `4xx`
|
|
308
|
+
is retried.
|
|
309
|
+
|
|
310
|
+
To change the number of retries, set `max_retries:`. To turn retrying off, set it to 0:
|
|
135
311
|
|
|
136
312
|
```ruby
|
|
137
|
-
client = LabelZoom::Client.new(max_retries: 0)
|
|
313
|
+
client = LabelZoom::Client.new(max_retries: 0)
|
|
138
314
|
```
|
|
139
315
|
|
|
140
|
-
|
|
316
|
+
A print keeps the same `Idempotency-Key` on every retry.
|
|
317
|
+
|
|
318
|
+
## Configure the client
|
|
319
|
+
|
|
320
|
+
`LabelZoom::Client.new` takes these keyword arguments:
|
|
321
|
+
|
|
322
|
+
| Keyword | Default | What it does |
|
|
323
|
+
| --- | --- | --- |
|
|
324
|
+
| `api_key:` | `LABELZOOM_API_KEY` | An `lz_live_` or `lz_test_` key, or a JWT. `nil` or an empty string forces the free tier. |
|
|
325
|
+
| `base_url:` | `https://api.labelzoom.com` | The API host. A path prefix is kept, so a proxy at `https://proxy.example.com/labelzoom` works. |
|
|
326
|
+
| `max_retries:` | `2` | Retries after the first attempt. `0` turns retrying off. |
|
|
327
|
+
| `timeout:` | `60` | The open and read timeout for each attempt, in seconds. |
|
|
328
|
+
| `user_agent_suffix:` | None | Text added to the end of the SDK's `User-Agent`. |
|
|
329
|
+
| `http_builder:` | None | A callable that takes the request `URI` and returns the `Net::HTTP` instance to use, for proxies and custom TLS. |
|
|
330
|
+
| `sleeper:` | `Kernel.sleep` | A callable that waits between retries. |
|
|
331
|
+
| `jitter:` | `true` | Full jitter on the retry delay. |
|
|
332
|
+
| `env:` | `ENV` | Where the client looks up `LABELZOOM_API_KEY`. |
|
|
333
|
+
|
|
334
|
+
`client.build_uri(:zpl, :png)` returns the URL a conversion is posted to, which helps when you debug
|
|
335
|
+
a proxy or a `base_url:`.
|
|
336
|
+
|
|
337
|
+
## Test your own code
|
|
141
338
|
|
|
142
|
-
The sleeper and the environment lookup are
|
|
143
|
-
|
|
339
|
+
The sleeper and the environment lookup are injectable, so a test never sleeps and never picks up a
|
|
340
|
+
developer's real key:
|
|
144
341
|
|
|
145
342
|
```ruby
|
|
146
343
|
slept = []
|
|
@@ -151,26 +348,47 @@ client = LabelZoom::Client.new(
|
|
|
151
348
|
)
|
|
152
349
|
```
|
|
153
350
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
rather than bypassed.
|
|
351
|
+
To stub HTTP, use [WebMock](https://github.com/bblimke/webmock). It intercepts at the `Net::HTTP`
|
|
352
|
+
layer, so your test runs the SDK's real request code.
|
|
157
353
|
|
|
158
|
-
##
|
|
354
|
+
## Migrate from 1.x
|
|
355
|
+
|
|
356
|
+
The `convert` call, its nested-hash options, and `ConversionResult` are unchanged. These calls
|
|
357
|
+
change:
|
|
358
|
+
|
|
359
|
+
| 1.x | 2.0 |
|
|
360
|
+
| --- | --- |
|
|
361
|
+
| `gem "labelzoom", "~> 1.0"` | `gem "labelzoom", "~> 2.0"` |
|
|
362
|
+
| `dpi: 300` | `source_dpi: 300` for the resolution the source was made for, `target_dpi: 300` for the output's, or both. Drop `source_dpi:` for a PDF, design-file, XML, or JSON source, and `target_dpi:` for a PDF target: each returns `400` there. 2.0 raises `LabelZoom::ValidationError` for `dpi:`, naming both. |
|
|
363
|
+
| `rescue LabelZoom::APIError` for a `422` | `rescue LabelZoom::UnprocessableError`, which is still an `APIError`. |
|
|
364
|
+
| Matching `e.message` | `e.code` |
|
|
365
|
+
| None | `client.print`, `client.print_template`, `client.jobs.get`, and `client.print_quota`. |
|
|
366
|
+
| None | `label_index:`, `split:`, `layout:`, and `zpl: { collapse_copies: }`. |
|
|
367
|
+
| None | The `:cpcl` target, and the `:esclabel`, `:fingerprint`, `:ezpl`, `:cpcl`, `:tpcl`, `:nlbl`, `:lbl`, `:btw`, and `:dymo` sources. |
|
|
368
|
+
|
|
369
|
+
## Contributing
|
|
370
|
+
|
|
371
|
+
To run the linter and the tests:
|
|
159
372
|
|
|
160
373
|
```sh
|
|
374
|
+
cd ruby
|
|
161
375
|
bundle install
|
|
162
|
-
bundle exec rake # rubocop
|
|
376
|
+
bundle exec rake # rubocop, then rspec
|
|
163
377
|
```
|
|
164
378
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
379
|
+
`bundle exec rubocop` and `bundle exec rspec` run each one alone. The spec suite runs WebMock with
|
|
380
|
+
network connections disabled, so it sends no requests.
|
|
381
|
+
|
|
382
|
+
The suite runs every shared fixture in [`../conformance/`](../conformance/), the cases every
|
|
383
|
+
LabelZoom SDK runs, and asserts that it ran all of them. See
|
|
384
|
+
[`../docs/CONFORMANCE.md`](../docs/CONFORMANCE.md) for how the fixtures work, and
|
|
385
|
+
[`../docs/API_CONTRACT.md`](../docs/API_CONTRACT.md) for the rules they check.
|
|
168
386
|
|
|
169
|
-
Ruby
|
|
387
|
+
Ruby skips the two `typecheck/*` cases in
|
|
170
388
|
[`../conformance/skips/ruby.json`](../conformance/skips/ruby.json), because Ruby has no compile
|
|
171
|
-
step. `spec/labelzoom/formats_spec.rb`
|
|
172
|
-
|
|
389
|
+
step. `spec/labelzoom/formats_spec.rb` asserts the run-time check that the skip relies on: a
|
|
390
|
+
source-only format passed as a target raises `LabelZoom::ValidationError`.
|
|
173
391
|
|
|
174
392
|
## License
|
|
175
393
|
|
|
176
|
-
MIT
|
|
394
|
+
MIT. See [LICENSE](LICENSE).
|