ruby-anvil 0.0.1.pre.rc.1
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 +7 -0
- data/CHANGELOG.md +61 -0
- data/LICENSE.txt +21 -0
- data/README.md +802 -0
- data/lib/anvil/auth.rb +66 -0
- data/lib/anvil/client.rb +624 -0
- data/lib/anvil/errors.rb +230 -0
- data/lib/anvil/graphql.rb +270 -0
- data/lib/anvil/multipart.rb +104 -0
- data/lib/anvil/payloads/base.rb +92 -0
- data/lib/anvil/payloads/create_etch_packet.rb +13 -0
- data/lib/anvil/payloads/etch_file.rb +21 -0
- data/lib/anvil/payloads/etch_signer.rb +22 -0
- data/lib/anvil/payloads/fill_pdf.rb +13 -0
- data/lib/anvil/payloads/forge_submit.rb +16 -0
- data/lib/anvil/payloads/generate_pdf.rb +26 -0
- data/lib/anvil/rate_limiter.rb +236 -0
- data/lib/anvil/response.rb +84 -0
- data/lib/anvil/types/base.rb +147 -0
- data/lib/anvil/types/etch_packet.rb +15 -0
- data/lib/anvil/types/generated/graphql.rb +1713 -0
- data/lib/anvil/types/generated/operations.rb +1342 -0
- data/lib/anvil/types/generated/rest.rb +496 -0
- data/lib/anvil/upload_with_options.rb +113 -0
- data/lib/anvil/uploads.rb +119 -0
- data/lib/anvil/version.rb +5 -0
- data/lib/anvil.rb +98 -0
- data/lib/ruby-anvil.rb +5 -0
- metadata +73 -0
data/README.md
ADDED
|
@@ -0,0 +1,802 @@
|
|
|
1
|
+
# Anvil API Client for Ruby
|
|
2
|
+
|
|
3
|
+
[](https://rubygems.org/gems/ruby-anvil)
|
|
4
|
+
[](https://github.com/anvilco/ruby-anvil/actions/workflows/ci.yml)
|
|
5
|
+
|
|
6
|
+
[Anvil](https://www.useanvil.com/developers) is a suite of APIs for paperwork:
|
|
7
|
+
|
|
8
|
+
- [PDF filling](https://www.useanvil.com/docs/api/fill-pdf): fill a PDF template with JSON data
|
|
9
|
+
- [PDF generation](https://www.useanvil.com/docs/api/generate-pdf): turn HTML and CSS, or markdown, into a PDF
|
|
10
|
+
- [Etch e-sign](https://www.useanvil.com/docs/api/e-signatures): send documents out for signature
|
|
11
|
+
- [Workflows](https://www.useanvil.com/docs/api/workflows): collect data through a webform and pour it into documents
|
|
12
|
+
|
|
13
|
+
This gem wraps all of it. It is built on the standard library alone, so it adds
|
|
14
|
+
no runtime dependencies to your app.
|
|
15
|
+
|
|
16
|
+
## Contents
|
|
17
|
+
|
|
18
|
+
- [Installation](#installation)
|
|
19
|
+
- [Quick start](#quick-start)
|
|
20
|
+
- [Authentication](#authentication)
|
|
21
|
+
- [Configuration](#configuration)
|
|
22
|
+
- [Methods](#methods)
|
|
23
|
+
- [fill_pdf](#fill_pdfpdf_template_id-payload-options)
|
|
24
|
+
- [generate_pdf](#generate_pdfpayload-options)
|
|
25
|
+
- [create_etch_packet](#create_etch_packetvariables-response_query-mutation)
|
|
26
|
+
- [get_etch_packet](#get_etch_packeteid-response_query)
|
|
27
|
+
- [get_current_user](#get_current_userresponse_query)
|
|
28
|
+
- [generate_etch_sign_url](#generate_etch_sign_urlsigner_eid-client_user_id)
|
|
29
|
+
- [download_documents](#download_documentsdocument_group_eid-options)
|
|
30
|
+
- [forge_submit](#forge_submitvariables-response_query-mutation)
|
|
31
|
+
- [remove_weld_data](#remove_weld_dataeid)
|
|
32
|
+
- [request_graphql](#request_graphqlquery-variables)
|
|
33
|
+
- [request_rest](#request_restpath-options)
|
|
34
|
+
- [Version constants](#version-constants)
|
|
35
|
+
- [File uploads](#file-uploads)
|
|
36
|
+
- [Response types](#response-types)
|
|
37
|
+
- [Errors](#errors)
|
|
38
|
+
- [Rate limits and retries](#rate-limits-and-retries)
|
|
39
|
+
- [Typed payloads and responses](#typed-payloads-and-responses)
|
|
40
|
+
- [Development](#development)
|
|
41
|
+
- [Integration test](#integration-test)
|
|
42
|
+
- [Publishing](#publishing)
|
|
43
|
+
|
|
44
|
+
## Installation
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
# Gemfile
|
|
48
|
+
gem "ruby-anvil"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Or without Bundler:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
gem install ruby-anvil
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
Then:
|
|
58
|
+
|
|
59
|
+
```ruby
|
|
60
|
+
require "anvil"
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
The gem is named `ruby-anvil` and the library is named `Anvil`. `require
|
|
64
|
+
"ruby-anvil"` works too, and does the same thing.
|
|
65
|
+
|
|
66
|
+
Ruby 3.1 or newer.
|
|
67
|
+
|
|
68
|
+
## Quick start
|
|
69
|
+
|
|
70
|
+
```ruby
|
|
71
|
+
require "anvil"
|
|
72
|
+
|
|
73
|
+
client = Anvil::Client.new(api_key: ENV.fetch("ANVIL_API_KEY"))
|
|
74
|
+
|
|
75
|
+
response = client.fill_pdf("kA6Da9CuGqUtc6QiBDRR", {
|
|
76
|
+
title: "My PDF Title",
|
|
77
|
+
data: { someFieldId: "Hello World!" }
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
File.binwrite("filled.pdf", response.data)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Method names are Ruby's, so node-anvil's `fillPDF` is `fill_pdf` here and
|
|
84
|
+
`createEtchPacket` is `create_etch_packet`. Payload keys can be Ruby's too: the
|
|
85
|
+
typed payloads under `Anvil::Payloads` take `is_test` and send `isTest`. A plain
|
|
86
|
+
Hash is passed through exactly as you write it, so use the API's own camelCase
|
|
87
|
+
there.
|
|
88
|
+
|
|
89
|
+
## Authentication
|
|
90
|
+
|
|
91
|
+
Anvil takes either an API key or an OAuth access token. Pass one to the
|
|
92
|
+
constructor:
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
# An API key from your organization's settings, sent as Basic auth
|
|
96
|
+
client = Anvil::Client.new(api_key: "7j2JuUWmN4fGjBxsCltWaybHOEy3UEtt")
|
|
97
|
+
|
|
98
|
+
# An OAuth access token, sent as Bearer auth
|
|
99
|
+
client = Anvil::Client.new(access_token: "an-oauth-access-token")
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Each goes in the `Authorization` header the way the other Anvil clients send
|
|
103
|
+
it, so a credential that works in one client works in the other:
|
|
104
|
+
|
|
105
|
+
| Credential | Header |
|
|
106
|
+
| --- | --- |
|
|
107
|
+
| `api_key` | `Basic ` + base64 of `"#{api_key}:"` |
|
|
108
|
+
| `access_token`, a JWT | `Bearer ` + the token, unencoded |
|
|
109
|
+
| `access_token`, anything else | `Bearer ` + base64 of the token |
|
|
110
|
+
|
|
111
|
+
Note the trailing colon on the API key: it is a username with an empty
|
|
112
|
+
password, in the HTTP Basic style. The client handles this; you pass the raw
|
|
113
|
+
key.
|
|
114
|
+
|
|
115
|
+
Anvil's OAuth access tokens are JWTs, which the API wants verbatim. Any other
|
|
116
|
+
token is base64 encoded, which the API still accepts. The client works out
|
|
117
|
+
which it has, so you pass the token you were given either way.
|
|
118
|
+
|
|
119
|
+
`api_key` defaults to the **`ANVIL_API_KEY`** environment variable, so a client
|
|
120
|
+
with no arguments works when that is set:
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
client = Anvil::Client.new
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Find your API key in your organization's settings in the
|
|
127
|
+
[Anvil dashboard](https://app.useanvil.com). See the
|
|
128
|
+
[API key docs](https://www.useanvil.com/docs/api/getting-started#api-key) for
|
|
129
|
+
more.
|
|
130
|
+
|
|
131
|
+
## Configuration
|
|
132
|
+
|
|
133
|
+
```ruby
|
|
134
|
+
client = Anvil::Client.new(
|
|
135
|
+
api_key: ENV.fetch("ANVIL_API_KEY"),
|
|
136
|
+
|
|
137
|
+
# The Anvil environment to talk to. Defaults to production
|
|
138
|
+
base_url: "https://staging.useanvil.com",
|
|
139
|
+
|
|
140
|
+
# Overrides the User-Agent header
|
|
141
|
+
user_agent: "My App/1.0",
|
|
142
|
+
|
|
143
|
+
# What the client allows itself until the API tells it the real limit
|
|
144
|
+
request_limit: 1,
|
|
145
|
+
request_limit_ms: 1000,
|
|
146
|
+
|
|
147
|
+
# How many times to retry a request the API could not answer. Zero turns it
|
|
148
|
+
# off. A 429 is always retried, for as long as the API keeps asking
|
|
149
|
+
max_retries: 5,
|
|
150
|
+
|
|
151
|
+
# Seconds to wait for a connection, and for a response
|
|
152
|
+
timeout: 300,
|
|
153
|
+
|
|
154
|
+
# Share one limiter per process with every other client for the same
|
|
155
|
+
# environment and credentials
|
|
156
|
+
share_rate_limiter: true
|
|
157
|
+
)
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
### base_url
|
|
161
|
+
|
|
162
|
+
`base_url` defaults to `https://app.useanvil.com`. Every endpoint is derived
|
|
163
|
+
from it, so setting it once points the whole client at another environment:
|
|
164
|
+
|
|
165
|
+
| Endpoint | URL |
|
|
166
|
+
| ------------------ | --------------------------------------------- |
|
|
167
|
+
| REST | `{base_url}/api/v1/...` |
|
|
168
|
+
| GraphQL | `{base_url}/graphql` |
|
|
169
|
+
| Document downloads | `{base_url}/api/document-group/{eid}.zip` |
|
|
170
|
+
|
|
171
|
+
```ruby
|
|
172
|
+
client = Anvil::Client.new(api_key: key, base_url: "https://staging.useanvil.com")
|
|
173
|
+
# GraphQL now goes to https://staging.useanvil.com/graphql
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Methods
|
|
177
|
+
|
|
178
|
+
Every method raises on failure rather than returning an error, so the happy
|
|
179
|
+
path reads straight through. See [Errors](#errors).
|
|
180
|
+
|
|
181
|
+
### `fill_pdf(pdf_template_id, payload, **options)`
|
|
182
|
+
|
|
183
|
+
Fills a PDF template with your JSON data, and returns the filled PDF.
|
|
184
|
+
|
|
185
|
+
You will need to have
|
|
186
|
+
[uploaded a PDF to Anvil](https://useanvil.com/docs/api/fill-pdf#creating-a-pdf-template)
|
|
187
|
+
first. Its id is on the template's page in the dashboard, and in that page's
|
|
188
|
+
URL: `https://app.useanvil.com/org/<your-org-slug>/pdf/<pdf-template-id>`.
|
|
189
|
+
|
|
190
|
+
```ruby
|
|
191
|
+
response = client.fill_pdf("kA6Da9CuGqUtc6QiBDRR", {
|
|
192
|
+
title: "My PDF Title",
|
|
193
|
+
fontSize: 10,
|
|
194
|
+
textColor: "#CC0000",
|
|
195
|
+
data: { someFieldId: "Hello World!" }
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
File.binwrite("filled.pdf", response.data)
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
The payload's keys can be passed as keyword arguments, or as a typed
|
|
202
|
+
`Anvil::Payloads::FillPdf`:
|
|
203
|
+
|
|
204
|
+
```ruby
|
|
205
|
+
client.fill_pdf("kA6Da9CuGqUtc6QiBDRR", data: { someFieldId: "Hello World!" })
|
|
206
|
+
|
|
207
|
+
client.fill_pdf("kA6Da9CuGqUtc6QiBDRR", Anvil::Payloads::FillPdf.new(
|
|
208
|
+
title: "My PDF Title",
|
|
209
|
+
font_size: 10,
|
|
210
|
+
text_color: "#CC0000",
|
|
211
|
+
data: { someFieldId: "Hello World!" }
|
|
212
|
+
))
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Options:
|
|
216
|
+
|
|
217
|
+
- `data_type` (Symbol) `:string` (the default), `:io` or `:stream`. See
|
|
218
|
+
[Response types](#response-types)
|
|
219
|
+
- `version_number` (Integer) which version of the template to fill. Defaults to
|
|
220
|
+
the latest published version. See [Version constants](#version-constants)
|
|
221
|
+
- `to` (IO) where to write a `:stream` response
|
|
222
|
+
|
|
223
|
+
Returns an `Anvil::RestResponse`: `data` is the PDF, and `status_code` and
|
|
224
|
+
`headers` are there if you want them.
|
|
225
|
+
|
|
226
|
+
### `generate_pdf(payload, **options)`
|
|
227
|
+
|
|
228
|
+
Generates a new PDF from HTML and CSS, or from markdown. Nothing has to be set
|
|
229
|
+
up in the dashboard first: send data, get a PDF.
|
|
230
|
+
|
|
231
|
+
```ruby
|
|
232
|
+
# HTML and CSS
|
|
233
|
+
response = client.generate_pdf(
|
|
234
|
+
title: "Example",
|
|
235
|
+
type: "html",
|
|
236
|
+
data: {
|
|
237
|
+
html: "<h1 class='header-one'>What is Lorem Ipsum?</h1><p>Lorem Ipsum is...</p>",
|
|
238
|
+
css: "body { font-size: 14px; color: #171717; } .header-one { text-decoration: underline; }"
|
|
239
|
+
}
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
# Markdown
|
|
243
|
+
response = client.generate_pdf(
|
|
244
|
+
title: "Example Invoice",
|
|
245
|
+
data: [
|
|
246
|
+
{ label: "Name", content: "Sally Jones" },
|
|
247
|
+
{ content: "Lorem **ipsum** dolor sit _amet_" },
|
|
248
|
+
{
|
|
249
|
+
table: {
|
|
250
|
+
firstRowHeaders: true,
|
|
251
|
+
rows: [
|
|
252
|
+
["Description", "Quantity", "Price"],
|
|
253
|
+
["4x Large Widgets", "4", "$40.00"]
|
|
254
|
+
]
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
]
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
File.binwrite("generated.pdf", response.data)
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
Takes the same options as `fill_pdf`, except `version_number`. See the
|
|
264
|
+
[HTML to PDF](https://www.useanvil.com/docs/api/generate-pdf#html--css-to-pdf)
|
|
265
|
+
and [markdown to PDF](https://www.useanvil.com/docs/api/generate-pdf#markdown-to-pdf)
|
|
266
|
+
docs for everything `data` accepts.
|
|
267
|
+
|
|
268
|
+
### `create_etch_packet(variables:, response_query: nil, mutation: nil)`
|
|
269
|
+
|
|
270
|
+
Creates an Etch e-sign packet, and sends it to the first signer unless it is a
|
|
271
|
+
draft.
|
|
272
|
+
|
|
273
|
+
```ruby
|
|
274
|
+
packet = client.create_etch_packet(variables: Anvil::Payloads::CreateEtchPacket.new(
|
|
275
|
+
name: "Test Docs - Testy Signer",
|
|
276
|
+
# The packet is ready to send: an email goes to the first signer.
|
|
277
|
+
# Use is_draft: true to review it in the dashboard first
|
|
278
|
+
is_draft: false,
|
|
279
|
+
# Test packets use development signatures and are not billed
|
|
280
|
+
is_test: true,
|
|
281
|
+
signature_email_subject: "Please sign these documents",
|
|
282
|
+
|
|
283
|
+
files: [
|
|
284
|
+
Anvil::Payloads::EtchFile.new(id: "sampleTemplate", cast_eid: "05xXsZko33JIO6aq5Pnr")
|
|
285
|
+
],
|
|
286
|
+
|
|
287
|
+
# Fills the PDFs before they go out for signature
|
|
288
|
+
data: { payloads: { sampleTemplate: { data: { name: "Testy Signer" } } } },
|
|
289
|
+
|
|
290
|
+
signers: [
|
|
291
|
+
Anvil::Payloads::EtchSigner.new(
|
|
292
|
+
id: "signer1",
|
|
293
|
+
name: "Testy Signer",
|
|
294
|
+
email: "testy@example.com",
|
|
295
|
+
signer_type: "email",
|
|
296
|
+
fields: [Anvil::Payloads::SignerField.new(file_id: "sampleTemplate", field_id: "signature")]
|
|
297
|
+
)
|
|
298
|
+
]
|
|
299
|
+
))
|
|
300
|
+
|
|
301
|
+
puts packet.details_url
|
|
302
|
+
puts packet.signers.map(&:eid)
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
- `variables` (`Anvil::Payloads::CreateEtchPacket` or Hash) the mutation's
|
|
306
|
+
variables. See the [API reference](https://www.useanvil.com/docs/api/graphql/reference/)
|
|
307
|
+
- `response_query` (String) the fields to select on the new packet, instead of
|
|
308
|
+
the [default set](#typed-payloads-and-responses)
|
|
309
|
+
- `mutation` (String) replaces the whole mutation, response query and all, for
|
|
310
|
+
when you want complete control
|
|
311
|
+
|
|
312
|
+
Returns an `Anvil::Types::EtchPacket`.
|
|
313
|
+
|
|
314
|
+
To upload a new PDF with the packet rather than using a template, see
|
|
315
|
+
[File uploads](#file-uploads).
|
|
316
|
+
|
|
317
|
+
### `get_etch_packet(eid:, response_query: nil)`
|
|
318
|
+
|
|
319
|
+
Reads an Etch packet.
|
|
320
|
+
|
|
321
|
+
```ruby
|
|
322
|
+
packet = client.get_etch_packet(eid: "AwpXqV3mnjeMgQb0VHKk")
|
|
323
|
+
|
|
324
|
+
packet.status
|
|
325
|
+
packet.number_remaining_signers
|
|
326
|
+
packet.document_group.eid
|
|
327
|
+
packet.signers.map { |signer| [signer.email, signer.status] }
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
Pass `variables:` instead of `eid:` if you would rather give the query's
|
|
331
|
+
variables whole.
|
|
332
|
+
|
|
333
|
+
### `get_current_user(response_query:)`
|
|
334
|
+
|
|
335
|
+
Reads whoever the API key or access token belongs to.
|
|
336
|
+
|
|
337
|
+
```ruby
|
|
338
|
+
user = client.get_current_user
|
|
339
|
+
|
|
340
|
+
user.eid
|
|
341
|
+
user.email
|
|
342
|
+
user.organizations.map(&:slug)
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
Returns an `Anvil::Types::User`.
|
|
346
|
+
|
|
347
|
+
### `generate_etch_sign_url(signer_eid:, client_user_id:)`
|
|
348
|
+
|
|
349
|
+
Generates a signing URL for one signer, for signing in your own app rather than
|
|
350
|
+
through Anvil's email. The signer must have been created with a `signer_type`
|
|
351
|
+
of `"embedded"`.
|
|
352
|
+
|
|
353
|
+
```ruby
|
|
354
|
+
url = client.generate_etch_sign_url(
|
|
355
|
+
signer_eid: "XcVgTZ2hBjmYFhtR5nIj",
|
|
356
|
+
client_user_id: "your-user-id"
|
|
357
|
+
)
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Returns the URL as a String.
|
|
361
|
+
|
|
362
|
+
### `download_documents(document_group_eid, **options)`
|
|
363
|
+
|
|
364
|
+
Downloads a document group's documents, as a zip.
|
|
365
|
+
|
|
366
|
+
```ruby
|
|
367
|
+
response = client.download_documents("6Dnu5jXb0Z1MGJTdhEJX")
|
|
368
|
+
File.binwrite("documents.zip", response.data)
|
|
369
|
+
|
|
370
|
+
# Or stream it straight to disk
|
|
371
|
+
File.open("documents.zip", "wb") do |file|
|
|
372
|
+
client.download_documents("6Dnu5jXb0Z1MGJTdhEJX", data_type: :stream, to: file)
|
|
373
|
+
end
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
Takes `data_type` and `to`, the same as `fill_pdf`.
|
|
377
|
+
|
|
378
|
+
### `forge_submit(variables:, response_query: nil, mutation: nil)`
|
|
379
|
+
|
|
380
|
+
Submits data to one webform (a Forge) of a Workflow.
|
|
381
|
+
|
|
382
|
+
```ruby
|
|
383
|
+
submission = client.forge_submit(variables: Anvil::Payloads::ForgeSubmit.new(
|
|
384
|
+
forge_eid: "FsqmyvJjDRNvqmyvJsqm",
|
|
385
|
+
payload: { name: "Sally Jones", email: "sally@example.com" },
|
|
386
|
+
is_test: true
|
|
387
|
+
))
|
|
388
|
+
|
|
389
|
+
submission.eid
|
|
390
|
+
submission.weld_data.eid # the Workflow submission this went into
|
|
391
|
+
submission.continue_url
|
|
392
|
+
```
|
|
393
|
+
|
|
394
|
+
Leave `weld_data_eid` out to start a new Workflow submission, and pass the one
|
|
395
|
+
you got back to add to an existing one.
|
|
396
|
+
|
|
397
|
+
Returns an `Anvil::Types::Submission`.
|
|
398
|
+
|
|
399
|
+
### `remove_weld_data(eid:)`
|
|
400
|
+
|
|
401
|
+
Removes a Workflow submission and everything in it. This cannot be undone.
|
|
402
|
+
|
|
403
|
+
```ruby
|
|
404
|
+
client.remove_weld_data(eid: "weldDataEid") # => true
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### `request_graphql(query:, variables: {})`
|
|
408
|
+
|
|
409
|
+
Runs any GraphQL query or mutation, for everything without a method of its own.
|
|
410
|
+
See the [GraphQL reference](https://www.useanvil.com/docs/api/graphql/reference/)
|
|
411
|
+
for what the API can do.
|
|
412
|
+
|
|
413
|
+
```ruby
|
|
414
|
+
response = client.request_graphql(
|
|
415
|
+
query: <<~GRAPHQL,
|
|
416
|
+
query WeldDataQuery($eid: String!) {
|
|
417
|
+
weldData(eid: $eid) {
|
|
418
|
+
eid
|
|
419
|
+
isComplete
|
|
420
|
+
isTest
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
GRAPHQL
|
|
424
|
+
variables: { eid: "nxflNZqxDUbltLUbYWK" }
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
response["weldData"]["isComplete"] # the result of one operation
|
|
428
|
+
response.data # the whole GraphQL data map
|
|
429
|
+
response.status_code
|
|
430
|
+
```
|
|
431
|
+
|
|
432
|
+
Files anywhere in `variables` are uploaded as multipart. See
|
|
433
|
+
[File uploads](#file-uploads).
|
|
434
|
+
|
|
435
|
+
Returns an `Anvil::GraphQLResponse`, and raises `Anvil::GraphQLError` when the
|
|
436
|
+
response carries GraphQL errors.
|
|
437
|
+
|
|
438
|
+
### `request_rest(path, **options)`
|
|
439
|
+
|
|
440
|
+
Runs any REST request, for the endpoints without a method of their own.
|
|
441
|
+
|
|
442
|
+
```ruby
|
|
443
|
+
response = client.request_rest(
|
|
444
|
+
"/api/v1/fill/#{pdf_template_id}.pdf",
|
|
445
|
+
method: :post,
|
|
446
|
+
body: payload,
|
|
447
|
+
data_type: :stream,
|
|
448
|
+
to: file
|
|
449
|
+
)
|
|
450
|
+
```
|
|
451
|
+
|
|
452
|
+
- `path` (String) a path from `base_url`, or a whole URL
|
|
453
|
+
- `method` (Symbol) `:get`, `:post`, `:put`, `:patch`, `:delete` or `:head`
|
|
454
|
+
- `body` (Hash, `Anvil::Payloads::Base`, String or nil) a Hash or payload is
|
|
455
|
+
sent as JSON, a String is sent as it is
|
|
456
|
+
- `headers` (Hash) extra request headers
|
|
457
|
+
- `data_type` (Symbol) `:string`, `:io`, `:json` or `:stream`
|
|
458
|
+
- `to` (IO) where to write a `:stream` response
|
|
459
|
+
|
|
460
|
+
Returns an `Anvil::RestResponse`.
|
|
461
|
+
|
|
462
|
+
## Version constants
|
|
463
|
+
|
|
464
|
+
PDF templates are versioned. `fill_pdf` uses the latest published version
|
|
465
|
+
unless you ask for another:
|
|
466
|
+
|
|
467
|
+
```ruby
|
|
468
|
+
Anvil::VERSION_LATEST # => -1, the latest version, usually the current draft
|
|
469
|
+
Anvil::VERSION_LATEST_PUBLISHED # => -2, the latest published version, the API's default
|
|
470
|
+
|
|
471
|
+
# Fill with the current draft, to check a change before publishing it
|
|
472
|
+
client.fill_pdf(template_id, payload, version_number: Anvil::VERSION_LATEST)
|
|
473
|
+
|
|
474
|
+
# Or pin to a specific version
|
|
475
|
+
client.fill_pdf(template_id, payload, version_number: 3)
|
|
476
|
+
```
|
|
477
|
+
|
|
478
|
+
## File uploads
|
|
479
|
+
|
|
480
|
+
Anvil takes a new PDF in three ways: a URL it fetches itself, base64 data
|
|
481
|
+
inline, or a multipart file upload. The multipart route is the one to use for
|
|
482
|
+
anything large, and `Anvil.prepare_graphql_file` is how you say so:
|
|
483
|
+
|
|
484
|
+
```ruby
|
|
485
|
+
# From a path
|
|
486
|
+
file = Anvil.prepare_graphql_file("./contract.pdf")
|
|
487
|
+
|
|
488
|
+
# From an open IO
|
|
489
|
+
file = Anvil.prepare_graphql_file(File.open("./contract.pdf", "rb"))
|
|
490
|
+
|
|
491
|
+
# From bytes you already have, which need a filename of their own
|
|
492
|
+
file = Anvil.prepare_graphql_file(StringIO.new(bytes), filename: "contract.pdf")
|
|
493
|
+
|
|
494
|
+
# Override what it is uploaded as
|
|
495
|
+
file = Anvil.prepare_graphql_file("./contract", filename: "contract.pdf", mimetype: "application/pdf")
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
Put the result wherever the API takes an `Upload`, which is always the `file`
|
|
499
|
+
key of an object:
|
|
500
|
+
|
|
501
|
+
```ruby
|
|
502
|
+
packet = client.create_etch_packet(variables: {
|
|
503
|
+
name: "Mutual NDA",
|
|
504
|
+
isTest: true,
|
|
505
|
+
files: [{
|
|
506
|
+
id: "nda",
|
|
507
|
+
title: "Mutual NDA",
|
|
508
|
+
file: Anvil.prepare_graphql_file("./nda.pdf"),
|
|
509
|
+
fields: [{
|
|
510
|
+
id: "signature",
|
|
511
|
+
type: "signature",
|
|
512
|
+
pageNum: 1,
|
|
513
|
+
rect: { x: 100, y: 100, width: 200, height: 40 }
|
|
514
|
+
}]
|
|
515
|
+
}],
|
|
516
|
+
signers: [{ id: "signer1", name: "Sally", email: "sally@example.com", signerType: "email",
|
|
517
|
+
fields: [{ fileId: "nda", fieldId: "signature" }] }]
|
|
518
|
+
})
|
|
519
|
+
```
|
|
520
|
+
|
|
521
|
+
The client sends it over the
|
|
522
|
+
[GraphQL multipart request spec](https://github.com/jaydenseric/graphql-multipart-request-spec),
|
|
523
|
+
assembling the body in a tempfile so a large PDF is never held in memory whole.
|
|
524
|
+
This is the Ruby equivalent of node-anvil's `Anvil.prepareGraphQLFile()`.
|
|
525
|
+
|
|
526
|
+
The other two shapes go in the same place, as plain data:
|
|
527
|
+
|
|
528
|
+
```ruby
|
|
529
|
+
# base64
|
|
530
|
+
file: { data: Base64.strict_encode64(bytes), filename: "nda.pdf", mimetype: "application/pdf" }
|
|
531
|
+
|
|
532
|
+
# a URL Anvil fetches itself, which has to be publicly reachable
|
|
533
|
+
file: { url: "https://example.com/nda.pdf" }
|
|
534
|
+
```
|
|
535
|
+
|
|
536
|
+
An upload anywhere other than under a `file` key is rejected before the request
|
|
537
|
+
is made, with `Anvil::ConfigurationError`, since the API would reject it too.
|
|
538
|
+
|
|
539
|
+
## Response types
|
|
540
|
+
|
|
541
|
+
The methods that return a file take a `data_type`:
|
|
542
|
+
|
|
543
|
+
| `data_type` | `response.data` is |
|
|
544
|
+
| ------------------- | ------------------------------------------------------------ |
|
|
545
|
+
| `:string` (default) | a binary String of the whole body |
|
|
546
|
+
| `:io` | a `StringIO` over the whole body |
|
|
547
|
+
| `:stream` | the number of bytes handed to your block or written to `to:` |
|
|
548
|
+
| `:json` | the parsed JSON (`request_rest` only) |
|
|
549
|
+
|
|
550
|
+
`:stream` never holds the body in memory, so it is the one to use for a large
|
|
551
|
+
PDF or zip. It needs somewhere to put the bytes: a block, or an IO as `to:`.
|
|
552
|
+
|
|
553
|
+
```ruby
|
|
554
|
+
# To a block
|
|
555
|
+
client.fill_pdf(template_id, payload, data_type: :stream) do |chunk|
|
|
556
|
+
digest << chunk
|
|
557
|
+
end
|
|
558
|
+
|
|
559
|
+
# To an IO
|
|
560
|
+
File.open("filled.pdf", "wb") do |file|
|
|
561
|
+
client.fill_pdf(template_id, payload, data_type: :stream, to: file)
|
|
562
|
+
end
|
|
563
|
+
```
|
|
564
|
+
|
|
565
|
+
## Errors
|
|
566
|
+
|
|
567
|
+
Anything that goes wrong raises. Every exception descends from `Anvil::Error`,
|
|
568
|
+
so one rescue catches the lot:
|
|
569
|
+
|
|
570
|
+
```ruby
|
|
571
|
+
begin
|
|
572
|
+
client.fill_pdf(template_id, payload)
|
|
573
|
+
rescue Anvil::ValidationError => e
|
|
574
|
+
e.status_code # => 422
|
|
575
|
+
e.message # => "ValidationError (data: Required)"
|
|
576
|
+
e.fields.map(&:property) # => ["data"]
|
|
577
|
+
e.fields.map(&:message) # => ["Required"]
|
|
578
|
+
rescue Anvil::Error => e
|
|
579
|
+
warn "Anvil call failed: #{e.message}"
|
|
580
|
+
end
|
|
581
|
+
```
|
|
582
|
+
|
|
583
|
+
| Exception | Raised when |
|
|
584
|
+
| --------------------------- | ---------------------------------------------------------------- |
|
|
585
|
+
| `Anvil::ConfigurationError` | the client was called with unusable arguments, before any request |
|
|
586
|
+
| `Anvil::ConnectionError` | the API could not be reached, or answered with unparseable JSON |
|
|
587
|
+
| `Anvil::ValidationError` | 400 or 422 |
|
|
588
|
+
| `Anvil::AuthenticationError`| 401 |
|
|
589
|
+
| `Anvil::ForbiddenError` | 403 |
|
|
590
|
+
| `Anvil::NotFoundError` | 404 |
|
|
591
|
+
| `Anvil::RateLimitError` | 429, after the retries were used up |
|
|
592
|
+
| `Anvil::ClientError` | any other 4xx |
|
|
593
|
+
| `Anvil::ServerError` | 5xx |
|
|
594
|
+
| `Anvil::GraphQLError` | a GraphQL response carrying errors, which the API sends as a 200 |
|
|
595
|
+
|
|
596
|
+
Everything from `Anvil::ValidationError` down is an `Anvil::ResponseError`,
|
|
597
|
+
which carries:
|
|
598
|
+
|
|
599
|
+
- `status_code` (Integer)
|
|
600
|
+
- `errors` (Array of `Anvil::ErrorDetail`) every error the API reported, each
|
|
601
|
+
with a `name`, a `message`, any `fields`, and the `raw` error as sent
|
|
602
|
+
- `fields` (Array of `Anvil::ErrorField`) the field-level details across all of
|
|
603
|
+
them, each with a `message` and the `property` at fault
|
|
604
|
+
- `body` the parsed response body
|
|
605
|
+
- `headers` (Hash)
|
|
606
|
+
|
|
607
|
+
GraphQL and REST errors are normalized into the same shape.
|
|
608
|
+
|
|
609
|
+
Two rules about what counts as an error:
|
|
610
|
+
|
|
611
|
+
- A GraphQL response carrying an `errors` array raises, even though the API
|
|
612
|
+
answers those with a 200.
|
|
613
|
+
- A 2xx whose body happens to carry only a `name` or a `message` does **not**
|
|
614
|
+
raise. node-anvil treats that as an error; python-anvil and dotnet-anvil do
|
|
615
|
+
not, and this client follows them.
|
|
616
|
+
|
|
617
|
+
## Rate limits and retries
|
|
618
|
+
|
|
619
|
+
The API rate limits by organization. The client throttles itself to stay under
|
|
620
|
+
the limit rather than letting you hit it:
|
|
621
|
+
|
|
622
|
+
1. It starts at one request a second, which is below any real limit.
|
|
623
|
+
2. The first response carries your organization's actual limit in its
|
|
624
|
+
`x-ratelimit-limit` and `x-ratelimit-interval-ms` headers, and the limiter
|
|
625
|
+
resizes itself from those. Requests made before that answer wait for it.
|
|
626
|
+
3. A request takes a slot before it is sent and gives it back when its response
|
|
627
|
+
arrives, and a finished request keeps counting for one interval. So the
|
|
628
|
+
requests in flight count against the limit alongside the ones that just
|
|
629
|
+
finished, which is what stops a burst of concurrent calls overshooting.
|
|
630
|
+
|
|
631
|
+
**The limiter is shared per process**, keyed by the environment and a hash of
|
|
632
|
+
the credentials, so every client built for the same organization throttles as
|
|
633
|
+
one. Pass `share_rate_limiter: false` for a client with its own, or
|
|
634
|
+
`rate_limiter:` to supply one.
|
|
635
|
+
|
|
636
|
+
Retries, when the API does not answer:
|
|
637
|
+
|
|
638
|
+
| Response | What happens |
|
|
639
|
+
| --- | --- |
|
|
640
|
+
| 429 | waits the `Retry-After` it asked for, capped at 60s, plus 50ms, then tries again, for as long as the API keeps asking |
|
|
641
|
+
| 429 with no `Retry-After` | waits a second, plus the buffer |
|
|
642
|
+
| 500, 502, 503, 504 | retried `max_retries` times, 100ms doubling each time, or the `Retry-After` if it sent one |
|
|
643
|
+
| A connection that never arrived | retried the same way |
|
|
644
|
+
| A timeout | **not** retried, since the request may already have been acted on |
|
|
645
|
+
| Anything else | raised |
|
|
646
|
+
|
|
647
|
+
`Anvil::RateLimitError` is only raised if a 429 somehow reaches you, which the
|
|
648
|
+
throttling is there to prevent.
|
|
649
|
+
|
|
650
|
+
## Typed payloads and responses
|
|
651
|
+
|
|
652
|
+
Request payloads live under `Anvil::Payloads` and take snake_case names, which
|
|
653
|
+
they convert to the API's own spelling:
|
|
654
|
+
|
|
655
|
+
```ruby
|
|
656
|
+
Anvil::Payloads::CreateEtchPacket.new(is_test: true, webhook_url: "https://example.com/hook").to_h
|
|
657
|
+
# => { "isTest" => true, "webhookURL" => "https://example.com/hook" }
|
|
658
|
+
```
|
|
659
|
+
|
|
660
|
+
They are a convenience, not a requirement: every method also takes a plain Hash
|
|
661
|
+
with the API's camelCase keys. Anything a payload class does not declare is
|
|
662
|
+
camelized and passed through, so a new API field does not have to wait for a
|
|
663
|
+
gem release:
|
|
664
|
+
|
|
665
|
+
```ruby
|
|
666
|
+
Anvil::Payloads::FillPdf.new(data: {}, some_new_option: true).to_h
|
|
667
|
+
# => { "data" => {}, "someNewOption" => true }
|
|
668
|
+
```
|
|
669
|
+
|
|
670
|
+
The payloads the client's methods take: `FillPdf`, `GeneratePdf`,
|
|
671
|
+
`CreateEtchPacket`, `EtchFile`, `EtchSigner`, `SignerField`, `ForgeSubmit`.
|
|
672
|
+
Behind them, every REST request body and GraphQL input type in the API's specs
|
|
673
|
+
is generated too.
|
|
674
|
+
|
|
675
|
+
Responses come back under `Anvil::Types` with snake_case readers, and keep
|
|
676
|
+
everything the API sent:
|
|
677
|
+
|
|
678
|
+
```ruby
|
|
679
|
+
packet = client.get_etch_packet(eid: "AwpXqV3mnjeMgQb0VHKk")
|
|
680
|
+
|
|
681
|
+
packet.details_url # a declared field
|
|
682
|
+
packet.created_at # timestamps are parsed into Time
|
|
683
|
+
packet.document_group.signers # nested objects are typed too
|
|
684
|
+
packet["someFieldAddedYesterday"] # anything else, by its API name
|
|
685
|
+
packet.to_h # the response exactly as it arrived
|
|
686
|
+
```
|
|
687
|
+
|
|
688
|
+
Every type the API has is here, generated from its schema: `EtchPacket`,
|
|
689
|
+
`DocumentGroup`, `Signer`, `Submission`, `WeldData`, `User`, `Organization`
|
|
690
|
+
and some fifty more, plus `Anvil::Operations::<Name>Args` for the arguments of
|
|
691
|
+
every query and mutation. See [CONTRIBUTING.md](CONTRIBUTING.md) for how they
|
|
692
|
+
are generated and kept in step with production.
|
|
693
|
+
|
|
694
|
+
A custom `response_query` works with this: fields you did not select read as
|
|
695
|
+
`nil`, and fields the type does not declare are still in `to_h` and reachable
|
|
696
|
+
with `[]`.
|
|
697
|
+
|
|
698
|
+
## Development
|
|
699
|
+
|
|
700
|
+
```sh
|
|
701
|
+
bin/setup # install the dependencies
|
|
702
|
+
bundle exec rake # rubocop and rspec, what CI runs
|
|
703
|
+
bundle exec rspec # just the tests
|
|
704
|
+
bundle exec rubocop # just the linter
|
|
705
|
+
bin/console # an IRB session with the gem loaded
|
|
706
|
+
```
|
|
707
|
+
|
|
708
|
+
The tests mock HTTP with [WebMock](https://github.com/bblimke/webmock) and talk
|
|
709
|
+
to nothing. The suite is held above 95% line coverage by SimpleCov, which fails
|
|
710
|
+
the run if it drops below.
|
|
711
|
+
|
|
712
|
+
Runnable examples are in [`examples/`](examples). [CONTRIBUTING.md](CONTRIBUTING.md)
|
|
713
|
+
covers developing, the test client, the types, versioning and publishing.
|
|
714
|
+
|
|
715
|
+
## Integration test
|
|
716
|
+
|
|
717
|
+
`test-client.rb` runs every method against a real Anvil environment and prints
|
|
718
|
+
a pass or fail for each. It defaults to staging:
|
|
719
|
+
|
|
720
|
+
```sh
|
|
721
|
+
ANVIL_API_KEY=<your staging API key> ruby test-client.rb
|
|
722
|
+
```
|
|
723
|
+
|
|
724
|
+
```
|
|
725
|
+
Testing ruby-anvil 0.1.0 against https://staging.useanvil.com
|
|
726
|
+
|
|
727
|
+
PASS request_graphql (organization UESmQW9ZP6AKL9RJ7wlS)
|
|
728
|
+
PASS rate limiting (a burst past the limit surfaces no 429) (5 requests at 4 per 1000ms in 1.5s)
|
|
729
|
+
SKIP OAuth access token (Bearer auth): Set ANVIL_ACCESS_TOKEN to check Bearer auth
|
|
730
|
+
PASS get_current_user (lE0vobbpGM20qRNnkvd7, 1 organization(s))
|
|
731
|
+
PASS generate_pdf (markdown) (18023 bytes)
|
|
732
|
+
...
|
|
733
|
+
PASS cleanup (removes the packets this run created) (removed 4 packet(s))
|
|
734
|
+
PASS error handling (a rejected request raises a typed error) (ValidationError on HTTP 400)
|
|
735
|
+
|
|
736
|
+
22 passed, 0 failed, 1 skipped
|
|
737
|
+
```
|
|
738
|
+
|
|
739
|
+
It exits non-zero if anything failed, so it can be run in a pipeline. Run it
|
|
740
|
+
against a release before and after a change to see which behaviors changed.
|
|
741
|
+
|
|
742
|
+
Environment variables:
|
|
743
|
+
|
|
744
|
+
| Variable | Required | What it does |
|
|
745
|
+
| ----------------------- | -------- | -------------------------------------------------------------------------- |
|
|
746
|
+
| `ANVIL_API_KEY` | yes | an API key for the environment being tested |
|
|
747
|
+
| `ANVIL_BASE_URL` | no | the environment. Defaults to `https://staging.useanvil.com` |
|
|
748
|
+
| `ANVIL_ACCESS_TOKEN` | no | an OAuth token for the same environment, to check Bearer auth |
|
|
749
|
+
| `ANVIL_PDF_TEMPLATE_ID` | no | the PDF template to fill. Defaults to the first one with example data |
|
|
750
|
+
| `ANVIL_FORGE_EID` | no | the webform to submit to. Defaults to the first one in your organization |
|
|
751
|
+
|
|
752
|
+
Everything it creates is test data, and it removes all of it at the end: the
|
|
753
|
+
Etch packets are test packets whose signers are embedded, so no email is sent
|
|
754
|
+
to anyone, and the Workflow submission is a test submission. The PDF template
|
|
755
|
+
and the webform it uses are found in your organization rather than created, so
|
|
756
|
+
they survive the run.
|
|
757
|
+
|
|
758
|
+
`bundle exec rake integration` runs the same script.
|
|
759
|
+
|
|
760
|
+
## Publishing
|
|
761
|
+
|
|
762
|
+
Releases are cut from [package-ops](https://github.com/anvilco/package-ops),
|
|
763
|
+
not from this repo, the same way python-anvil and dotnet-anvil are: one
|
|
764
|
+
workflow there runs the lint and tests, bumps `Anvil::VERSION`, renames
|
|
765
|
+
`[Unreleased]` in the changelog to the new version, commits, tags `v<version>`,
|
|
766
|
+
creates the GitHub release with that section as its notes, and pushes the gem
|
|
767
|
+
to RubyGems with trusted publishing. This repo holds no publishing secret and
|
|
768
|
+
no publish workflow, and nothing publishes on merge.
|
|
769
|
+
|
|
770
|
+
To release:
|
|
771
|
+
|
|
772
|
+
1. Make sure the changes are noted under `## [Unreleased]` in
|
|
773
|
+
[CHANGELOG.md](CHANGELOG.md). A `- BREAKING:` bullet there forces the next
|
|
774
|
+
release to be a major.
|
|
775
|
+
2. In package-ops, Actions > Release > Run workflow: pick `anvilco/ruby-anvil`
|
|
776
|
+
and the increment, leaving `dry_run` on. Read what it plans to do.
|
|
777
|
+
3. Run it again with `dry_run` off.
|
|
778
|
+
|
|
779
|
+
The release config is
|
|
780
|
+
[`repos/ruby-anvil/release.js`](https://github.com/anvilco/package-ops/blob/main/repos/ruby-anvil/release.js),
|
|
781
|
+
and the notes next to it say what is specific to this repo.
|
|
782
|
+
|
|
783
|
+
### What the release needs, outside this repo
|
|
784
|
+
|
|
785
|
+
| Where | What |
|
|
786
|
+
| ----------------- | ---------------------------------------------------------------------------------------------------------------------- |
|
|
787
|
+
| anvilco org | The `anvil-package-release` GitHub App installed on this repository, and on the default branch ruleset's bypass list |
|
|
788
|
+
| RubyGems | A trusted publisher on the `ruby-anvil` gem naming `anvilco/package-ops`, workflow `release.yml`, environment `release` |
|
|
789
|
+
| package-ops | `RELEASE_APPROVERS` includes whoever runs it |
|
|
790
|
+
|
|
791
|
+
RubyGems takes a trusted publisher for a gem that does not exist yet, as a
|
|
792
|
+
pending publisher under the account's profile, so 0.1.0 goes out through
|
|
793
|
+
package-ops like every release after it. A pending publisher expires 12 hours
|
|
794
|
+
after it is created, and the account that creates it becomes the gem's owner, so
|
|
795
|
+
create it from the account registered to services@useanvil.com shortly before
|
|
796
|
+
the first release. That account is recorded in 1Password, as the npm, PyPI and
|
|
797
|
+
NuGet accounts are. The gemspec sets `rubygems_mfa_required`, so it needs MFA
|
|
798
|
+
enabled.
|
|
799
|
+
|
|
800
|
+
## License
|
|
801
|
+
|
|
802
|
+
MIT. See [LICENSE.txt](LICENSE.txt).
|