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
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
module Payloads
|
|
5
|
+
# A file in an Etch packet: either an existing PDF template (`cast_eid`) or
|
|
6
|
+
# a PDF you upload with the packet (`file`). Every field is generated from
|
|
7
|
+
# the schema in generated/operations.rb.
|
|
8
|
+
#
|
|
9
|
+
# Upload a new PDF by passing {Anvil.prepare_graphql_file} as `file`:
|
|
10
|
+
#
|
|
11
|
+
# Anvil::Payloads::EtchFile.new(
|
|
12
|
+
# id: "contract",
|
|
13
|
+
# title: "Contract",
|
|
14
|
+
# file: Anvil.prepare_graphql_file("./contract.pdf"),
|
|
15
|
+
# fields: [{ id: "signature", type: "signature", pageNum: 1, rect: { ... } }]
|
|
16
|
+
# )
|
|
17
|
+
class EtchFile
|
|
18
|
+
# Reopened to carry the documentation above
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
module Payloads
|
|
5
|
+
# One field a signer signs or fills, on one of the packet's files. The API
|
|
6
|
+
# takes signer fields as JSON, so this is the one payload with no schema
|
|
7
|
+
# type behind it.
|
|
8
|
+
class SignerField < Base
|
|
9
|
+
# @return [String] the `id` of the {EtchFile} this field is on
|
|
10
|
+
attribute :file_id
|
|
11
|
+
# @return [String] the field's id on that PDF template
|
|
12
|
+
attribute :field_id
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# A signer on a {CreateEtchPacket}. Every field is generated from the
|
|
16
|
+
# OpenAPI spec in generated/rest.rb. Signers sign in the order they appear
|
|
17
|
+
# in the packet's `signers`, unless `routing_order` says otherwise.
|
|
18
|
+
class EtchSigner
|
|
19
|
+
# Reopened to carry the documentation above
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
module Payloads
|
|
5
|
+
# The body of a {Anvil::Client#fill_pdf} call. Every field is generated from
|
|
6
|
+
# the OpenAPI spec into {FillPdfRequest}; this is the name the client's API
|
|
7
|
+
# uses for it.
|
|
8
|
+
#
|
|
9
|
+
# @see https://www.useanvil.com/docs/api/fill-pdf
|
|
10
|
+
class FillPdf < FillPdfRequest
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
module Payloads
|
|
5
|
+
# The variables of a {Anvil::Client#forge_submit} call: a submission to one
|
|
6
|
+
# webform (a Forge) of a Workflow. Every argument is generated from the
|
|
7
|
+
# schema into {Anvil::Operations::ForgeSubmitArgs}.
|
|
8
|
+
#
|
|
9
|
+
# Leave `weld_data_eid` out to start a new submission; pass the one you got
|
|
10
|
+
# back to add to an existing submission.
|
|
11
|
+
#
|
|
12
|
+
# @see https://www.useanvil.com/docs/api/workflows
|
|
13
|
+
class ForgeSubmit < Anvil::Operations::ForgeSubmitArgs
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
module Payloads
|
|
5
|
+
# The body of a {Anvil::Client#generate_pdf} call.
|
|
6
|
+
#
|
|
7
|
+
# `type` picks the input: "markdown" (the API's default) takes an Array of
|
|
8
|
+
# content Hashes in `data`, while "html" takes a Hash of `html` and `css`.
|
|
9
|
+
#
|
|
10
|
+
# @see https://www.useanvil.com/docs/api/generate-pdf
|
|
11
|
+
class GeneratePdf < Base
|
|
12
|
+
# @return [String] the title encoded into the generated PDF
|
|
13
|
+
attribute :title
|
|
14
|
+
# @return [String] "markdown" or "html"
|
|
15
|
+
attribute :type
|
|
16
|
+
# @return [Array<Hash>, Hash] the content to render
|
|
17
|
+
attribute :data
|
|
18
|
+
# @return [Hash] page size, margins and orientation
|
|
19
|
+
attribute :page
|
|
20
|
+
# @return [Hash] a logo to place on the page
|
|
21
|
+
attribute :logo
|
|
22
|
+
# @return [String] the filename to return the PDF under
|
|
23
|
+
attribute :filename
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
require "monitor"
|
|
5
|
+
|
|
6
|
+
module Anvil
|
|
7
|
+
# Keeps requests inside the API's rate limit, and retries the ones it still
|
|
8
|
+
# refuses.
|
|
9
|
+
#
|
|
10
|
+
# A request takes a slot before it is sent and gives it back when its response
|
|
11
|
+
# arrives, so the requests in flight count against the limit alongside the ones
|
|
12
|
+
# that have already finished. A finished request keeps counting for one
|
|
13
|
+
# interval, which is what makes this a sliding window rather than a bucket that
|
|
14
|
+
# refills on a fixed tick.
|
|
15
|
+
#
|
|
16
|
+
# The limiter is shared per process, keyed by the environment and the
|
|
17
|
+
# credentials, so several clients built against the same organization throttle
|
|
18
|
+
# as one. See {.shared}.
|
|
19
|
+
#
|
|
20
|
+
# The algorithm is python-anvil's `rate_limit.py`, which the SDK conventions
|
|
21
|
+
# name as the reference for transport.
|
|
22
|
+
class RateLimiter
|
|
23
|
+
include MonitorMixin
|
|
24
|
+
|
|
25
|
+
# Added to the window and to every retry delay, so the API's clock and this
|
|
26
|
+
# one ticking apart cannot push a request over the line.
|
|
27
|
+
RETRY_BUFFER_MS = 50
|
|
28
|
+
|
|
29
|
+
# What to wait when a 429 names no delay. Retrying after only the buffer
|
|
30
|
+
# would hammer a server that is already refusing.
|
|
31
|
+
DEFAULT_RETRY_AFTER_MS = 1000
|
|
32
|
+
|
|
33
|
+
# However long a 429 asks for, wait no longer than this.
|
|
34
|
+
MAX_RETRY_AFTER_MS = 60_000
|
|
35
|
+
|
|
36
|
+
# @return [Integer] requests allowed per interval
|
|
37
|
+
attr_reader :limit
|
|
38
|
+
# @return [Integer] the interval, in milliseconds
|
|
39
|
+
attr_reader :interval_ms
|
|
40
|
+
|
|
41
|
+
# @param limit [Integer] requests allowed per interval
|
|
42
|
+
# @param interval_ms [Integer] the interval, in milliseconds
|
|
43
|
+
def initialize(limit:, interval_ms:)
|
|
44
|
+
super()
|
|
45
|
+
@limit = positive_int(limit) || 1
|
|
46
|
+
@interval_ms = positive_int(interval_ms) || 1000
|
|
47
|
+
@in_flight = 0
|
|
48
|
+
@finished_at = []
|
|
49
|
+
@has_set_from_response = false
|
|
50
|
+
@setting_in_progress = false
|
|
51
|
+
@gate_open = false
|
|
52
|
+
@turn = new_cond
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# The limiter every client with these credentials shares.
|
|
56
|
+
#
|
|
57
|
+
# @param base_url [String]
|
|
58
|
+
# @param auth_header [String]
|
|
59
|
+
# @param limit [Integer]
|
|
60
|
+
# @param interval_ms [Integer]
|
|
61
|
+
# @return [RateLimiter]
|
|
62
|
+
def self.shared(base_url:, auth_header:, limit: 1, interval_ms: 1000)
|
|
63
|
+
key = [base_url, Digest::SHA256.hexdigest(auth_header.to_s)]
|
|
64
|
+
registry_lock.synchronize do
|
|
65
|
+
registry[key] ||= new(limit: limit, interval_ms: interval_ms)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @api private
|
|
70
|
+
def self.registry
|
|
71
|
+
@registry ||= {}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# @api private
|
|
75
|
+
def self.registry_lock
|
|
76
|
+
@registry_lock ||= Mutex.new
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# How long to wait before retrying a 429: the seconds it asked for, capped,
|
|
80
|
+
# plus the buffer. A missing or unreadable value means one second.
|
|
81
|
+
#
|
|
82
|
+
# @param retry_after [String, Numeric, nil] the Retry-After header
|
|
83
|
+
# @return [Float] seconds
|
|
84
|
+
def self.retry_delay_seconds(retry_after)
|
|
85
|
+
asked = begin
|
|
86
|
+
seconds = Float(retry_after).abs
|
|
87
|
+
seconds.finite? ? seconds : DEFAULT_RETRY_AFTER_MS / 1000.0
|
|
88
|
+
rescue ArgumentError, TypeError
|
|
89
|
+
DEFAULT_RETRY_AFTER_MS / 1000.0
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
[asked, MAX_RETRY_AFTER_MS / 1000.0].min + (RETRY_BUFFER_MS / 1000.0)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Replaces the limit, keeping the requests already counted.
|
|
96
|
+
#
|
|
97
|
+
# @return [Boolean] whether anything changed
|
|
98
|
+
# rubocop:disable-next Naming/PredicateMethod -- it sets the limits and reports whether they moved
|
|
99
|
+
def set_limits(limit, interval_ms)
|
|
100
|
+
limit = positive_int(limit)
|
|
101
|
+
interval_ms = positive_int(interval_ms)
|
|
102
|
+
return false unless limit && interval_ms
|
|
103
|
+
return false if limit == @limit && interval_ms == @interval_ms
|
|
104
|
+
|
|
105
|
+
synchronize do
|
|
106
|
+
@limit = limit
|
|
107
|
+
@interval_ms = interval_ms
|
|
108
|
+
@turn.broadcast
|
|
109
|
+
end
|
|
110
|
+
true
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Adopts the organization's real limit from a response, once.
|
|
114
|
+
#
|
|
115
|
+
# @param headers [Hash{String => String}] keys downcased
|
|
116
|
+
# @return [void]
|
|
117
|
+
def update_from_headers(headers)
|
|
118
|
+
return if @has_set_from_response
|
|
119
|
+
|
|
120
|
+
set_limits(headers["x-ratelimit-limit"], headers["x-ratelimit-interval-ms"])
|
|
121
|
+
synchronize do
|
|
122
|
+
@has_set_from_response = true
|
|
123
|
+
release_setup_gate
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Runs the block when the limit allows, retrying it after any 429.
|
|
128
|
+
#
|
|
129
|
+
# The block is called again for each retry, so it has to be able to build
|
|
130
|
+
# its request again.
|
|
131
|
+
#
|
|
132
|
+
# @yieldreturn [Object]
|
|
133
|
+
# @return [Object] whatever the block returned
|
|
134
|
+
def run
|
|
135
|
+
loop do
|
|
136
|
+
acquired = false
|
|
137
|
+
begin
|
|
138
|
+
wait_for_turn
|
|
139
|
+
acquired = true
|
|
140
|
+
result = yield
|
|
141
|
+
rescue RateLimitError => e
|
|
142
|
+
finish if acquired
|
|
143
|
+
release_setup_gate
|
|
144
|
+
sleep(self.class.retry_delay_seconds(e.retry_after))
|
|
145
|
+
next
|
|
146
|
+
rescue Exception # rubocop:disable Lint/RescueException -- the request may have reached the API, so the slot has to be given back whatever happened
|
|
147
|
+
finish if acquired
|
|
148
|
+
release_setup_gate
|
|
149
|
+
raise
|
|
150
|
+
end
|
|
151
|
+
finish
|
|
152
|
+
# The first response is in, with or without limit headers: let the requests
|
|
153
|
+
# held behind it go, or a response missing them would hold them forever
|
|
154
|
+
release_setup_gate
|
|
155
|
+
return result
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# @return [Integer] requests counting against the limit right now
|
|
160
|
+
def in_use
|
|
161
|
+
synchronize do
|
|
162
|
+
prune(monotonic_now)
|
|
163
|
+
@in_flight + @finished_at.length
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
private
|
|
168
|
+
|
|
169
|
+
# Waits until another request would stay within `limit` requests in any
|
|
170
|
+
# `interval_ms`: the ones out now, plus those whose response arrived inside
|
|
171
|
+
# the last interval.
|
|
172
|
+
def wait_for_turn
|
|
173
|
+
synchronize do
|
|
174
|
+
# Until the first response has arrived, only the request whose response
|
|
175
|
+
# it will be may go
|
|
176
|
+
@turn.wait_while { @setting_in_progress && !@gate_open }
|
|
177
|
+
@setting_in_progress = true unless @gate_open
|
|
178
|
+
|
|
179
|
+
loop do
|
|
180
|
+
now = monotonic_now
|
|
181
|
+
window_start = prune(now)
|
|
182
|
+
|
|
183
|
+
if @in_flight + @finished_at.length < @limit
|
|
184
|
+
@in_flight += 1
|
|
185
|
+
return
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
if @finished_at.empty?
|
|
189
|
+
# Every slot is a request still out; a response will wake us
|
|
190
|
+
@turn.wait
|
|
191
|
+
else
|
|
192
|
+
@turn.wait(@finished_at.first - window_start)
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# A response arrived, or the request failed: it counts for another interval.
|
|
199
|
+
def finish
|
|
200
|
+
synchronize do
|
|
201
|
+
@in_flight -= 1
|
|
202
|
+
@finished_at << monotonic_now
|
|
203
|
+
@turn.broadcast
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
# Lets the requests waiting on the first response go ahead.
|
|
208
|
+
def release_setup_gate
|
|
209
|
+
synchronize do
|
|
210
|
+
@gate_open = true
|
|
211
|
+
@setting_in_progress = false
|
|
212
|
+
@turn.broadcast
|
|
213
|
+
end
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
# Drops the responses that have left the window. The buffer allows for the
|
|
217
|
+
# API's clock and this one ticking apart.
|
|
218
|
+
def prune(now)
|
|
219
|
+
window_start = now - ((@interval_ms + RETRY_BUFFER_MS) / 1000.0)
|
|
220
|
+
@finished_at.shift while @finished_at.first && @finished_at.first <= window_start
|
|
221
|
+
window_start
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Not the wall clock, so a change to it cannot hold requests up.
|
|
225
|
+
def monotonic_now
|
|
226
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def positive_int(value)
|
|
230
|
+
number = Integer(value)
|
|
231
|
+
number.positive? ? number : nil
|
|
232
|
+
rescue ArgumentError, TypeError
|
|
233
|
+
nil
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
# What the escape hatches {Client#request_rest} and {Client#request_graphql}
|
|
5
|
+
# return. The methods with a name of their own return the useful part
|
|
6
|
+
# directly instead: the PDF's bytes, the typed packet, the sign URL.
|
|
7
|
+
class Response
|
|
8
|
+
# @return [Integer] the HTTP status code
|
|
9
|
+
attr_reader :status_code
|
|
10
|
+
# @return [Hash{String => String}] the response headers, keys downcased
|
|
11
|
+
attr_reader :headers
|
|
12
|
+
|
|
13
|
+
def initialize(status_code:, headers: {})
|
|
14
|
+
@status_code = status_code
|
|
15
|
+
@headers = headers
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# @return [Boolean]
|
|
19
|
+
def success?
|
|
20
|
+
status_code.between?(200, 299)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# @return [String, nil]
|
|
24
|
+
def content_type
|
|
25
|
+
headers["content-type"]
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# A response from a REST endpoint.
|
|
30
|
+
class RestResponse < Response
|
|
31
|
+
# The body, in the shape asked for by `data_type`:
|
|
32
|
+
#
|
|
33
|
+
# - `:string` (the default) a binary String
|
|
34
|
+
# - `:io` a StringIO holding the whole body
|
|
35
|
+
# - `:json` the parsed JSON
|
|
36
|
+
# - `:stream` the number of bytes handed to your block or written to `to:`
|
|
37
|
+
#
|
|
38
|
+
# @return [String, StringIO, Hash, Array, Integer, nil]
|
|
39
|
+
attr_reader :data
|
|
40
|
+
|
|
41
|
+
def initialize(status_code:, headers: {}, data: nil)
|
|
42
|
+
super(status_code: status_code, headers: headers)
|
|
43
|
+
@data = data
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @return [Integer, nil] the body's length in bytes, when it is known
|
|
47
|
+
def content_length
|
|
48
|
+
length = headers["content-length"]
|
|
49
|
+
length&.to_i
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# A response from the GraphQL endpoint.
|
|
54
|
+
#
|
|
55
|
+
# A GraphQL request that fails raises {GraphQLError} rather than returning a
|
|
56
|
+
# response with an `errors` array, so anything you get back here succeeded.
|
|
57
|
+
class GraphQLResponse < Response
|
|
58
|
+
# @return [Hash] the whole response body, `{"data" => {...}}`
|
|
59
|
+
attr_reader :body
|
|
60
|
+
|
|
61
|
+
def initialize(status_code:, headers: {}, body: nil)
|
|
62
|
+
super(status_code: status_code, headers: headers)
|
|
63
|
+
@body = body.is_a?(Hash) ? body : {}
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# The `data` map of the GraphQL response: a key per operation in the query.
|
|
67
|
+
#
|
|
68
|
+
# @return [Hash]
|
|
69
|
+
def data
|
|
70
|
+
value = body["data"]
|
|
71
|
+
value.is_a?(Hash) ? value : {}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The result of one operation in the query.
|
|
75
|
+
#
|
|
76
|
+
# response["createEtchPacket"]
|
|
77
|
+
#
|
|
78
|
+
# @param name [String, Symbol] the operation's name in the response
|
|
79
|
+
# @return [Object, nil]
|
|
80
|
+
def [](name)
|
|
81
|
+
data[name.to_s]
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "time"
|
|
4
|
+
|
|
5
|
+
module Anvil
|
|
6
|
+
module Types
|
|
7
|
+
# The attribute DSL shared by the response types in {Anvil::Types} and the
|
|
8
|
+
# request payloads in {Anvil::Payloads}.
|
|
9
|
+
#
|
|
10
|
+
# It exists to map between Ruby's snake_case and the API's camelCase (and
|
|
11
|
+
# its handful of names like `webhookURL` that no rule produces), and to
|
|
12
|
+
# build nested objects out of nested JSON.
|
|
13
|
+
#
|
|
14
|
+
# @api private
|
|
15
|
+
module Attributes
|
|
16
|
+
def self.included(base)
|
|
17
|
+
base.extend(ClassMethods)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @api private
|
|
21
|
+
module ClassMethods
|
|
22
|
+
# @return [Hash{Symbol => Hash}] every attribute, including inherited ones
|
|
23
|
+
def attributes
|
|
24
|
+
@attributes ||= (superclass.respond_to?(:attributes) ? superclass.attributes.dup : {})
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Declares an attribute.
|
|
28
|
+
#
|
|
29
|
+
# @param name [Symbol] the Ruby name, in snake_case
|
|
30
|
+
# @param key [String] the API's name for it. Defaults to `name` camelized
|
|
31
|
+
# @param type [Class, Symbol, nil] `:time` to parse a timestamp, or a
|
|
32
|
+
# {Types::Base} subclass to build from a nested object
|
|
33
|
+
# @param array_of [Class, nil] a {Types::Base} subclass to build each
|
|
34
|
+
# element of an array from
|
|
35
|
+
# @return [void]
|
|
36
|
+
def attribute(name, key: nil, type: nil, array_of: nil)
|
|
37
|
+
attributes[name.to_sym] = {
|
|
38
|
+
name: name.to_sym,
|
|
39
|
+
key: key || camelize(name),
|
|
40
|
+
type: type,
|
|
41
|
+
array_of: array_of
|
|
42
|
+
}
|
|
43
|
+
attr_accessor name
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# "signer_type" => "signerType"
|
|
47
|
+
#
|
|
48
|
+
# @api private
|
|
49
|
+
def camelize(name)
|
|
50
|
+
head, *rest = name.to_s.split("_")
|
|
51
|
+
([head] + rest.map(&:capitalize)).join
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# @api private
|
|
55
|
+
def cast(value, definition)
|
|
56
|
+
return nil if value.nil?
|
|
57
|
+
|
|
58
|
+
if definition[:array_of]
|
|
59
|
+
return value unless value.is_a?(Array)
|
|
60
|
+
|
|
61
|
+
return value.map { |element| definition[:array_of].from_h(element) }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
case definition[:type]
|
|
65
|
+
when nil then value
|
|
66
|
+
when :time then parse_time(value)
|
|
67
|
+
else definition[:type].from_h(value)
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# @api private
|
|
72
|
+
def parse_time(value)
|
|
73
|
+
return value if value.is_a?(Time)
|
|
74
|
+
|
|
75
|
+
Time.iso8601(value.to_s)
|
|
76
|
+
rescue ArgumentError
|
|
77
|
+
value
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
# An object the API returned.
|
|
83
|
+
#
|
|
84
|
+
# Every declared field has a reader. Anything the API sent that this client
|
|
85
|
+
# does not know about is still there in {#raw}, so a new API field is
|
|
86
|
+
# reachable without a client release, and a custom `response_query` that
|
|
87
|
+
# selects fields this class does not declare still returns all of them.
|
|
88
|
+
class Base
|
|
89
|
+
include Attributes
|
|
90
|
+
|
|
91
|
+
# @return [Hash] the object exactly as the API returned it
|
|
92
|
+
attr_reader :raw
|
|
93
|
+
|
|
94
|
+
# @param raw [Hash]
|
|
95
|
+
def initialize(raw = {})
|
|
96
|
+
@raw = raw.is_a?(Hash) ? raw : {}
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# @param hash [Hash, nil]
|
|
100
|
+
# @return [Base, nil] nil when there was nothing to build from
|
|
101
|
+
def self.from_h(hash)
|
|
102
|
+
return nil if hash.nil?
|
|
103
|
+
return hash if hash.is_a?(self)
|
|
104
|
+
|
|
105
|
+
instance = new(hash)
|
|
106
|
+
return instance unless hash.is_a?(Hash)
|
|
107
|
+
|
|
108
|
+
attributes.each_value do |definition|
|
|
109
|
+
value = hash.key?(definition[:key]) ? hash[definition[:key]] : hash[definition[:name]]
|
|
110
|
+
instance.public_send(:"#{definition[:name]}=", cast(value, definition))
|
|
111
|
+
end
|
|
112
|
+
instance
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Reads a field by its API name, including fields this class does not
|
|
116
|
+
# declare.
|
|
117
|
+
#
|
|
118
|
+
# @param key [String, Symbol]
|
|
119
|
+
# @return [Object, nil]
|
|
120
|
+
def [](key)
|
|
121
|
+
raw[key.to_s].nil? ? raw[key.to_sym] : raw[key.to_s]
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# @return [Hash] the object as the API returned it
|
|
125
|
+
def to_h
|
|
126
|
+
raw
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def ==(other)
|
|
130
|
+
other.is_a?(self.class) && other.raw == raw
|
|
131
|
+
end
|
|
132
|
+
alias eql? ==
|
|
133
|
+
|
|
134
|
+
def hash
|
|
135
|
+
[self.class, raw].hash
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def inspect
|
|
139
|
+
shown = self.class.attributes.each_key.filter_map do |name|
|
|
140
|
+
value = public_send(name)
|
|
141
|
+
"#{name}=#{value.inspect}" unless value.nil?
|
|
142
|
+
end
|
|
143
|
+
"#<#{self.class.name} #{shown.join(" ")}>"
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Anvil
|
|
4
|
+
module Types
|
|
5
|
+
# Every field is generated from the schema in generated/graphql.rb; this
|
|
6
|
+
# adds what the API's shape does not say outright.
|
|
7
|
+
class EtchPacket
|
|
8
|
+
# @return [Array<Signer>] the packet's signers, or an empty array when
|
|
9
|
+
# the response query did not select them
|
|
10
|
+
def signers
|
|
11
|
+
document_group&.signers || []
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|