mizanpage 1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +49 -0
- data/lib/mizanpage.rb +364 -0
- metadata +50 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 69fd2c6d5e6c40f941075b543fbe0315e059a941af02e6dc2d26367b8c06712f
|
|
4
|
+
data.tar.gz: c8e2730d77346d38cca07af2315746df413f034251171160aab9da6314770166
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f8b58b10236c84fe70bb25b0435c09123b4ad514fa804c6217385986c2515111b916b0e6d17f93503d43bf45faf8b496a4594bdd592ccbc2cb08bffb2a9e1c77
|
|
7
|
+
data.tar.gz: 41eb772afed50cf304f213baa05f649ef762691377cb6bd7f72c0c8350dc51051d84d443867b5c60a5654d3d9b69e146308cbd90d57bfb2aa329a446a425ab12
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Laudelout Studio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# mizanpage
|
|
2
|
+
|
|
3
|
+
Client officiel de l'API [Mizanpage](https://mizanpage.com) pour Ruby 3.0+, sans dépendance.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
gem install mizanpage
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
require "mizanpage"
|
|
11
|
+
|
|
12
|
+
mizanpage = Mizanpage::Client.new(ENV.fetch("MIZANPAGE_KEY"))
|
|
13
|
+
|
|
14
|
+
# Le PDF dans la même réponse
|
|
15
|
+
mizanpage.render(
|
|
16
|
+
{ template: "facture", data: { values: { "client.nom" => "Brasserie Lumen SRL", "facture.total" => "7 788,00 €" } } },
|
|
17
|
+
idempotency_key: "F-2026-0148"
|
|
18
|
+
).save("facture.pdf")
|
|
19
|
+
|
|
20
|
+
# Sans attendre (POST /v1/renders) : demander, attendre, télécharger
|
|
21
|
+
job, pdf = mizanpage.renders.create_and_wait({ template: "facture", reference: "C-2041", data: { values: values } })
|
|
22
|
+
|
|
23
|
+
# Un webhook reçu (Rails : request.raw_post et request.headers)
|
|
24
|
+
event = Mizanpage.verify_webhook(request.raw_post, request.headers.to_h, ENV.fetch("MIZANPAGE_WEBHOOK_SECRET"))
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
| Méthode | Route |
|
|
28
|
+
| --- | --- |
|
|
29
|
+
| `render(params, idempotency_key:)` | `POST /v1/render` |
|
|
30
|
+
| `renders.create / get / wait / download / create_and_wait` | `POST /v1/renders`, `GET /v1/renders/{id}`, `GET /v1/renders/{id}/pdf` |
|
|
31
|
+
| `templates.list(kind:) / get(ref) / fields(ref)` | `GET /v1/templates`, `GET /v1/templates/{ref}/fields` |
|
|
32
|
+
| `catalogs.list / get / put` | `/v1/catalogs` |
|
|
33
|
+
| `webhooks.list / create / update / delete / test` | `/v1/webhooks` |
|
|
34
|
+
| `pdf.from_html(html, **options) / merge(pdfs) / split(pdf, pages: \| ranges: \| every:)` | `POST /v1/pdf/html`, `/v1/pdf/merge`, `/v1/pdf/split` |
|
|
35
|
+
| `me` | `GET /v1/me` |
|
|
36
|
+
|
|
37
|
+
Options du client : `base_url:`, `organisation:` (installation de partenaire), `user: { id:, name: }`, `timeout:` (secondes), `transport:`.
|
|
38
|
+
|
|
39
|
+
Une erreur de l'API lève une `Mizanpage::Error` avec `status`, `code` et `details`. Documentation : https://mizanpage.com/docs/developpeurs. Référence : https://api.mizanpage.com/docs.
|
|
40
|
+
|
|
41
|
+
À utiliser côté serveur : une clé d'API ne va jamais dans un navigateur.
|
|
42
|
+
|
|
43
|
+
## Développer
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
ruby -Ilib -Itest test/client_test.rb
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Licence MIT.
|
data/lib/mizanpage.rb
ADDED
|
@@ -0,0 +1,364 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "openssl"
|
|
6
|
+
require "uri"
|
|
7
|
+
|
|
8
|
+
# Official client for the Mizanpage API.
|
|
9
|
+
#
|
|
10
|
+
# - +render+: the PDF in the same response (POST /v1/render).
|
|
11
|
+
# - +renders+: asynchronous rendering (POST /v1/renders), then waiting,
|
|
12
|
+
# polling and downloading.
|
|
13
|
+
# - +pdf+: the PDF tools without a template (HTML to PDF, merge, split).
|
|
14
|
+
# - +templates+, +catalogs+, +webhooks+, +me+: the rest of the integration.
|
|
15
|
+
# - Mizanpage.verify_webhook: check the signature of a webhook you received.
|
|
16
|
+
#
|
|
17
|
+
# No dependency beyond the Ruby standard library. Server side only: an API
|
|
18
|
+
# key never goes into a browser.
|
|
19
|
+
module Mizanpage
|
|
20
|
+
VERSION = "1.0.0"
|
|
21
|
+
DEFAULT_BASE_URL = "https://api.mizanpage.com"
|
|
22
|
+
|
|
23
|
+
# An API error: +status+, a stable +code+, a readable message and +details+.
|
|
24
|
+
class Error < StandardError
|
|
25
|
+
attr_reader :status, :code, :details
|
|
26
|
+
|
|
27
|
+
def initialize(status, code, message, details = {})
|
|
28
|
+
super(message)
|
|
29
|
+
@status = status
|
|
30
|
+
@code = code
|
|
31
|
+
@details = details
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# The webhook signature does not match, or its timestamp is too old.
|
|
36
|
+
class WebhookVerificationError < StandardError; end
|
|
37
|
+
|
|
38
|
+
# What a transport returns: status, headers (lower-case names) and raw body.
|
|
39
|
+
Response = Struct.new(:status, :headers, :body)
|
|
40
|
+
|
|
41
|
+
# A PDF from POST /v1/render and what its headers say about it.
|
|
42
|
+
RenderedPdf = Struct.new(:pdf, :generation_id, :template_id, :template_version, :document_id, :test, keyword_init: true) do
|
|
43
|
+
def save(path)
|
|
44
|
+
File.binwrite(path, pdf)
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# The default transport, on Net::HTTP.
|
|
49
|
+
# Any object answering call(method, url, headers, body, timeout) can replace it.
|
|
50
|
+
NET_HTTP_TRANSPORT = lambda do |method, url, headers, body, timeout|
|
|
51
|
+
uri = URI(url)
|
|
52
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
53
|
+
http.use_ssl = uri.scheme == "https"
|
|
54
|
+
http.open_timeout = timeout
|
|
55
|
+
http.read_timeout = timeout
|
|
56
|
+
request = Net::HTTPGenericRequest.new(method, !body.nil?, true, uri.request_uri, headers)
|
|
57
|
+
request.body = body unless body.nil?
|
|
58
|
+
answer = http.request(request)
|
|
59
|
+
Response.new(answer.code.to_i, answer.each_header.to_h { |k, v| [k.downcase, v] }, answer.body.to_s.b)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# The client.
|
|
63
|
+
#
|
|
64
|
+
# mizanpage = Mizanpage::Client.new(ENV.fetch("MIZANPAGE_KEY"))
|
|
65
|
+
#
|
|
66
|
+
# +organisation+: for a partner installation, the organisation reference.
|
|
67
|
+
# +user+: { id:, name: } of the person in your software the call is made for.
|
|
68
|
+
# +timeout+: seconds per call (60 by default).
|
|
69
|
+
class Client
|
|
70
|
+
attr_reader :renders, :templates, :catalogs, :webhooks, :pdf
|
|
71
|
+
|
|
72
|
+
def initialize(api_key, base_url: DEFAULT_BASE_URL, organisation: nil, user: nil, timeout: 60, transport: NET_HTTP_TRANSPORT)
|
|
73
|
+
raise ArgumentError, "Mizanpage: api_key is required." if !api_key.is_a?(String) || api_key.empty?
|
|
74
|
+
|
|
75
|
+
@api_key = api_key
|
|
76
|
+
@base_url = base_url.sub(%r{/+\z}, "")
|
|
77
|
+
@organisation = organisation
|
|
78
|
+
@user = user
|
|
79
|
+
@timeout = timeout
|
|
80
|
+
@transport = transport
|
|
81
|
+
@renders = Renders.new(self)
|
|
82
|
+
@templates = Templates.new(self)
|
|
83
|
+
@catalogs = Catalogs.new(self)
|
|
84
|
+
@webhooks = Webhooks.new(self)
|
|
85
|
+
@pdf = PdfTools.new(self)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# The PDF in the same response (POST /v1/render).
|
|
89
|
+
def render(params, idempotency_key: nil)
|
|
90
|
+
response = request("POST", "/v1/render", stringify(params).merge("format" => "pdf"), idempotency_key: idempotency_key)
|
|
91
|
+
match = /\A([^;]+);version=(\d+)\z/.match(response.headers["x-mizanpage-template"].to_s)
|
|
92
|
+
RenderedPdf.new(
|
|
93
|
+
pdf: response.body,
|
|
94
|
+
generation_id: response.headers["x-mizanpage-generation"],
|
|
95
|
+
template_id: match && match[1],
|
|
96
|
+
template_version: match && match[2].to_i,
|
|
97
|
+
document_id: response.headers["x-mizanpage-document"],
|
|
98
|
+
test: response.headers["x-mizanpage-test"] == "1"
|
|
99
|
+
)
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The workspace, the person acting and what the studio offers (GET /v1/me).
|
|
103
|
+
def me
|
|
104
|
+
json("GET", "/v1/me")
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# @api private
|
|
108
|
+
def json(method, path, body = nil, idempotency_key: nil, query: nil)
|
|
109
|
+
response = request(method, path, body, idempotency_key: idempotency_key, query: query)
|
|
110
|
+
return nil if response.status == 204 || response.body.empty?
|
|
111
|
+
|
|
112
|
+
JSON.parse(response.body.force_encoding("UTF-8"))
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# @api private
|
|
116
|
+
def request(method, path, body = nil, idempotency_key: nil, query: nil)
|
|
117
|
+
headers = {
|
|
118
|
+
"Authorization" => "Bearer #{@api_key}",
|
|
119
|
+
"Accept" => "application/json, application/pdf",
|
|
120
|
+
"User-Agent" => "mizanpage-ruby/#{VERSION}"
|
|
121
|
+
}
|
|
122
|
+
payload = nil
|
|
123
|
+
unless body.nil?
|
|
124
|
+
headers["Content-Type"] = "application/json"
|
|
125
|
+
payload = JSON.generate(body)
|
|
126
|
+
end
|
|
127
|
+
headers["Idempotency-Key"] = idempotency_key if idempotency_key
|
|
128
|
+
headers["X-Mizanpage-Organisation"] = @organisation if @organisation
|
|
129
|
+
if @user
|
|
130
|
+
headers["X-Mizanpage-User"] = @user[:id].to_s
|
|
131
|
+
headers["X-Mizanpage-User-Name"] = @user[:name].to_s if @user[:name]
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
url = @base_url + path
|
|
135
|
+
params = (query || {}).compact
|
|
136
|
+
url += "?#{URI.encode_www_form(params)}" unless params.empty?
|
|
137
|
+
|
|
138
|
+
response = @transport.call(method, url, headers, payload, @timeout)
|
|
139
|
+
raise Mizanpage.error_of(response) if response.status >= 400
|
|
140
|
+
|
|
141
|
+
response
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
private
|
|
145
|
+
|
|
146
|
+
def stringify(hash)
|
|
147
|
+
hash.to_h.transform_keys(&:to_s)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# @api private
|
|
152
|
+
def self.escape(value)
|
|
153
|
+
URI.encode_www_form_component(value.to_s).gsub("+", "%20")
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
# Asynchronous rendering.
|
|
157
|
+
class Renders
|
|
158
|
+
def initialize(client)
|
|
159
|
+
@client = client
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Ask for a PDF without waiting (POST /v1/renders): the answer says queued.
|
|
163
|
+
def create(params, idempotency_key: nil)
|
|
164
|
+
@client.json("POST", "/v1/renders", params.to_h.transform_keys(&:to_s).merge("format" => "pdf"), idempotency_key: idempotency_key)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Where the render stands (GET /v1/renders/{id}).
|
|
168
|
+
def get(id)
|
|
169
|
+
@client.json("GET", "/v1/renders/#{Mizanpage.escape(id)}")
|
|
170
|
+
end
|
|
171
|
+
|
|
172
|
+
# Wait for the render to finish. Returns the succeeded render; raises
|
|
173
|
+
# Mizanpage::Error if it failed or the delay ran out.
|
|
174
|
+
def wait(id, timeout: 120, interval: 2, sleeper: ->(seconds) { sleep(seconds) })
|
|
175
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
176
|
+
interval = [1, interval].max
|
|
177
|
+
loop do
|
|
178
|
+
job = get(id)
|
|
179
|
+
return job if job["status"] == "succeeded"
|
|
180
|
+
|
|
181
|
+
if job["status"] == "failed"
|
|
182
|
+
error = job["error"] || {}
|
|
183
|
+
raise Error.new(422, error["code"] || "render_failed", error["message"] || "The render failed.", { "render_id" => job["id"] })
|
|
184
|
+
end
|
|
185
|
+
if Process.clock_gettime(Process::CLOCK_MONOTONIC) + interval > deadline
|
|
186
|
+
raise Error.new(408, "render_timeout", "Render #{job["id"]} did not finish in time.", { "render_id" => job["id"] })
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
sleeper.call(interval)
|
|
190
|
+
end
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# The PDF of a succeeded render, during the hour it is kept.
|
|
194
|
+
def download(id)
|
|
195
|
+
@client.request("GET", "/v1/renders/#{Mizanpage.escape(id)}/pdf").body
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
# Ask, wait and download: [render, pdf].
|
|
199
|
+
def create_and_wait(params, idempotency_key: nil, timeout: 120, interval: 2, sleeper: ->(seconds) { sleep(seconds) })
|
|
200
|
+
created = create(params, idempotency_key: idempotency_key)
|
|
201
|
+
job = wait(created["id"], timeout: timeout, interval: interval, sleeper: sleeper)
|
|
202
|
+
[job, download(job["id"])]
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
# The PDF tools without a template.
|
|
207
|
+
class PdfTools
|
|
208
|
+
def initialize(client)
|
|
209
|
+
@client = client
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
# A complete HTML page to PDF (POST /v1/pdf/html), counted as a generated PDF.
|
|
213
|
+
# Options: paper ("A4" by default, or { width:, height: } in mm), landscape,
|
|
214
|
+
# margins (mm), css_page_size, print_background. CSS, images and fonts
|
|
215
|
+
# must be inside the page (data:).
|
|
216
|
+
def from_html(html, idempotency_key: nil, **options)
|
|
217
|
+
response = @client.request("POST", "/v1/pdf/html", options.transform_keys(&:to_s).merge("html" => html), idempotency_key: idempotency_key)
|
|
218
|
+
RenderedPdf.new(
|
|
219
|
+
pdf: response.body,
|
|
220
|
+
generation_id: response.headers["x-mizanpage-generation"],
|
|
221
|
+
test: response.headers["x-mizanpage-test"] == "1"
|
|
222
|
+
)
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
# Join 2 to 20 PDFs into one, in order (POST /v1/pdf/merge). Free.
|
|
226
|
+
def merge(pdfs)
|
|
227
|
+
@client.request("POST", "/v1/pdf/merge", { "pdfs" => pdfs.map { |pdf| [pdf].pack("m0") } }).body
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
# Extract pages (POST /v1/pdf/split). Free. Give one option: pages ("1-3,5")
|
|
231
|
+
# returns a PDF; ranges (["1-2", "3-"]) or every return a ZIP of PDFs.
|
|
232
|
+
def split(pdf, pages: nil, ranges: nil, every: nil)
|
|
233
|
+
body = { "pdf" => [pdf].pack("m0"), "pages" => pages, "ranges" => ranges, "every" => every }.compact
|
|
234
|
+
@client.request("POST", "/v1/pdf/split", body).body
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
# Templates of the workspace.
|
|
239
|
+
class Templates
|
|
240
|
+
def initialize(client)
|
|
241
|
+
@client = client
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# The workspace templates, optionally of one kind.
|
|
245
|
+
def list(kind: nil)
|
|
246
|
+
@client.json("GET", "/v1/templates", query: { "kind" => kind })["data"]
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# A template, by id or external_id.
|
|
250
|
+
def get(ref)
|
|
251
|
+
@client.json("GET", "/v1/templates/#{Mizanpage.escape(ref)}")
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
# The fields to send for this template.
|
|
255
|
+
def fields(ref)
|
|
256
|
+
@client.json("GET", "/v1/templates/#{Mizanpage.escape(ref)}/fields")
|
|
257
|
+
end
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# Field catalogs, one per document kind.
|
|
261
|
+
class Catalogs
|
|
262
|
+
def initialize(client)
|
|
263
|
+
@client = client
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
def list
|
|
267
|
+
@client.json("GET", "/v1/catalogs")["data"]
|
|
268
|
+
end
|
|
269
|
+
|
|
270
|
+
def get(kind)
|
|
271
|
+
@client.json("GET", "/v1/catalogs/#{Mizanpage.escape(kind)}")
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
# Create or replace the catalog of a document kind.
|
|
275
|
+
def put(kind, catalog)
|
|
276
|
+
@client.json("PUT", "/v1/catalogs/#{Mizanpage.escape(kind)}", catalog)
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
# Webhook endpoints.
|
|
281
|
+
class Webhooks
|
|
282
|
+
def initialize(client)
|
|
283
|
+
@client = client
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
def list
|
|
287
|
+
@client.json("GET", "/v1/webhooks")["data"]
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
# Declare an endpoint. The secret is only shown here: keep it.
|
|
291
|
+
def create(url:, events: nil, description: nil)
|
|
292
|
+
@client.json("POST", "/v1/webhooks", { "url" => url, "events" => events, "description" => description }.compact)
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# Change url, events, description or enabled.
|
|
296
|
+
def update(id, **changes)
|
|
297
|
+
@client.json("PATCH", "/v1/webhooks/#{Mizanpage.escape(id)}", changes.transform_keys(&:to_s))
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
def delete(id)
|
|
301
|
+
@client.request("DELETE", "/v1/webhooks/#{Mizanpage.escape(id)}")
|
|
302
|
+
nil
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Send a webhook.test event and say whether it was received.
|
|
306
|
+
def test(id)
|
|
307
|
+
@client.json("POST", "/v1/webhooks/#{Mizanpage.escape(id)}/test")
|
|
308
|
+
end
|
|
309
|
+
end
|
|
310
|
+
|
|
311
|
+
# Check a received webhook (Standard Webhooks) and return its event.
|
|
312
|
+
#
|
|
313
|
+
# +payload+ is the raw body, as received. +headers+ holds webhook-id,
|
|
314
|
+
# webhook-timestamp and webhook-signature (any case). A timestamp older
|
|
315
|
+
# than +tolerance_seconds+ is refused.
|
|
316
|
+
def self.verify_webhook(payload, headers, secret, tolerance_seconds: 300)
|
|
317
|
+
normalised = {}
|
|
318
|
+
headers.each do |name, value|
|
|
319
|
+
value = value.first if value.is_a?(Array)
|
|
320
|
+
normalised[name.to_s.downcase.tr("_", "-").sub(/\Ahttp-/, "")] = value.to_s
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
id = normalised["webhook-id"].to_s
|
|
324
|
+
timestamp = normalised["webhook-timestamp"].to_s
|
|
325
|
+
signatures = normalised["webhook-signature"].to_s
|
|
326
|
+
raise WebhookVerificationError, "Missing webhook-id, webhook-timestamp or webhook-signature header." if id.empty? || timestamp.empty? || signatures.empty?
|
|
327
|
+
raise WebhookVerificationError, "Timestamp missing or too old." if !timestamp.match?(/\A\d+\z/) || (Time.now.to_i - timestamp.to_i).abs > tolerance_seconds
|
|
328
|
+
|
|
329
|
+
begin
|
|
330
|
+
key = secret.delete_prefix("whsec_").unpack1("m0")
|
|
331
|
+
rescue ArgumentError
|
|
332
|
+
raise WebhookVerificationError, "The secret is not valid."
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
expected = [OpenSSL::HMAC.digest("SHA256", key, "#{id}.#{timestamp}.#{payload}")].pack("m0")
|
|
336
|
+
valid = signatures.split(" ").any? do |candidate|
|
|
337
|
+
version, signature = candidate.split(",", 2)
|
|
338
|
+
version == "v1" && !signature.nil? && signature.bytesize == expected.bytesize &&
|
|
339
|
+
OpenSSL.fixed_length_secure_compare(signature, expected)
|
|
340
|
+
end
|
|
341
|
+
raise WebhookVerificationError, "Invalid signature." unless valid
|
|
342
|
+
|
|
343
|
+
event = JSON.parse(payload)
|
|
344
|
+
raise WebhookVerificationError, "The body is not a JSON object." unless event.is_a?(Hash)
|
|
345
|
+
|
|
346
|
+
event
|
|
347
|
+
end
|
|
348
|
+
|
|
349
|
+
# @api private
|
|
350
|
+
def self.error_of(response)
|
|
351
|
+
error = begin
|
|
352
|
+
JSON.parse(response.body.to_s)["error"]
|
|
353
|
+
rescue JSON::ParserError, TypeError, NoMethodError
|
|
354
|
+
nil
|
|
355
|
+
end
|
|
356
|
+
error = {} unless error.is_a?(Hash)
|
|
357
|
+
Error.new(
|
|
358
|
+
response.status,
|
|
359
|
+
error["code"].is_a?(String) ? error["code"] : "http_#{response.status}",
|
|
360
|
+
error["message"].is_a?(String) ? error["message"] : "HTTP #{response.status}",
|
|
361
|
+
error["details"].is_a?(Hash) ? error["details"] : {}
|
|
362
|
+
)
|
|
363
|
+
end
|
|
364
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mizanpage
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Laudelout Studio
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-27 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Générer des PDF depuis vos modèles Mizanpage, en direct ou en file d'attente,
|
|
14
|
+
et vérifier les webhooks. Sans dépendance.
|
|
15
|
+
email:
|
|
16
|
+
- contact@mizanpage.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- LICENSE
|
|
22
|
+
- README.md
|
|
23
|
+
- lib/mizanpage.rb
|
|
24
|
+
homepage: https://mizanpage.com
|
|
25
|
+
licenses:
|
|
26
|
+
- MIT
|
|
27
|
+
metadata:
|
|
28
|
+
homepage_uri: https://mizanpage.com
|
|
29
|
+
documentation_uri: https://mizanpage.com/docs
|
|
30
|
+
rubygems_mfa_required: 'true'
|
|
31
|
+
post_install_message:
|
|
32
|
+
rdoc_options: []
|
|
33
|
+
require_paths:
|
|
34
|
+
- lib
|
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '3.0'
|
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - ">="
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '0'
|
|
45
|
+
requirements: []
|
|
46
|
+
rubygems_version: 3.5.22
|
|
47
|
+
signing_key:
|
|
48
|
+
specification_version: 4
|
|
49
|
+
summary: Client officiel de l'API Mizanpage.
|
|
50
|
+
test_files: []
|