formfeed 0.3.32
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/LICENSE +21 -0
- data/README.md +194 -0
- data/lib/formfeed/client.rb +66 -0
- data/lib/formfeed/error.rb +21 -0
- data/lib/formfeed/internal/connection.rb +35 -0
- data/lib/formfeed/internal/download.rb +96 -0
- data/lib/formfeed/internal/fields.rb +73 -0
- data/lib/formfeed/internal/multipart.rb +62 -0
- data/lib/formfeed/internal/transport.rb +155 -0
- data/lib/formfeed/internal.rb +100 -0
- data/lib/formfeed/models/job.rb +34 -0
- data/lib/formfeed/models/library_file.rb +22 -0
- data/lib/formfeed/models/page.rb +49 -0
- data/lib/formfeed/models/pdf_info.rb +20 -0
- data/lib/formfeed/models/problem.rb +32 -0
- data/lib/formfeed/models/render.rb +48 -0
- data/lib/formfeed/models/render_input.rb +31 -0
- data/lib/formfeed/models/template.rb +26 -0
- data/lib/formfeed/models/usage.rb +26 -0
- data/lib/formfeed/models/webhook_endpoint.rb +25 -0
- data/lib/formfeed/resources/account.rb +24 -0
- data/lib/formfeed/resources/einvoices.rb +28 -0
- data/lib/formfeed/resources/files.rb +47 -0
- data/lib/formfeed/resources/jobs.rb +22 -0
- data/lib/formfeed/resources/pdf.rb +93 -0
- data/lib/formfeed/resources/renders.rb +86 -0
- data/lib/formfeed/resources/templates.rb +49 -0
- data/lib/formfeed/resources/webhooks.rb +51 -0
- data/lib/formfeed/upload.rb +75 -0
- data/lib/formfeed/version.rb +6 -0
- data/lib/formfeed/webhook.rb +63 -0
- data/lib/formfeed.rb +44 -0
- metadata +82 -0
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Formfeed
|
|
4
|
+
module Resources
|
|
5
|
+
# Jobs: a batch, or a render queued with its own webhook URL.
|
|
6
|
+
class Jobs
|
|
7
|
+
def initialize(transport)
|
|
8
|
+
@transport = transport
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def get(job)
|
|
12
|
+
Job.from_hash(@transport.object("GET", "/jobs/#{Internal::Ids.path(job)}"))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Polls until no render of the job is queued or running, at most +timeout+ seconds.
|
|
16
|
+
def wait_for(job, timeout: 600, interval: 2)
|
|
17
|
+
id = Internal::Ids.of(job)
|
|
18
|
+
Internal.poll(@transport, timeout, interval, "job #{id}") { get(id) }
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Formfeed
|
|
4
|
+
module Resources
|
|
5
|
+
# The PDF tools, on the output of an earlier render (a Render or its id), a PDF of the file
|
|
6
|
+
# library (a LibraryFile, its id or its name), or, for info, split, compress, to_image, redact and
|
|
7
|
+
# fill, a Formfeed::Upload, which is never stored. Options go as keywords or as one Hash. Every
|
|
8
|
+
# tool but info answers a new render, on the Starter plan and above: merge, protect, watermark,
|
|
9
|
+
# split, compress, redact and fill cost 0.5 units each on live keys, to_image one unit per
|
|
10
|
+
# picture; info is free.
|
|
11
|
+
class Pdf
|
|
12
|
+
def initialize(transport)
|
|
13
|
+
@transport = transport
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Joins PDFs in the order given; +filename:+, +expires_in:+, ...
|
|
17
|
+
def merge(sources, options = {}, idempotency_key: nil, **fields)
|
|
18
|
+
body = Internal.params(options, fields).merge("sources" => sources.map { |source| Internal::Ids.of(source) })
|
|
19
|
+
render("/pdf/merge", body, idempotency_key)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Encrypts a PDF with AES-256: +user_password:+, +owner_password:+, +permissions:+.
|
|
23
|
+
def protect(source, options = {}, idempotency_key: nil, **fields)
|
|
24
|
+
render("/pdf/protect", stored(source, Internal.params(options, fields)), idempotency_key)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Stamps a text or a library image on the pages.
|
|
28
|
+
def watermark(source, options = {}, idempotency_key: nil, **fields)
|
|
29
|
+
render("/pdf/watermark", stored(source, Internal.params(options, fields)), idempotency_key)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Page count, page sizes, encryption, metadata and the form with every field's name, type,
|
|
33
|
+
# value and options, which is how a form's names are found for +fill+. Free; creates no render.
|
|
34
|
+
def info(source)
|
|
35
|
+
PdfInfo.from_hash(@transport.object("POST", "/pdf/info", body(source, {})))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Cuts a PDF into parts, +every:+ pages each or one per entry of +ranges:+: a ZIP of the parts,
|
|
39
|
+
# or the PDF itself for one part.
|
|
40
|
+
def split(source, options = {}, idempotency_key: nil, **fields)
|
|
41
|
+
render("/pdf/split", body(source, Internal.params(options, fields)), idempotency_key)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Makes a PDF smaller; the render keeps the source when it cannot.
|
|
45
|
+
def compress(source, options = {}, idempotency_key: nil, **fields)
|
|
46
|
+
render("/pdf/compress", body(source, Internal.params(options, fields)), idempotency_key)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Draws pages as PNG or JPEG pictures (+format:+, +pages:+, +dpi:+, ...): one page answers the
|
|
50
|
+
# picture, several a ZIP of them.
|
|
51
|
+
def to_image(source, options = {}, idempotency_key: nil, **fields)
|
|
52
|
+
render("/pdf/to-image", body(source, Internal.params(options, fields)), idempotency_key)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Removes what lies under the rectangles (+regions:+) and every place a text stands (+text:+),
|
|
56
|
+
# from the file itself, and reads the result again: text still there fails the render with
|
|
57
|
+
# +redaction_incomplete+, which names regions and texts by number, never a text.
|
|
58
|
+
def redact(source, options = {}, idempotency_key: nil, **fields)
|
|
59
|
+
render("/pdf/redact", body(source, Internal.params(options, fields)), idempotency_key)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Fills a form's fields by their full names (+fields:+) and keeps it fillable, or with
|
|
63
|
+
# +flatten: true+ draws the values into the pages. The values are never stored.
|
|
64
|
+
def fill(source, options = {}, idempotency_key: nil, **fields)
|
|
65
|
+
render("/pdf/fill", body(source, Internal.params(options, fields)), idempotency_key)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Converts an office document to a PDF render (Starter plan and above): an Upload (Word, Excel,
|
|
69
|
+
# PowerPoint, OpenDocument, RTF or HTML, up to 20 MB, not kept), or the output of a Word or
|
|
70
|
+
# PowerPoint template render.
|
|
71
|
+
def convert(source, options = {}, idempotency_key: nil, **fields)
|
|
72
|
+
options = Internal.params(options, fields)
|
|
73
|
+
request = source.is_a?(Upload) ? Internal::Multipart.new(options, source) : stored(source, options)
|
|
74
|
+
render("/pdf/convert", request, idempotency_key)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
private
|
|
78
|
+
|
|
79
|
+
def render(path, body, idempotency_key)
|
|
80
|
+
Render.from_hash(@transport.object("POST", path, body, idempotency_key || Internal::Ids.key))
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# A tool's request: a stored source by its id as JSON, an upload as multipart beside the options.
|
|
84
|
+
def body(source, options)
|
|
85
|
+
source.is_a?(Upload) ? Internal::Multipart.new(options, source) : stored(source, options)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def stored(source, options)
|
|
89
|
+
options.merge("source" => Internal::Ids.of(source))
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Formfeed
|
|
4
|
+
module Resources
|
|
5
|
+
# Renders: create one, follow it, download its file, list them, render many as a batch.
|
|
6
|
+
class Renders
|
|
7
|
+
def initialize(transport)
|
|
8
|
+
@transport = transport
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Renders a template, HTML, a URL or Markdown: +template:+ with +data:+, or +html:+, +url:+ or
|
|
12
|
+
# +markdown:+, and +output:+, +settings:+, ... as keywords or as one Hash. Sync by default, so
|
|
13
|
+
# the answer is the finished render; +mode: "async"+ answers it queued. A retry of the same
|
|
14
|
+
# call sends the same Idempotency-Key, generated unless you pass one, so it cannot charge twice.
|
|
15
|
+
def create(request = {}, idempotency_key: nil, **fields)
|
|
16
|
+
Render.from_hash(@transport.object("POST", "/renders", Internal.params(request, fields),
|
|
17
|
+
idempotency_key || Internal::Ids.key))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def get(render)
|
|
21
|
+
Render.from_hash(@transport.object("GET", "/renders/#{Internal::Ids.path(render)}"))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# One page of renders, newest first: +template:+, +status:+, +environment:+, +since:+,
|
|
25
|
+
# +until:+, +limit:+ and +cursor:+.
|
|
26
|
+
def list(query = {}, **fields)
|
|
27
|
+
Page.from_hash(
|
|
28
|
+
@transport.object("GET", "/renders#{Internal::Query.string(Internal.params(query, fields))}"), Render
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Every render the query matches, page by page as it is iterated: an Enumerator, or with a block
|
|
33
|
+
# each render in turn.
|
|
34
|
+
def all(query = {}, **fields, &)
|
|
35
|
+
params = Internal.params(query, fields)
|
|
36
|
+
renders = Internal.pages { |cursor| list(params.merge("cursor" => cursor)) }
|
|
37
|
+
block_given? ? renders.each(&) : renders
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Removes the stored files of a render before they expire. Needs the +file:delete+ scope.
|
|
41
|
+
def delete_outputs(render)
|
|
42
|
+
@transport.json("DELETE", "/renders/#{Internal::Ids.path(render)}/outputs")
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# The stored request of a render, to render it again. Needs +render:input+; answers
|
|
47
|
+
# +render_input_expired+ (410) when the workspace keeps no requests or the period ended.
|
|
48
|
+
def input(render)
|
|
49
|
+
RenderInput.from_hash(@transport.object("GET", "/renders/#{Internal::Ids.path(render)}/input"))
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Queues many renders as one job: +items:+, and +template:+, +output:+, +zip:+, ... shared by
|
|
53
|
+
# them; its renders keep the order of +items+.
|
|
54
|
+
def batch(request = {}, idempotency_key: nil, **fields)
|
|
55
|
+
Job.from_hash(@transport.object("POST", "/renders/batch", Internal.params(request, fields),
|
|
56
|
+
idempotency_key || Internal::Ids.key))
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Polls until the render succeeded or failed (an async one), at most +timeout+ seconds.
|
|
60
|
+
def wait_for(render, timeout: 120, interval: 1)
|
|
61
|
+
id = Internal::Ids.of(render)
|
|
62
|
+
Internal.poll(@transport, timeout, interval, "render #{id}") { get(id) }
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# The output of a finished render (a Render or its id), as binary bytes.
|
|
66
|
+
def download(render)
|
|
67
|
+
@transport.download(download_url(render))
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Writes the output of a finished render to a path or an IO, streaming it; answers the bytes
|
|
71
|
+
# written.
|
|
72
|
+
def download_to(render, target)
|
|
73
|
+
@transport.download(download_url(render), target)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
private
|
|
77
|
+
|
|
78
|
+
def download_url(render)
|
|
79
|
+
target = render.is_a?(String) ? get(render) : render
|
|
80
|
+
return target.download_url unless target.download_url.nil?
|
|
81
|
+
|
|
82
|
+
raise Error.new("not_ready", "render #{target.id} has no output (#{target.status})")
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Formfeed
|
|
4
|
+
module Resources
|
|
5
|
+
# The workspace's templates, read and checked. Writing them (versions, publishing, channels) is
|
|
6
|
+
# the command line's and the app's; Formfeed::Client#request reaches those calls.
|
|
7
|
+
class Templates
|
|
8
|
+
def initialize(transport)
|
|
9
|
+
@transport = transport
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# One page of templates: +kind:+, +engine:+, +tag:+, +q:+, +limit:+, +cursor:+.
|
|
13
|
+
def list(query = {}, **fields)
|
|
14
|
+
Page.from_hash(
|
|
15
|
+
@transport.object("GET", "/templates#{Internal::Query.string(Internal.params(query, fields))}"), Template
|
|
16
|
+
)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Every template the query matches, page by page as it is iterated.
|
|
20
|
+
def all(query = {}, **fields, &)
|
|
21
|
+
params = Internal.params(query, fields)
|
|
22
|
+
templates = Internal.pages { |cursor| list(params.merge("cursor" => cursor)) }
|
|
23
|
+
block_given? ? templates.each(&) : templates
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# A template by its slug, +tpl_...+ id or UUID.
|
|
27
|
+
def get(id_or_slug)
|
|
28
|
+
Template.from_hash(@transport.object("GET", "/templates/#{Internal.escape(id_or_slug)}"))
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# The JSON Schema of the template's data, as a Hash: stored, else inferred from the sample data.
|
|
32
|
+
# Of the published (else the latest) version, or of +version+: +published+, +latest+, a number
|
|
33
|
+
# or a channel name.
|
|
34
|
+
def schema(id_or_slug, version: nil)
|
|
35
|
+
@transport.object(
|
|
36
|
+
"GET", "/templates/#{Internal.escape(id_or_slug)}/schema#{Internal::Query.string("version" => version)}"
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Checks a version with data without rendering: syntax, header and footer, missing partials,
|
|
41
|
+
# runtime errors and the data against the stored schema. Free. +version:+ and +data:+; without
|
|
42
|
+
# +data+, the version's sample data is checked. Answers +valid+ and the +errors+ and
|
|
43
|
+
# +warnings+ found.
|
|
44
|
+
def validate(id_or_slug, input = {}, **fields)
|
|
45
|
+
@transport.object("POST", "/templates/#{Internal.escape(id_or_slug)}/validate", Internal.params(input, fields))
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Formfeed
|
|
4
|
+
module Resources
|
|
5
|
+
# The workspace's webhook endpoints. Needs +webhook:manage+. Verify a delivery with
|
|
6
|
+
# Formfeed::Webhook.
|
|
7
|
+
class Webhooks
|
|
8
|
+
def initialize(transport)
|
|
9
|
+
@transport = transport
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Every endpoint, as Formfeed::WebhookEndpoint.
|
|
13
|
+
def list
|
|
14
|
+
Internal::Fields.objects(@transport.object("GET", "/webhooks"), "data").map do |endpoint|
|
|
15
|
+
WebhookEndpoint.from_hash(endpoint)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Registers an endpoint (+url:+, +events:+, +environments:+, +description:+); the answer
|
|
20
|
+
# carries its +secret+, once.
|
|
21
|
+
def create(input = {}, **fields)
|
|
22
|
+
WebhookEndpoint.from_hash(@transport.object("POST", "/webhooks", Internal.params(input, fields)))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Changes an endpoint: +url:+, +events:+, +environments:+, +description:+, +enabled:+.
|
|
26
|
+
def update(endpoint, patch = {}, **fields)
|
|
27
|
+
WebhookEndpoint.from_hash(
|
|
28
|
+
@transport.object("PUT", "/webhooks/#{Internal::Ids.path(endpoint)}", Internal.params(patch, fields))
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def delete(endpoint)
|
|
33
|
+
@transport.json("DELETE", "/webhooks/#{Internal::Ids.path(endpoint)}")
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Sends a test delivery to the endpoint; answers +queued+.
|
|
38
|
+
def test(endpoint)
|
|
39
|
+
@transport.object("POST", "/webhooks/#{Internal::Ids.path(endpoint)}/test")
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# Sends a stored event (+evt_...+) again as a new delivery to an endpoint; answers the delivery.
|
|
43
|
+
def resend(event_id, endpoint_id:)
|
|
44
|
+
@transport.object(
|
|
45
|
+
"POST", "/webhooks/events/#{Internal.escape(event_id)}/resend",
|
|
46
|
+
{ "endpoint_id" => Internal::Ids.of(endpoint_id) }
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Formfeed
|
|
4
|
+
# A file sent with a request: a PDF for the PDF tools, an office document to convert, an e-invoice
|
|
5
|
+
# to validate, a file for the library. It is read into memory and sent as multipart; the API keeps
|
|
6
|
+
# only what the library stores.
|
|
7
|
+
class Upload
|
|
8
|
+
TYPES = {
|
|
9
|
+
"pdf" => "application/pdf",
|
|
10
|
+
"xml" => "application/xml",
|
|
11
|
+
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
12
|
+
"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
13
|
+
"pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
14
|
+
"odt" => "application/vnd.oasis.opendocument.text",
|
|
15
|
+
"ods" => "application/vnd.oasis.opendocument.spreadsheet",
|
|
16
|
+
"odp" => "application/vnd.oasis.opendocument.presentation",
|
|
17
|
+
"rtf" => "application/rtf",
|
|
18
|
+
"html" => "text/html",
|
|
19
|
+
"htm" => "text/html",
|
|
20
|
+
"png" => "image/png",
|
|
21
|
+
"jpg" => "image/jpeg",
|
|
22
|
+
"jpeg" => "image/jpeg",
|
|
23
|
+
"webp" => "image/webp",
|
|
24
|
+
"gif" => "image/gif",
|
|
25
|
+
"svg" => "image/svg+xml"
|
|
26
|
+
}.freeze
|
|
27
|
+
|
|
28
|
+
attr_reader :contents, :name, :content_type
|
|
29
|
+
|
|
30
|
+
def initialize(contents, name, content_type = nil)
|
|
31
|
+
@contents = contents.b.freeze
|
|
32
|
+
@name = name.to_s.freeze
|
|
33
|
+
@content_type = (content_type || self.class.type_of(@name)).freeze
|
|
34
|
+
freeze
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# A file on disk; +name+ is its file name unless given.
|
|
38
|
+
def self.from_path(path, name: nil, content_type: nil)
|
|
39
|
+
raise ArgumentError, "cannot read #{path}" unless File.file?(path) && File.readable?(path)
|
|
40
|
+
|
|
41
|
+
new(File.binread(path), name || File.basename(path), content_type)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Bytes held in memory, under the name the API is to see.
|
|
45
|
+
def self.from_string(contents, name, content_type: nil)
|
|
46
|
+
new(contents, name, content_type)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# What an IO holds from where it stands: a File, a StringIO, or an upload a Rails or Rack
|
|
50
|
+
# application received, whose +original_filename+ and +content_type+ are taken where given.
|
|
51
|
+
def self.from_io(io, name: nil, content_type: nil)
|
|
52
|
+
name ||= name_of(io)
|
|
53
|
+
raise ArgumentError, "name is required for an IO without a file name" if name.nil? || name.to_s.empty?
|
|
54
|
+
|
|
55
|
+
content_type ||= io.content_type if io.respond_to?(:content_type)
|
|
56
|
+
new(io.read.to_s, name, content_type)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def self.name_of(io)
|
|
60
|
+
return io.original_filename if io.respond_to?(:original_filename)
|
|
61
|
+
|
|
62
|
+
File.basename(io.path) if io.respond_to?(:path) && io.path
|
|
63
|
+
end
|
|
64
|
+
private_class_method :name_of
|
|
65
|
+
|
|
66
|
+
# What the name says the file is; the API reads the format from the bytes either way.
|
|
67
|
+
def self.type_of(name)
|
|
68
|
+
TYPES.fetch(File.extname(name).delete_prefix(".").downcase, "application/octet-stream")
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def inspect
|
|
72
|
+
"#<#{self.class.name} #{@name} (#{@content_type}, #{@contents.bytesize} bytes)>"
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Formfeed
|
|
4
|
+
# Checks a webhook delivery: +Webhook-Signature: t=<unix>,v1=<hex>+, the HMAC-SHA256 of
|
|
5
|
+
# +<t>.<raw body>+ with the endpoint's secret, at most 300 seconds either way, compared in constant
|
|
6
|
+
# time. Pass the body exactly as it arrived: the signature covers the bytes on the wire.
|
|
7
|
+
#
|
|
8
|
+
# event = Formfeed::Webhook.parse(secret, request.headers["Webhook-Signature"], request.raw_post)
|
|
9
|
+
module Webhook
|
|
10
|
+
# A delivery whose signature does not match, or whose body is not a JSON object.
|
|
11
|
+
class Error < StandardError; end
|
|
12
|
+
|
|
13
|
+
TOLERANCE = 300
|
|
14
|
+
HEX = /\A[0-9a-f]{64}\z/
|
|
15
|
+
DIGITS = /\A[0-9]+\z/
|
|
16
|
+
|
|
17
|
+
module_function
|
|
18
|
+
|
|
19
|
+
# True when the header signs this body with this secret, within +tolerance+ seconds of +now+.
|
|
20
|
+
def verify(secret, signature_header, raw_body, tolerance: TOLERANCE, now: nil)
|
|
21
|
+
return false if blank?(signature_header) || blank?(secret)
|
|
22
|
+
|
|
23
|
+
parts = pairs(signature_header)
|
|
24
|
+
t = parts.fetch("t", "")
|
|
25
|
+
v1 = parts.fetch("v1", "").downcase
|
|
26
|
+
return false unless DIGITS.match?(t) && HEX.match?(v1)
|
|
27
|
+
|
|
28
|
+
time = t.to_i
|
|
29
|
+
return false if ((now || Time.now.to_i) - time).abs > tolerance
|
|
30
|
+
|
|
31
|
+
OpenSSL.fixed_length_secure_compare(OpenSSL::HMAC.hexdigest("SHA256", secret, "#{time}.#{raw_body}"), v1)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# The event of a verified delivery, with its +id+, +type+, +created_at+, +workspace_id+ and +data+;
|
|
35
|
+
# raises Formfeed::Webhook::Error when the signature does not match.
|
|
36
|
+
def parse(secret, signature_header, raw_body, tolerance: TOLERANCE, now: nil)
|
|
37
|
+
raise Error, "invalid webhook signature" unless verify(secret, signature_header, raw_body,
|
|
38
|
+
tolerance: tolerance, now: now)
|
|
39
|
+
|
|
40
|
+
event = begin
|
|
41
|
+
JSON.parse(raw_body.to_s)
|
|
42
|
+
rescue JSON::ParserError, EncodingError
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
raise Error, "the webhook body is not a JSON object" unless event.is_a?(Hash)
|
|
46
|
+
|
|
47
|
+
event
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def pairs(header)
|
|
51
|
+
header.split(",").to_h do |pair|
|
|
52
|
+
key, value = pair.strip.split("=", 2)
|
|
53
|
+
[key.to_s.strip, value.to_s.strip]
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
private_class_method :pairs
|
|
57
|
+
|
|
58
|
+
def blank?(text)
|
|
59
|
+
text.nil? || text.empty?
|
|
60
|
+
end
|
|
61
|
+
private_class_method :blank?
|
|
62
|
+
end
|
|
63
|
+
end
|
data/lib/formfeed.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Ruby's standard library only: these are default gems in every Ruby the SDK supports.
|
|
4
|
+
require "json"
|
|
5
|
+
require "net/http"
|
|
6
|
+
require "openssl"
|
|
7
|
+
require "securerandom"
|
|
8
|
+
require "uri"
|
|
9
|
+
require "zlib"
|
|
10
|
+
|
|
11
|
+
require_relative "formfeed/version"
|
|
12
|
+
require_relative "formfeed/error"
|
|
13
|
+
require_relative "formfeed/internal"
|
|
14
|
+
require_relative "formfeed/internal/fields"
|
|
15
|
+
require_relative "formfeed/internal/multipart"
|
|
16
|
+
require_relative "formfeed/internal/connection"
|
|
17
|
+
require_relative "formfeed/internal/download"
|
|
18
|
+
require_relative "formfeed/internal/transport"
|
|
19
|
+
require_relative "formfeed/upload"
|
|
20
|
+
require_relative "formfeed/webhook"
|
|
21
|
+
require_relative "formfeed/models/problem"
|
|
22
|
+
require_relative "formfeed/models/render"
|
|
23
|
+
require_relative "formfeed/models/job"
|
|
24
|
+
require_relative "formfeed/models/page"
|
|
25
|
+
require_relative "formfeed/models/render_input"
|
|
26
|
+
require_relative "formfeed/models/library_file"
|
|
27
|
+
require_relative "formfeed/models/template"
|
|
28
|
+
require_relative "formfeed/models/usage"
|
|
29
|
+
require_relative "formfeed/models/pdf_info"
|
|
30
|
+
require_relative "formfeed/models/webhook_endpoint"
|
|
31
|
+
require_relative "formfeed/resources/renders"
|
|
32
|
+
require_relative "formfeed/resources/jobs"
|
|
33
|
+
require_relative "formfeed/resources/pdf"
|
|
34
|
+
require_relative "formfeed/resources/einvoices"
|
|
35
|
+
require_relative "formfeed/resources/files"
|
|
36
|
+
require_relative "formfeed/resources/templates"
|
|
37
|
+
require_relative "formfeed/resources/webhooks"
|
|
38
|
+
require_relative "formfeed/resources/account"
|
|
39
|
+
require_relative "formfeed/client"
|
|
40
|
+
|
|
41
|
+
# The Formfeed API: PDFs and images from templates, HTML, URLs or Markdown, and tools for existing
|
|
42
|
+
# PDFs. Start with Formfeed::Client; check webhook deliveries with Formfeed::Webhook.
|
|
43
|
+
module Formfeed
|
|
44
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: formfeed
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.3.32
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Formfeed
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-26 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Renders PDFs and images from templates, HTML, URLs or Markdown through
|
|
14
|
+
the Formfeed API, and runs its tools on existing PDFs (merge, protect, watermark,
|
|
15
|
+
split, compress, redact, fill, convert), with retries, idempotency keys and webhook
|
|
16
|
+
verification. Ruby's standard library only.
|
|
17
|
+
email:
|
|
18
|
+
- support@formfeed.dev
|
|
19
|
+
executables: []
|
|
20
|
+
extensions: []
|
|
21
|
+
extra_rdoc_files: []
|
|
22
|
+
files:
|
|
23
|
+
- LICENSE
|
|
24
|
+
- README.md
|
|
25
|
+
- lib/formfeed.rb
|
|
26
|
+
- lib/formfeed/client.rb
|
|
27
|
+
- lib/formfeed/error.rb
|
|
28
|
+
- lib/formfeed/internal.rb
|
|
29
|
+
- lib/formfeed/internal/connection.rb
|
|
30
|
+
- lib/formfeed/internal/download.rb
|
|
31
|
+
- lib/formfeed/internal/fields.rb
|
|
32
|
+
- lib/formfeed/internal/multipart.rb
|
|
33
|
+
- lib/formfeed/internal/transport.rb
|
|
34
|
+
- lib/formfeed/models/job.rb
|
|
35
|
+
- lib/formfeed/models/library_file.rb
|
|
36
|
+
- lib/formfeed/models/page.rb
|
|
37
|
+
- lib/formfeed/models/pdf_info.rb
|
|
38
|
+
- lib/formfeed/models/problem.rb
|
|
39
|
+
- lib/formfeed/models/render.rb
|
|
40
|
+
- lib/formfeed/models/render_input.rb
|
|
41
|
+
- lib/formfeed/models/template.rb
|
|
42
|
+
- lib/formfeed/models/usage.rb
|
|
43
|
+
- lib/formfeed/models/webhook_endpoint.rb
|
|
44
|
+
- lib/formfeed/resources/account.rb
|
|
45
|
+
- lib/formfeed/resources/einvoices.rb
|
|
46
|
+
- lib/formfeed/resources/files.rb
|
|
47
|
+
- lib/formfeed/resources/jobs.rb
|
|
48
|
+
- lib/formfeed/resources/pdf.rb
|
|
49
|
+
- lib/formfeed/resources/renders.rb
|
|
50
|
+
- lib/formfeed/resources/templates.rb
|
|
51
|
+
- lib/formfeed/resources/webhooks.rb
|
|
52
|
+
- lib/formfeed/upload.rb
|
|
53
|
+
- lib/formfeed/version.rb
|
|
54
|
+
- lib/formfeed/webhook.rb
|
|
55
|
+
homepage: https://formfeed.dev
|
|
56
|
+
licenses:
|
|
57
|
+
- MIT
|
|
58
|
+
metadata:
|
|
59
|
+
source_code_uri: https://github.com/formfeed-dev/formfeed
|
|
60
|
+
documentation_uri: https://docs.formfeed.dev
|
|
61
|
+
rubygems_mfa_required: 'true'
|
|
62
|
+
post_install_message:
|
|
63
|
+
rdoc_options: []
|
|
64
|
+
require_paths:
|
|
65
|
+
- lib
|
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
67
|
+
requirements:
|
|
68
|
+
- - ">="
|
|
69
|
+
- !ruby/object:Gem::Version
|
|
70
|
+
version: '3.3'
|
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
requirements: []
|
|
77
|
+
rubygems_version: 3.5.22
|
|
78
|
+
signing_key:
|
|
79
|
+
specification_version: 4
|
|
80
|
+
summary: 'Ruby client for the Formfeed API: PDFs and images from templates, HTML or
|
|
81
|
+
URLs.'
|
|
82
|
+
test_files: []
|