delivrie 0.1.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 +72 -0
- data/lib/delivrie/client.rb +106 -0
- data/lib/delivrie/error.rb +26 -0
- data/lib/delivrie/resources.rb +631 -0
- data/lib/delivrie/version.rb +5 -0
- data/lib/delivrie/webhook.rb +48 -0
- data/lib/delivrie.rb +13 -0
- metadata +56 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0f14080ab1c540ecd942bf1a38a622944759c4f7dfa436dafdb2622d2af2580f
|
|
4
|
+
data.tar.gz: b88b32c904a142c511b6ac7e4aa8be0d3ffdd8b72725c43680d46123ea2e5070
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 292763ed16cd4c589c84127be27b0ff6c0f4bcdb089efde1a7bcd53fad4e9fd66a120f31809cefd1c751086137d44e0f7f97167cd0df57da8787a998485a6b8a
|
|
7
|
+
data.tar.gz: 0e816be055bce691636f75710454a432aa2e810cb7a71998287d464f42f8ce5e6372b2449f68d2996cef766bde8e0dfc990027bfd2a0601d9781849404192fda
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 South Circle
|
|
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,72 @@
|
|
|
1
|
+
# Delivrie for Ruby
|
|
2
|
+
|
|
3
|
+
The official Ruby client for [Delivrie](https://delivrie.io): transactional email, broadcasts, templates, contacts, automations, the Inbox and AI writing. No runtime dependencies, Ruby 3.1 and later.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
gem install delivrie
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
require "delivrie"
|
|
11
|
+
|
|
12
|
+
delivrie = Delivrie::Client.new(ENV["DELIVRIE_API_KEY"])
|
|
13
|
+
|
|
14
|
+
email = delivrie.emails.send_email(
|
|
15
|
+
from: "Acme <orders@mail.acme.com>",
|
|
16
|
+
to: "ada@example.com",
|
|
17
|
+
subject: "Your order is confirmed",
|
|
18
|
+
html: "<p>Thanks for your order!</p>",
|
|
19
|
+
)
|
|
20
|
+
puts email["id"]
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Methods return the API's JSON as a Hash with string keys, and raise `Delivrie::Error` (with `status`, `name` and `message`) when a request fails.
|
|
24
|
+
|
|
25
|
+
## More examples
|
|
26
|
+
|
|
27
|
+
```ruby
|
|
28
|
+
# Send a published template, safe to retry
|
|
29
|
+
delivrie.emails.send_email({ from: "Acme <hi@acme.com>", to: "ada@example.com", template: { id: "welcome", variables: { first_name: "Ada" } } }, idempotency_key: "signup-42")
|
|
30
|
+
|
|
31
|
+
# Attach a local file (read and sent as base64) or a public URL (fetched by Delivrie)
|
|
32
|
+
delivrie.emails.send_email(from: "...", to: "...", subject: "Invoice", text: "Attached.", attachments: [{ path: "./invoice.pdf" }, { path: "https://acme.com/terms.pdf" }])
|
|
33
|
+
|
|
34
|
+
# Contacts, segments and broadcasts
|
|
35
|
+
segment = delivrie.segments.create(name: "Beta users")
|
|
36
|
+
delivrie.segments.add_contacts(segment["id"], contacts: ["ada@example.com"])
|
|
37
|
+
broadcast = delivrie.broadcasts.create(name: "Launch", from: "Acme <news@acme.com>", subject: "We're live", segment_id: segment["id"], html: "<p>Hello {{first_name}}</p>")
|
|
38
|
+
delivrie.broadcasts.send_broadcast(broadcast["id"], scheduled_at: "tomorrow at 9am")
|
|
39
|
+
|
|
40
|
+
# The Inbox
|
|
41
|
+
delivrie.inbox.threads(needs_reply: true, q: "label:billing").fetch("data").each do |t|
|
|
42
|
+
delivrie.inbox.create_draft(thread_id: t["id"], text: "Thanks, we're looking into it.")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# AI writing in your brand voice, straight into a template
|
|
46
|
+
delivrie.ai.draft(url: "https://acme.com/blog/launch", target: { type: "template", id: "launch-email" })
|
|
47
|
+
|
|
48
|
+
# Anything else
|
|
49
|
+
delivrie.request(:get, "/emails", query: { limit: 5 })
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Webhooks
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
post "/webhooks/delivrie" do
|
|
56
|
+
event = Delivrie::Webhook.verify(request.body.read, request.env, ENV["DELIVRIE_WEBHOOK_SECRET"])
|
|
57
|
+
# event["type"], event["data"]
|
|
58
|
+
200
|
|
59
|
+
rescue Delivrie::WebhookVerificationError
|
|
60
|
+
400
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Options
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
Delivrie::Client.new(key, base_url: "https://delivrie.io/api", timeout: 30, max_retries: 2)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Rate limits (429), server errors and network errors are retried with backoff for GET and DELETE, and for sends only when you pass an `idempotency_key`, so a retry never sends twice.
|
|
71
|
+
|
|
72
|
+
Docs: [delivrie.io/docs/ruby](https://delivrie.io/docs/ruby) · API reference: [delivrie.io/docs/api](https://delivrie.io/docs/api)
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Delivrie
|
|
8
|
+
# The Delivrie client.
|
|
9
|
+
#
|
|
10
|
+
# delivrie = Delivrie::Client.new(ENV["DELIVRIE_API_KEY"])
|
|
11
|
+
# email = delivrie.emails.send_email(from: "Acme <hi@acme.com>", to: "ada@example.com", subject: "Hello", html: "<p>It works</p>")
|
|
12
|
+
# email["id"]
|
|
13
|
+
#
|
|
14
|
+
# Every method returns the API's JSON as a Hash (string keys) or raises Delivrie::Error. Rate limits, server errors
|
|
15
|
+
# and network errors are retried for safe requests, and for sends only with an idempotency key, so a retry never
|
|
16
|
+
# sends an email twice.
|
|
17
|
+
class Client
|
|
18
|
+
DEFAULT_BASE_URL = "https://delivrie.io/api"
|
|
19
|
+
|
|
20
|
+
attr_reader :base_url
|
|
21
|
+
|
|
22
|
+
# api_key falls back to ENV["DELIVRIE_API_KEY"]
|
|
23
|
+
# base_url falls back to ENV["DELIVRIE_BASE_URL"], then https://delivrie.io/api
|
|
24
|
+
# transport replaces the HTTP call (for tests): ->(method, url, headers, body, timeout) { [status, headers, body] }
|
|
25
|
+
def initialize(api_key = nil, base_url: nil, timeout: 30, max_retries: 2, headers: {}, transport: nil)
|
|
26
|
+
@api_key = api_key || ENV["DELIVRIE_API_KEY"]
|
|
27
|
+
if @api_key.nil? || @api_key.empty?
|
|
28
|
+
raise ArgumentError, "Missing API key. Pass it to Delivrie::Client.new(key) or set DELIVRIE_API_KEY. Create one at https://delivrie.io/api-keys"
|
|
29
|
+
end
|
|
30
|
+
@base_url = (base_url || ENV["DELIVRIE_BASE_URL"] || DEFAULT_BASE_URL).sub(%r{/+\z}, "")
|
|
31
|
+
@timeout = timeout
|
|
32
|
+
@max_retries = [max_retries.to_i, 0].max
|
|
33
|
+
@headers = headers
|
|
34
|
+
@transport = transport
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Calls any API path (relative to the base URL, like "/emails") and returns the decoded JSON.
|
|
38
|
+
def request(method, path, body = nil, query: nil, idempotency_key: nil)
|
|
39
|
+
method = method.to_s.upcase
|
|
40
|
+
url = @base_url + path
|
|
41
|
+
q = (query || {}).reject { |_k, v| v.nil? || v == "" }
|
|
42
|
+
url += (url.include?("?") ? "&" : "?") + URI.encode_www_form(q.transform_values { |v| v == true || v == false ? v.to_s : v }) unless q.empty?
|
|
43
|
+
headers = { "Authorization" => "Bearer #{@api_key}", "Accept" => "application/json", "User-Agent" => "delivrie-ruby/#{VERSION}" }.merge(@headers)
|
|
44
|
+
payload = nil
|
|
45
|
+
unless body.nil?
|
|
46
|
+
payload = JSON.generate(body)
|
|
47
|
+
headers["Content-Type"] = "application/json"
|
|
48
|
+
end
|
|
49
|
+
headers["Idempotency-Key"] = idempotency_key.to_s if idempotency_key
|
|
50
|
+
# GET and DELETE are safe to repeat; POST and PATCH only with an idempotency key.
|
|
51
|
+
retryable = %w[GET DELETE].include?(method) || !idempotency_key.nil?
|
|
52
|
+
|
|
53
|
+
attempt = 0
|
|
54
|
+
loop do
|
|
55
|
+
begin
|
|
56
|
+
status, res_headers, raw = perform(method, url, headers, payload)
|
|
57
|
+
rescue Error => e
|
|
58
|
+
raise unless retryable && attempt < @max_retries
|
|
59
|
+
sleep(backoff(attempt))
|
|
60
|
+
attempt += 1
|
|
61
|
+
next
|
|
62
|
+
end
|
|
63
|
+
json = raw.nil? || raw.empty? ? nil : (JSON.parse(raw) rescue nil)
|
|
64
|
+
return json.is_a?(Hash) ? json : { "data" => json } if status.between?(200, 299)
|
|
65
|
+
|
|
66
|
+
if (status == 429 || status >= 500) && retryable && attempt < @max_retries
|
|
67
|
+
after = res_headers["retry-after"].to_i
|
|
68
|
+
sleep(after.positive? ? [after, 30].min : backoff(attempt))
|
|
69
|
+
attempt += 1
|
|
70
|
+
next
|
|
71
|
+
end
|
|
72
|
+
raise Error.from_response(status, json, raw)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Plan, emails sent this month and today, and credits.
|
|
77
|
+
def usage = request(:get, "/usage")
|
|
78
|
+
|
|
79
|
+
# Sent, delivered, opened, clicked, bounced and complained totals: { days:, domain_id: }.
|
|
80
|
+
def metrics(params = nil, **fields) = request(:get, "/metrics", query: (params || {}).merge(fields))
|
|
81
|
+
|
|
82
|
+
private
|
|
83
|
+
|
|
84
|
+
def backoff(attempt)
|
|
85
|
+
[8.0, 0.5 * (2**attempt)].min + rand * 0.25
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def perform(method, url, headers, payload)
|
|
89
|
+
return @transport.call(method, url, headers, payload, @timeout) if @transport
|
|
90
|
+
|
|
91
|
+
uri = URI(url)
|
|
92
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
93
|
+
http.use_ssl = uri.scheme == "https"
|
|
94
|
+
http.open_timeout = [10, @timeout].min
|
|
95
|
+
http.read_timeout = @timeout
|
|
96
|
+
req = Net::HTTPGenericRequest.new(method, !payload.nil?, true, uri.request_uri, headers)
|
|
97
|
+
req.body = payload if payload
|
|
98
|
+
res = http.request(req)
|
|
99
|
+
[res.code.to_i, res.each_header.to_h, res.body.to_s]
|
|
100
|
+
rescue Net::OpenTimeout, Net::ReadTimeout
|
|
101
|
+
raise Error.new("No response within #{@timeout} seconds", status: 0, name: "timeout")
|
|
102
|
+
rescue SocketError, SystemCallError, IOError, OpenSSL::SSL::SSLError => e
|
|
103
|
+
raise Error.new("Couldn't reach Delivrie: #{e.message}", status: 0, name: "network_error")
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Delivrie
|
|
4
|
+
# Raised for any failed request. `status` is the HTTP status (0 for network errors and timeouts) and `name` is
|
|
5
|
+
# Delivrie's error name, like "validation_error", "invalid_api_key" or "rate_limit_exceeded".
|
|
6
|
+
class Error < StandardError
|
|
7
|
+
attr_reader :status, :name
|
|
8
|
+
|
|
9
|
+
def initialize(message, status: 0, name: "application_error")
|
|
10
|
+
super(message)
|
|
11
|
+
@status = status
|
|
12
|
+
@name = name
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# @api private
|
|
16
|
+
def self.from_response(status, json, raw)
|
|
17
|
+
j = json.is_a?(Hash) ? json : {}
|
|
18
|
+
name = j["name"].is_a?(String) ? j["name"] : { 401 => "invalid_api_key", 404 => "not_found", 429 => "rate_limit_exceeded" }.fetch(status, "application_error")
|
|
19
|
+
message = j["message"].is_a?(String) ? j["message"] : (raw.to_s[0, 300].strip.empty? ? "Request failed with status #{status}" : raw.to_s[0, 300].strip)
|
|
20
|
+
new(message, status: j["statusCode"].is_a?(Integer) ? j["statusCode"] : status, name: name)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Raised by Delivrie::Webhook.verify when a request isn't a genuine, recent Delivrie webhook.
|
|
25
|
+
class WebhookVerificationError < StandardError; end
|
|
26
|
+
end
|
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
# Generated by tools/gen_sdks.py from tools/sdk_spec.py. Don't edit by hand.
|
|
3
|
+
|
|
4
|
+
require "base64"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module Delivrie
|
|
8
|
+
# Base class for API resources.
|
|
9
|
+
class Resource
|
|
10
|
+
def initialize(client)
|
|
11
|
+
@client = client
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
private
|
|
15
|
+
|
|
16
|
+
def enc(id) = URI.encode_www_form_component(id.to_s).gsub("+", "%20")
|
|
17
|
+
|
|
18
|
+
# Methods take a Hash, keyword arguments, or both: send_email(from: "…", to: "…").
|
|
19
|
+
def merge(params, fields)
|
|
20
|
+
return params unless params.nil? || params.is_a?(Hash)
|
|
21
|
+
(params || {}).merge(fields)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Local attachment files ({ path: "./invoice.pdf" }) are read and sent as base64; https:// paths are fetched by Delivrie.
|
|
25
|
+
def prepare_email(email)
|
|
26
|
+
return email unless email.is_a?(Hash)
|
|
27
|
+
e = email.transform_keys(&:to_s)
|
|
28
|
+
e["scheduled_at"] = e["scheduled_at"].iso8601 if e["scheduled_at"].respond_to?(:iso8601)
|
|
29
|
+
if e["attachments"].is_a?(Array)
|
|
30
|
+
e["attachments"] = e["attachments"].map do |a|
|
|
31
|
+
a = a.transform_keys(&:to_s)
|
|
32
|
+
if a["path"] && !a["content"] && !a["path"].to_s.match?(%r{\Ahttps?://})
|
|
33
|
+
a["content"] = Base64.strict_encode64(File.binread(a["path"]))
|
|
34
|
+
a["filename"] ||= File.basename(a["path"])
|
|
35
|
+
a.delete("path")
|
|
36
|
+
end
|
|
37
|
+
a
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
e
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Send, schedule, cancel, forward and look up emails.
|
|
45
|
+
class Emails < Resource
|
|
46
|
+
# Send an email. Returns { id }.
|
|
47
|
+
def send_email(params = nil, idempotency_key: nil, **fields)
|
|
48
|
+
@client.request(:post, "/emails", prepare_email(merge(params, fields)), idempotency_key: idempotency_key)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Send up to 100 emails in one request.
|
|
52
|
+
def send_batch(params, idempotency_key: nil)
|
|
53
|
+
@client.request(:post, "/emails/batch", params.map { |e| prepare_email(e) }, idempotency_key: idempotency_key)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# One email with its delivery events.
|
|
57
|
+
def get(id)
|
|
58
|
+
@client.request(:get, "/emails/#{enc(id)}")
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
# Sent emails, newest first.
|
|
62
|
+
def list(params = nil, **fields)
|
|
63
|
+
@client.request(:get, "/emails", query: merge(params, fields))
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Received emails, newest first.
|
|
67
|
+
def list_received(params = nil, **fields)
|
|
68
|
+
@client.request(:get, "/emails/received", query: merge(params, fields))
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# Reschedule a scheduled email: { scheduled_at }.
|
|
72
|
+
def update(id, params = nil, **fields)
|
|
73
|
+
@client.request(:patch, "/emails/#{enc(id)}", merge(params, fields))
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Cancel a scheduled email.
|
|
77
|
+
def cancel(id)
|
|
78
|
+
@client.request(:post, "/emails/#{enc(id)}/cancel", {})
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Forward an email: { to, from, note }. Stored attachments are included.
|
|
82
|
+
def forward(id, params = nil, **fields)
|
|
83
|
+
@client.request(:post, "/emails/#{enc(id)}/forward", merge(params, fields))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# A sent email's files with one-hour download links.
|
|
87
|
+
def attachments(id)
|
|
88
|
+
@client.request(:get, "/emails/#{enc(id)}/attachments")
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# A received email's files with one-hour download links.
|
|
92
|
+
def received_attachments(id)
|
|
93
|
+
@client.request(:get, "/emails/received/#{enc(id)}/attachments")
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# A public link to the email and its delivery timeline: { expires_in_hours }.
|
|
97
|
+
def share(id, params = nil, **fields)
|
|
98
|
+
@client.request(:post, "/emails/#{enc(id)}/share", merge(params, fields))
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Sending and receiving domains.
|
|
103
|
+
class Domains < Resource
|
|
104
|
+
# Add a domain: { name, region }. Returns its DNS records.
|
|
105
|
+
def create(params = nil, **fields)
|
|
106
|
+
@client.request(:post, "/domains", merge(params, fields))
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# A domain with its DNS records and their status.
|
|
110
|
+
def get(id)
|
|
111
|
+
@client.request(:get, "/domains/#{enc(id)}")
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
# Every domain on the team.
|
|
115
|
+
def list
|
|
116
|
+
@client.request(:get, "/domains")
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Check DNS now.
|
|
120
|
+
def verify(id)
|
|
121
|
+
@client.request(:post, "/domains/#{enc(id)}/verify", {})
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Change tracking, TLS or receiving settings.
|
|
125
|
+
def update(id, params = nil, **fields)
|
|
126
|
+
@client.request(:patch, "/domains/#{enc(id)}", merge(params, fields))
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Remove a domain.
|
|
130
|
+
def delete(id)
|
|
131
|
+
@client.request(:delete, "/domains/#{enc(id)}")
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# API keys.
|
|
136
|
+
class ApiKeys < Resource
|
|
137
|
+
# Create a key: { name, permission, domain_id }. The token is returned once.
|
|
138
|
+
def create(params = nil, **fields)
|
|
139
|
+
@client.request(:post, "/api-keys", merge(params, fields))
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# API keys (tokens are never returned).
|
|
143
|
+
def list
|
|
144
|
+
@client.request(:get, "/api-keys")
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# Rename a key: { name }.
|
|
148
|
+
def update(id, params = nil, **fields)
|
|
149
|
+
@client.request(:patch, "/api-keys/#{enc(id)}", merge(params, fields))
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Delete a key. Requests with it fail straight away.
|
|
153
|
+
def delete(id)
|
|
154
|
+
@client.request(:delete, "/api-keys/#{enc(id)}")
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Webhook endpoints.
|
|
159
|
+
class Webhooks < Resource
|
|
160
|
+
# Create an endpoint: { endpoint, events }. Returns its signing_secret once.
|
|
161
|
+
def create(params = nil, **fields)
|
|
162
|
+
@client.request(:post, "/webhooks", merge(params, fields))
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Webhook endpoints.
|
|
166
|
+
def list
|
|
167
|
+
@client.request(:get, "/webhooks")
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Change the endpoint, events or status (enabled, disabled).
|
|
171
|
+
def update(id, params = nil, **fields)
|
|
172
|
+
@client.request(:patch, "/webhooks/#{enc(id)}", merge(params, fields))
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Delete an endpoint.
|
|
176
|
+
def delete(id)
|
|
177
|
+
@client.request(:delete, "/webhooks/#{enc(id)}")
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Recent deliveries with status codes.
|
|
181
|
+
def deliveries(id)
|
|
182
|
+
@client.request(:get, "/webhooks/#{enc(id)}/events")
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# Send a test event and return the response.
|
|
186
|
+
def test(id)
|
|
187
|
+
@client.request(:post, "/webhooks/#{enc(id)}/test", {})
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# Send a past delivery again.
|
|
191
|
+
def replay(id)
|
|
192
|
+
@client.request(:post, "/webhook-deliveries/#{enc(id)}/replay", {})
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
# Contacts and contact imports.
|
|
197
|
+
class Contacts < Resource
|
|
198
|
+
# Create or update a contact by email.
|
|
199
|
+
def create(params = nil, **fields)
|
|
200
|
+
@client.request(:post, "/contacts", merge(params, fields))
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
# A contact by id or email.
|
|
204
|
+
def get(id)
|
|
205
|
+
@client.request(:get, "/contacts/#{enc(id)}")
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
# Contacts, newest first.
|
|
209
|
+
def list(params = nil, **fields)
|
|
210
|
+
@client.request(:get, "/contacts", query: merge(params, fields))
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# Change a contact's name, properties or subscription.
|
|
214
|
+
def update(id, params = nil, **fields)
|
|
215
|
+
@client.request(:patch, "/contacts/#{enc(id)}", merge(params, fields))
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
# Delete a contact.
|
|
219
|
+
def delete(id)
|
|
220
|
+
@client.request(:delete, "/contacts/#{enc(id)}")
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# Import up to 50,000 contacts from { csv } or { contacts }. Runs in the background.
|
|
224
|
+
def import_contacts(params = nil, **fields)
|
|
225
|
+
@client.request(:post, "/contacts/imports", merge(params, fields))
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# An import's progress and result.
|
|
229
|
+
def get_import(id)
|
|
230
|
+
@client.request(:get, "/contacts/imports/#{enc(id)}")
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Recent imports.
|
|
234
|
+
def list_imports(params = nil, **fields)
|
|
235
|
+
@client.request(:get, "/contacts/imports", query: merge(params, fields))
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
# Segments of contacts.
|
|
240
|
+
class Segments < Resource
|
|
241
|
+
# Create a segment: { name }.
|
|
242
|
+
def create(params = nil, **fields)
|
|
243
|
+
@client.request(:post, "/segments", merge(params, fields))
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# A segment with its contact count.
|
|
247
|
+
def get(id)
|
|
248
|
+
@client.request(:get, "/segments/#{enc(id)}")
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# Segments.
|
|
252
|
+
def list
|
|
253
|
+
@client.request(:get, "/segments")
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Rename a segment: { name }.
|
|
257
|
+
def update(id, params = nil, **fields)
|
|
258
|
+
@client.request(:patch, "/segments/#{enc(id)}", merge(params, fields))
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# Delete a segment (the contacts stay).
|
|
262
|
+
def delete(id)
|
|
263
|
+
@client.request(:delete, "/segments/#{enc(id)}")
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# The contacts in a segment.
|
|
267
|
+
def contacts(id, params = nil, **fields)
|
|
268
|
+
@client.request(:get, "/segments/#{enc(id)}/contacts", query: merge(params, fields))
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
# Add contacts: { contacts: [emails or objects] }.
|
|
272
|
+
def add_contacts(id, params = nil, **fields)
|
|
273
|
+
@client.request(:post, "/segments/#{enc(id)}/contacts", merge(params, fields))
|
|
274
|
+
end
|
|
275
|
+
|
|
276
|
+
# Take contacts out: { contacts: [emails or ids] }.
|
|
277
|
+
def remove_contacts(id, params = nil, **fields)
|
|
278
|
+
@client.request(:delete, "/segments/#{enc(id)}/contacts", merge(params, fields))
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
# Broadcasts to a segment.
|
|
283
|
+
class Broadcasts < Resource
|
|
284
|
+
# Create a draft.
|
|
285
|
+
def create(params = nil, **fields)
|
|
286
|
+
@client.request(:post, "/broadcasts", merge(params, fields))
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# A broadcast with its stats.
|
|
290
|
+
def get(id)
|
|
291
|
+
@client.request(:get, "/broadcasts/#{enc(id)}")
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# Broadcasts.
|
|
295
|
+
def list
|
|
296
|
+
@client.request(:get, "/broadcasts")
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# Edit a draft.
|
|
300
|
+
def update(id, params = nil, **fields)
|
|
301
|
+
@client.request(:patch, "/broadcasts/#{enc(id)}", merge(params, fields))
|
|
302
|
+
end
|
|
303
|
+
|
|
304
|
+
# Send now, or schedule with { scheduled_at }.
|
|
305
|
+
def send_broadcast(id, params = nil, **fields)
|
|
306
|
+
@client.request(:post, "/broadcasts/#{enc(id)}/send", merge(params, fields))
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# Cancel a scheduled broadcast (it goes back to draft).
|
|
310
|
+
def cancel(id)
|
|
311
|
+
@client.request(:post, "/broadcasts/#{enc(id)}/cancel", {})
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Copy it as a new draft.
|
|
315
|
+
def duplicate(id, params = nil, **fields)
|
|
316
|
+
@client.request(:post, "/broadcasts/#{enc(id)}/duplicate", merge(params, fields))
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
# Who it went to (or will reach), with delivery flags.
|
|
320
|
+
def recipients(id, params = nil, **fields)
|
|
321
|
+
@client.request(:get, "/broadcasts/#{enc(id)}/recipients", query: merge(params, fields))
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# Clicked links with total and unique clicks.
|
|
325
|
+
def links(id)
|
|
326
|
+
@client.request(:get, "/broadcasts/#{enc(id)}/links")
|
|
327
|
+
end
|
|
328
|
+
|
|
329
|
+
# Delete a broadcast that isn't sending.
|
|
330
|
+
def delete(id)
|
|
331
|
+
@client.request(:delete, "/broadcasts/#{enc(id)}")
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# A draft's subject and blocks with ids.
|
|
335
|
+
def get_content(id)
|
|
336
|
+
@client.request(:get, "/broadcasts/#{enc(id)}/content")
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
# Edit a draft block by block: { ops } or { blocks }. Shows up live in the editor.
|
|
340
|
+
def edit_content(id, params = nil, **fields)
|
|
341
|
+
@client.request(:post, "/broadcasts/#{enc(id)}/content", merge(params, fields))
|
|
342
|
+
end
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
# Email templates.
|
|
346
|
+
class Templates < Resource
|
|
347
|
+
# Create a template.
|
|
348
|
+
def create(params = nil, **fields)
|
|
349
|
+
@client.request(:post, "/templates", merge(params, fields))
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
# A template by id or alias.
|
|
353
|
+
def get(id)
|
|
354
|
+
@client.request(:get, "/templates/#{enc(id)}")
|
|
355
|
+
end
|
|
356
|
+
|
|
357
|
+
# Templates.
|
|
358
|
+
def list
|
|
359
|
+
@client.request(:get, "/templates")
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
# Change a template. Saved as a draft until published.
|
|
363
|
+
def update(id, params = nil, **fields)
|
|
364
|
+
@client.request(:patch, "/templates/#{enc(id)}", merge(params, fields))
|
|
365
|
+
end
|
|
366
|
+
|
|
367
|
+
# Publish the current draft.
|
|
368
|
+
def publish(id)
|
|
369
|
+
@client.request(:post, "/templates/#{enc(id)}/publish", {})
|
|
370
|
+
end
|
|
371
|
+
|
|
372
|
+
# Copy it as a new draft.
|
|
373
|
+
def duplicate(id, params = nil, **fields)
|
|
374
|
+
@client.request(:post, "/templates/#{enc(id)}/duplicate", merge(params, fields))
|
|
375
|
+
end
|
|
376
|
+
|
|
377
|
+
# Delete a template.
|
|
378
|
+
def delete(id)
|
|
379
|
+
@client.request(:delete, "/templates/#{enc(id)}")
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
# Its subject and blocks with ids.
|
|
383
|
+
def get_content(id)
|
|
384
|
+
@client.request(:get, "/templates/#{enc(id)}/content")
|
|
385
|
+
end
|
|
386
|
+
|
|
387
|
+
# Edit it block by block: { ops } or { blocks }. Shows up live in the editor.
|
|
388
|
+
def edit_content(id, params = nil, **fields)
|
|
389
|
+
@client.request(:post, "/templates/#{enc(id)}/content", merge(params, fields))
|
|
390
|
+
end
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
# Addresses Delivrie won't send to.
|
|
394
|
+
class Suppressions < Resource
|
|
395
|
+
# Suppressed addresses.
|
|
396
|
+
def list
|
|
397
|
+
@client.request(:get, "/suppressions")
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# Suppress addresses: { emails }.
|
|
401
|
+
def add(params = nil, **fields)
|
|
402
|
+
@client.request(:post, "/suppressions", merge(params, fields))
|
|
403
|
+
end
|
|
404
|
+
|
|
405
|
+
# Allow sending to an address again.
|
|
406
|
+
def delete(id)
|
|
407
|
+
@client.request(:delete, "/suppressions/#{enc(id)}")
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
# Events that start automations.
|
|
412
|
+
class Events < Resource
|
|
413
|
+
# Record an event: { name, email, data }.
|
|
414
|
+
def send_event(params = nil, idempotency_key: nil, **fields)
|
|
415
|
+
@client.request(:post, "/events", merge(params, fields), idempotency_key: idempotency_key)
|
|
416
|
+
end
|
|
417
|
+
|
|
418
|
+
# Event names your app has sent, or recent events with { name }.
|
|
419
|
+
def list(params = nil, **fields)
|
|
420
|
+
@client.request(:get, "/events", query: merge(params, fields))
|
|
421
|
+
end
|
|
422
|
+
end
|
|
423
|
+
|
|
424
|
+
# Automations.
|
|
425
|
+
class Automations < Resource
|
|
426
|
+
# Create an automation: a trigger and send/wait steps.
|
|
427
|
+
def create(params = nil, **fields)
|
|
428
|
+
@client.request(:post, "/automations", merge(params, fields))
|
|
429
|
+
end
|
|
430
|
+
|
|
431
|
+
# An automation with run counts.
|
|
432
|
+
def get(id)
|
|
433
|
+
@client.request(:get, "/automations/#{enc(id)}")
|
|
434
|
+
end
|
|
435
|
+
|
|
436
|
+
# Automations.
|
|
437
|
+
def list
|
|
438
|
+
@client.request(:get, "/automations")
|
|
439
|
+
end
|
|
440
|
+
|
|
441
|
+
# Rename it or change re-entry: { name, reentry }.
|
|
442
|
+
def update(id, params = nil, **fields)
|
|
443
|
+
@client.request(:patch, "/automations/#{enc(id)}", merge(params, fields))
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# Delete an automation.
|
|
447
|
+
def delete(id)
|
|
448
|
+
@client.request(:delete, "/automations/#{enc(id)}")
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
# Recent runs with each step's log.
|
|
452
|
+
def runs(id, params = nil, **fields)
|
|
453
|
+
@client.request(:get, "/automations/#{enc(id)}/runs", query: merge(params, fields))
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
# Publish it so it starts running.
|
|
457
|
+
def publish(id)
|
|
458
|
+
@client.request(:post, "/automations/#{enc(id)}/publish", {})
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
# Pause it.
|
|
462
|
+
def pause(id)
|
|
463
|
+
@client.request(:post, "/automations/#{enc(id)}/pause", {})
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
# Resume it.
|
|
467
|
+
def resume(id)
|
|
468
|
+
@client.request(:post, "/automations/#{enc(id)}/resume", {})
|
|
469
|
+
end
|
|
470
|
+
|
|
471
|
+
# Copy it as a new draft.
|
|
472
|
+
def duplicate(id, params = nil, **fields)
|
|
473
|
+
@client.request(:post, "/automations/#{enc(id)}/duplicate", merge(params, fields))
|
|
474
|
+
end
|
|
475
|
+
end
|
|
476
|
+
|
|
477
|
+
# Inboxes and their agent settings.
|
|
478
|
+
class Inboxes < Resource
|
|
479
|
+
# Inboxes with open, unread and needs-reply counts.
|
|
480
|
+
def list
|
|
481
|
+
@client.request(:get, "/inboxes")
|
|
482
|
+
end
|
|
483
|
+
|
|
484
|
+
# Create an inbox: { address, name }.
|
|
485
|
+
def create(params = nil, **fields)
|
|
486
|
+
@client.request(:post, "/inboxes", merge(params, fields))
|
|
487
|
+
end
|
|
488
|
+
|
|
489
|
+
# An inbox by id or address.
|
|
490
|
+
def get(id)
|
|
491
|
+
@client.request(:get, "/inboxes/#{enc(id)}")
|
|
492
|
+
end
|
|
493
|
+
|
|
494
|
+
# Change its name, description or agent.
|
|
495
|
+
def update(id, params = nil, **fields)
|
|
496
|
+
@client.request(:patch, "/inboxes/#{enc(id)}", merge(params, fields))
|
|
497
|
+
end
|
|
498
|
+
|
|
499
|
+
# Remove an inbox.
|
|
500
|
+
def delete(id)
|
|
501
|
+
@client.request(:delete, "/inboxes/#{enc(id)}")
|
|
502
|
+
end
|
|
503
|
+
|
|
504
|
+
# The agent's recent runs.
|
|
505
|
+
def agent_runs(id, params = nil, **fields)
|
|
506
|
+
@client.request(:get, "/inboxes/#{enc(id)}/agent-runs", query: merge(params, fields))
|
|
507
|
+
end
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
# Conversations, drafts and notes in the Inbox.
|
|
511
|
+
class Inbox < Resource
|
|
512
|
+
# Threads, newest first. Filters: inbox, status, unread, needs_reply, assignee, label, q.
|
|
513
|
+
def threads(params = nil, **fields)
|
|
514
|
+
@client.request(:get, "/inbox/threads", query: merge(params, fields))
|
|
515
|
+
end
|
|
516
|
+
|
|
517
|
+
# A thread with its messages, notes and drafts.
|
|
518
|
+
def thread(id)
|
|
519
|
+
@client.request(:get, "/inbox/threads/#{enc(id)}")
|
|
520
|
+
end
|
|
521
|
+
|
|
522
|
+
# Change status, unread, assignee or labels.
|
|
523
|
+
def update_thread(id, params = nil, **fields)
|
|
524
|
+
@client.request(:patch, "/inbox/threads/#{enc(id)}", merge(params, fields))
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
# Reply: { text } or { html }.
|
|
528
|
+
def reply(id, params = nil, **fields)
|
|
529
|
+
@client.request(:post, "/inbox/threads/#{enc(id)}/reply", merge(params, fields))
|
|
530
|
+
end
|
|
531
|
+
|
|
532
|
+
# Run the inbox's agent on this thread.
|
|
533
|
+
def run_agent(id)
|
|
534
|
+
@client.request(:post, "/inbox/threads/#{enc(id)}/agent", {})
|
|
535
|
+
end
|
|
536
|
+
|
|
537
|
+
# Internal notes.
|
|
538
|
+
def notes(id)
|
|
539
|
+
@client.request(:get, "/inbox/threads/#{enc(id)}/notes")
|
|
540
|
+
end
|
|
541
|
+
|
|
542
|
+
# Add a note: { body }.
|
|
543
|
+
def add_note(id, params = nil, **fields)
|
|
544
|
+
@client.request(:post, "/inbox/threads/#{enc(id)}/notes", merge(params, fields))
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
# Delete a note.
|
|
548
|
+
def delete_note(id)
|
|
549
|
+
@client.request(:delete, "/inbox/notes/#{enc(id)}")
|
|
550
|
+
end
|
|
551
|
+
|
|
552
|
+
# Open drafts.
|
|
553
|
+
def drafts(params = nil, **fields)
|
|
554
|
+
@client.request(:get, "/inbox/drafts", query: merge(params, fields))
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
# Save a draft.
|
|
558
|
+
def create_draft(params = nil, **fields)
|
|
559
|
+
@client.request(:post, "/inbox/drafts", merge(params, fields))
|
|
560
|
+
end
|
|
561
|
+
|
|
562
|
+
# Edit a draft.
|
|
563
|
+
def update_draft(id, params = nil, **fields)
|
|
564
|
+
@client.request(:patch, "/inbox/drafts/#{enc(id)}", merge(params, fields))
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
# Send a draft.
|
|
568
|
+
def send_draft(id)
|
|
569
|
+
@client.request(:post, "/inbox/drafts/#{enc(id)}/send", {})
|
|
570
|
+
end
|
|
571
|
+
|
|
572
|
+
# Discard a draft.
|
|
573
|
+
def discard_draft(id)
|
|
574
|
+
@client.request(:delete, "/inbox/drafts/#{enc(id)}")
|
|
575
|
+
end
|
|
576
|
+
|
|
577
|
+
# Labels.
|
|
578
|
+
def labels
|
|
579
|
+
@client.request(:get, "/inbox/labels")
|
|
580
|
+
end
|
|
581
|
+
end
|
|
582
|
+
|
|
583
|
+
# AI writing in the brand voice.
|
|
584
|
+
class Ai < Resource
|
|
585
|
+
# Write an email from { url } and/or { brief }; { target } writes it into a template or draft broadcast.
|
|
586
|
+
def draft(params = nil, **fields)
|
|
587
|
+
@client.request(:post, "/ai/draft", merge(params, fields))
|
|
588
|
+
end
|
|
589
|
+
|
|
590
|
+
# Rewrite { blocks } with an { instruction }.
|
|
591
|
+
def rewrite(params = nil, **fields)
|
|
592
|
+
@client.request(:post, "/ai/rewrite", merge(params, fields))
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
# Subject line ideas for an email's { text }.
|
|
596
|
+
def subjects(params = nil, **fields)
|
|
597
|
+
@client.request(:post, "/ai/subjects", merge(params, fields))
|
|
598
|
+
end
|
|
599
|
+
end
|
|
600
|
+
|
|
601
|
+
# API request logs.
|
|
602
|
+
class Logs < Resource
|
|
603
|
+
# Recent API requests.
|
|
604
|
+
def list(params = nil, **fields)
|
|
605
|
+
@client.request(:get, "/logs", query: merge(params, fields))
|
|
606
|
+
end
|
|
607
|
+
|
|
608
|
+
# One request with its bodies.
|
|
609
|
+
def get(id)
|
|
610
|
+
@client.request(:get, "/logs/#{enc(id)}")
|
|
611
|
+
end
|
|
612
|
+
end
|
|
613
|
+
|
|
614
|
+
class Client
|
|
615
|
+
def emails = @emails ||= Emails.new(self)
|
|
616
|
+
def domains = @domains ||= Domains.new(self)
|
|
617
|
+
def api_keys = @api_keys ||= ApiKeys.new(self)
|
|
618
|
+
def webhooks = @webhooks ||= Webhooks.new(self)
|
|
619
|
+
def contacts = @contacts ||= Contacts.new(self)
|
|
620
|
+
def segments = @segments ||= Segments.new(self)
|
|
621
|
+
def broadcasts = @broadcasts ||= Broadcasts.new(self)
|
|
622
|
+
def templates = @templates ||= Templates.new(self)
|
|
623
|
+
def suppressions = @suppressions ||= Suppressions.new(self)
|
|
624
|
+
def events = @events ||= Events.new(self)
|
|
625
|
+
def automations = @automations ||= Automations.new(self)
|
|
626
|
+
def inboxes = @inboxes ||= Inboxes.new(self)
|
|
627
|
+
def inbox = @inbox ||= Inbox.new(self)
|
|
628
|
+
def ai = @ai ||= Ai.new(self)
|
|
629
|
+
def logs = @logs ||= Logs.new(self)
|
|
630
|
+
end
|
|
631
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "base64"
|
|
4
|
+
require "json"
|
|
5
|
+
require "openssl"
|
|
6
|
+
|
|
7
|
+
module Delivrie
|
|
8
|
+
# Verifies webhook requests. Delivrie signs with the Standard Webhooks scheme: webhook-id, webhook-timestamp and
|
|
9
|
+
# webhook-signature headers, the signature being "v1,<base64 HMAC-SHA256 of id.timestamp.body>" keyed with the
|
|
10
|
+
# endpoint's whsec_ signing secret.
|
|
11
|
+
#
|
|
12
|
+
# event = Delivrie::Webhook.verify(request.raw_post, request.headers, ENV["DELIVRIE_WEBHOOK_SECRET"])
|
|
13
|
+
module Webhook
|
|
14
|
+
module_function
|
|
15
|
+
|
|
16
|
+
# Returns the parsed event (a Hash), or raises Delivrie::WebhookVerificationError.
|
|
17
|
+
# payload: the raw request body, exactly as received. headers: any Hash-like object (any letter case).
|
|
18
|
+
def verify(payload, headers, secret, tolerance: 300, now: nil)
|
|
19
|
+
raise WebhookVerificationError, "Missing signing secret" if secret.nil? || secret.empty?
|
|
20
|
+
|
|
21
|
+
h = {}
|
|
22
|
+
headers.each { |k, v| h[k.to_s.downcase.tr("_", "-").sub(/\Ahttp-/, "")] = Array(v).first.to_s }
|
|
23
|
+
id, ts, sigs = h["webhook-id"], h["webhook-timestamp"], h["webhook-signature"]
|
|
24
|
+
raise WebhookVerificationError, "Missing webhook-id, webhook-timestamp or webhook-signature header" if [id, ts, sigs].any? { |x| x.nil? || x.empty? }
|
|
25
|
+
raise WebhookVerificationError, "Invalid webhook-timestamp header" unless ts.match?(/\A\d+\z/)
|
|
26
|
+
raise WebhookVerificationError, "Webhook timestamp is too old or in the future" if ((now || Time.now.to_i) - ts.to_i).abs > tolerance
|
|
27
|
+
|
|
28
|
+
key = begin
|
|
29
|
+
Base64.strict_decode64(secret.delete_prefix("whsec_"))
|
|
30
|
+
rescue ArgumentError
|
|
31
|
+
raise WebhookVerificationError, "Signing secret isn't valid base64"
|
|
32
|
+
end
|
|
33
|
+
expected = OpenSSL::HMAC.digest("SHA256", key, "#{id}.#{ts}.#{payload}")
|
|
34
|
+
# Several space-separated signatures can arrive during secret rotation; any valid one is enough.
|
|
35
|
+
ok = sigs.split(/\s+/).any? do |part|
|
|
36
|
+
version, sig = part.split(",", 2)
|
|
37
|
+
next false unless version == "v1" && sig
|
|
38
|
+
decoded = (Base64.strict_decode64(sig) rescue nil)
|
|
39
|
+
decoded && decoded.bytesize == expected.bytesize && OpenSSL.fixed_length_secure_compare(decoded, expected)
|
|
40
|
+
end
|
|
41
|
+
raise WebhookVerificationError, "Signature doesn't match" unless ok
|
|
42
|
+
|
|
43
|
+
event = JSON.parse(payload) rescue nil
|
|
44
|
+
raise WebhookVerificationError, "Body is not valid JSON" unless event.is_a?(Hash)
|
|
45
|
+
event
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
data/lib/delivrie.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "delivrie/version"
|
|
4
|
+
require_relative "delivrie/error"
|
|
5
|
+
require_relative "delivrie/client"
|
|
6
|
+
require_relative "delivrie/resources"
|
|
7
|
+
require_relative "delivrie/webhook"
|
|
8
|
+
|
|
9
|
+
# Delivrie: email for developers and agents. See https://delivrie.io/docs/ruby
|
|
10
|
+
module Delivrie
|
|
11
|
+
# Shortcut for Delivrie::Client.new.
|
|
12
|
+
def self.new(*args, **kwargs) = Client.new(*args, **kwargs)
|
|
13
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: delivrie
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- South Circle
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Send transactional email and broadcasts, manage domains, contacts, templates
|
|
14
|
+
and automations, work the Inbox and verify webhooks with Delivrie. No runtime dependencies.
|
|
15
|
+
email: support@delivrie.io
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- LICENSE
|
|
21
|
+
- README.md
|
|
22
|
+
- lib/delivrie.rb
|
|
23
|
+
- lib/delivrie/client.rb
|
|
24
|
+
- lib/delivrie/error.rb
|
|
25
|
+
- lib/delivrie/resources.rb
|
|
26
|
+
- lib/delivrie/version.rb
|
|
27
|
+
- lib/delivrie/webhook.rb
|
|
28
|
+
homepage: https://delivrie.io/docs/ruby
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata:
|
|
32
|
+
homepage_uri: https://delivrie.io/docs/ruby
|
|
33
|
+
documentation_uri: https://delivrie.io/docs/ruby
|
|
34
|
+
source_code_uri: https://github.com/bysurce/delivrie/tree/main/sdk/ruby
|
|
35
|
+
changelog_uri: https://delivrie.io/changelog
|
|
36
|
+
rubygems_mfa_required: 'true'
|
|
37
|
+
post_install_message:
|
|
38
|
+
rdoc_options: []
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '3.1'
|
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '0'
|
|
51
|
+
requirements: []
|
|
52
|
+
rubygems_version: 3.5.22
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 4
|
|
55
|
+
summary: Ruby client for the Delivrie email API
|
|
56
|
+
test_files: []
|