mailkube 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.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +201 -0
  3. data/NOTICE +11 -0
  4. data/README.md +323 -0
  5. data/lib/mailkube/client.rb +52 -0
  6. data/lib/mailkube/config.rb +89 -0
  7. data/lib/mailkube/errors.rb +132 -0
  8. data/lib/mailkube/events/contexts.rb +129 -0
  9. data/lib/mailkube/events/envelopes.rb +96 -0
  10. data/lib/mailkube/events/node.rb +62 -0
  11. data/lib/mailkube/events/payloads.rb +88 -0
  12. data/lib/mailkube/events/registry.rb +26 -0
  13. data/lib/mailkube/logging.rb +113 -0
  14. data/lib/mailkube/net_http_adapter.rb +123 -0
  15. data/lib/mailkube/resources/emails.rb +81 -0
  16. data/lib/mailkube/resources/scheduled_email_requests.rb +78 -0
  17. data/lib/mailkube/resources/scheduled_emails.rb +142 -0
  18. data/lib/mailkube/serialization.rb +98 -0
  19. data/lib/mailkube/transport.rb +190 -0
  20. data/lib/mailkube/types/scheduled_acks.rb +57 -0
  21. data/lib/mailkube/types/scheduled_emails.rb +96 -0
  22. data/lib/mailkube/types.rb +54 -0
  23. data/lib/mailkube/version.rb +15 -0
  24. data/lib/mailkube/webhooks.rb +152 -0
  25. data/lib/mailkube.rb +68 -0
  26. data/sig/mailkube/client.rbs +9 -0
  27. data/sig/mailkube/config.rbs +14 -0
  28. data/sig/mailkube/errors.rbs +78 -0
  29. data/sig/mailkube/events/contexts.rbs +60 -0
  30. data/sig/mailkube/events/envelopes.rbs +60 -0
  31. data/sig/mailkube/events/node.rbs +32 -0
  32. data/sig/mailkube/events/payloads.rbs +54 -0
  33. data/sig/mailkube/events/registry.rbs +6 -0
  34. data/sig/mailkube/logging.rbs +26 -0
  35. data/sig/mailkube/net_http_adapter.rbs +15 -0
  36. data/sig/mailkube/resources/emails.rbs +18 -0
  37. data/sig/mailkube/resources/scheduled_email_requests.rbs +19 -0
  38. data/sig/mailkube/resources/scheduled_emails.rbs +32 -0
  39. data/sig/mailkube/serialization.rbs +11 -0
  40. data/sig/mailkube/transport.rbs +34 -0
  41. data/sig/mailkube/types/scheduled_acks.rbs +30 -0
  42. data/sig/mailkube/types/scheduled_emails.rbs +53 -0
  43. data/sig/mailkube/types.rbs +44 -0
  44. data/sig/mailkube/webhooks.rbs +30 -0
  45. data/sig/mailkube.rbs +36 -0
  46. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7641c72078cd5550384c2e708dc156aea1f487d2304d07689ed7fcebc0868bd4
4
+ data.tar.gz: 4d0d9ea1e793297301d4cc7652a3ee0fa40b08dc7fcc2fa069b33cb20ddcfef2
5
+ SHA512:
6
+ metadata.gz: 8abad977f64add055a7d30eb83c88529ba3602fdeeb37f7f543386f2b7683fbec70e00f28e7cd102fa1288fabf4f503dbf81cf4c616429f62539d672c3c2b499
7
+ data.tar.gz: dd85a8fc81e7cc52a2446bd13e70a348866694b65c0034133b4dbfe7fe8ecd31c45f82a4479216e60d783785131a5d5a27fa90ce265d86c3949493f161191cc2
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or Derivative
95
+ Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Mailtactic, Corp.
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/NOTICE ADDED
@@ -0,0 +1,11 @@
1
+ mailkube-ruby
2
+ Copyright 2026 Mailtactic, Corp.
3
+
4
+ This product is licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this product except in compliance with the License. You may
6
+ obtain a copy of the License at:
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ "mailkube" is a trademark of Mailtactic, Corp. Use of the name is governed by
11
+ Section 6 (Trademarks) of the License.
data/README.md ADDED
@@ -0,0 +1,323 @@
1
+ # mailkube-ruby
2
+
3
+ [![CI](https://github.com/mailkube/mailkube-ruby/actions/workflows/ci.yml/badge.svg)](https://github.com/mailkube/mailkube-ruby/actions/workflows/ci.yml)
4
+ [![Gem Version](https://img.shields.io/gem/v/mailkube.svg)](https://rubygems.org/gems/mailkube)
5
+ [![License: Apache 2.0](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)
6
+ [![Code of Conduct](https://img.shields.io/badge/Contributor%20Covenant-2.1-purple.svg)](CODE_OF_CONDUCT.md)
7
+
8
+ The official Ruby SDK for [mailkube](https://mailkube.com).
9
+
10
+ Requires Ruby 3.4 or newer.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ bundle add mailkube
16
+ ```
17
+
18
+ **Zero runtime dependencies.** The gemspec has no `add_dependency`; the standard library covers
19
+ everything. That is deliberate — this gem is installed into other people's applications, where
20
+ every dependency is a version conflict waiting to happen.
21
+
22
+ ## Usage
23
+
24
+ ```ruby
25
+ require "mailkube"
26
+
27
+ client = Mailkube.new # reads MAILKUBE_API_KEY
28
+
29
+ email = client.emails.send(
30
+ from: "Acme <hello@yourdomain.com>",
31
+ to: "customer@example.com",
32
+ subject: "Hello world",
33
+ html: "<p>It works!</p>"
34
+ )
35
+
36
+ puts email.id
37
+ ```
38
+
39
+ Create one client and reuse it.
40
+
41
+ ### Configuration
42
+
43
+ | Option | Argument | Environment | Default |
44
+ |---|---|---|---|
45
+ | API key | `api_key:` | `MAILKUBE_API_KEY` | required |
46
+ | Base URL | `base_url:` | `MAILKUBE_BASE_URL` | `https://api.mailkube.com/mta/v1/` |
47
+ | Timeout | `timeout:` | | 30s |
48
+ | HTTP adapter | `http:` | | `Mailkube::NetHttpAdapter` |
49
+
50
+ Pass your own `http:` adapter — anything responding to
51
+ `#call(method:, url:, headers:, body:)` and returning a `Mailkube::HttpResponse` — to route
52
+ through a proxy, add instrumentation, or drive the client from a test.
53
+
54
+ There are deliberately **no built-in retries**. A `RateLimitError` carries `retry_after` and a
55
+ `ServerError` is safe to retry with backoff, so the calling application decides.
56
+
57
+ ### Idempotency
58
+
59
+ Set an idempotency key on anything you might retry. The API replays the original response instead
60
+ of sending twice.
61
+
62
+ ```ruby
63
+ client.emails.send(**params, idempotency_key: "order-1234-shipped")
64
+ ```
65
+
66
+ Reusing a key with a *different* payload is a `ConflictError`, not a silent overwrite.
67
+
68
+ ### Attachments and tags
69
+
70
+ ```ruby
71
+ client.emails.send(
72
+ from: "Acme <hello@yourdomain.com>",
73
+ to: "customer@example.com",
74
+ subject: "Your report",
75
+ text: "Attached.",
76
+ attachments: [Mailkube::Attachment.new(filename: "report.csv", content: File.binread("report.csv"))],
77
+ tags: [Mailkube::Tag.new(name: "campaign", value: "welcome")]
78
+ )
79
+ ```
80
+
81
+ `content` is raw bytes; the SDK base64-encodes them for you. Tags are denormalized onto the sending
82
+ log, so you can filter, export and dashboard by them, and they ride along on delivery webhooks.
83
+ **Tag values are not encrypted — never put personal data in one.**
84
+
85
+ ## Scheduling
86
+
87
+ Pass `scheduled_at:` to schedule instead of sending now. The result reports `#scheduled?`.
88
+
89
+ ```ruby
90
+ email = client.emails.send(**params, scheduled_at: Time.now.utc + 3600)
91
+ email.scheduled? # => true
92
+ email.status # => "scheduled"
93
+ ```
94
+
95
+ A `Time` is rendered to ISO-8601 for you; a string is passed through untouched, so it must already
96
+ carry an offset. The instant must be in the future and within your plan's scheduling horizon.
97
+
98
+ ### Managing scheduled emails
99
+
100
+ ```ruby
101
+ client.scheduled_emails.get(email.id)
102
+ client.scheduled_emails.update(email.id, scheduled_at: later, batch_id: "welcome")
103
+ client.scheduled_emails.cancel(email.id)
104
+ ```
105
+
106
+ The content of a scheduled email is immutable; only its due time and batch can change.
107
+
108
+ ### Listing and pagination
109
+
110
+ ```ruby
111
+ page = client.scheduled_emails.list(status: %w[scheduled canceled], page: 2)
112
+ page.data # => [Mailkube::ScheduledEmail, ...]
113
+ page.pagination.total_count
114
+ page.more? # => true when the server issued a next-page link
115
+ ```
116
+
117
+ Filters: `status`, `batch_id`, `scheduled_at_gte`, `scheduled_at_lte`, `page`. An omitted filter is
118
+ never sent. Only `scheduled`, `canceled` and `failed` can be listed — a sent email has left the
119
+ collection, so `status: "sent"` is a validation error rather than an empty result.
120
+
121
+ For every page, use `iter_all`. It returns a lazy `Enumerator`, follows the server's `next` link
122
+ rather than counting pages, and makes no request until you iterate it:
123
+
124
+ ```ruby
125
+ client.scheduled_emails.iter_all(status: "scheduled").each { |email| puts email.id }
126
+ client.scheduled_emails.iter_all.first(5) # fetches only the pages it needs
127
+ ```
128
+
129
+ `recipients` on a listed email is a **summary string** (`"a@b.com +2"`), not an array. The full
130
+ recipient list stays server-side.
131
+
132
+ ### Batches
133
+
134
+ Group scheduled sends under a `batch_id:` and move or cancel them as a unit:
135
+
136
+ ```ruby
137
+ client.emails.send(**params, scheduled_at: due, batch_id: "welcome-2026-08")
138
+ client.scheduled_emails.batches.update("welcome-2026-08", scheduled_at: later)
139
+ client.scheduled_emails.batches.cancel("welcome-2026-08")
140
+ ```
141
+
142
+ An unknown batch is a no-op reporting `canceled_count: 0` rather than a 404, so a count of zero is
143
+ not a failure.
144
+
145
+ ## Errors
146
+
147
+ Every error descends from `Mailkube::Error`, so rescue the category you care about:
148
+
149
+ ```ruby
150
+ begin
151
+ client.emails.send(from: ..., to: ..., subject: ...)
152
+ rescue Mailkube::RateLimitError => e
153
+ sleep(e.retry_after || 1)
154
+ rescue Mailkube::APIError => e
155
+ warn "#{e.status_code} #{e.error_name}: #{e.message} (request #{e.request_id})"
156
+ end
157
+ ```
158
+
159
+ **Quote `request_id` when you report a failure.** Every API response carries an `X-Request-Id`, and
160
+ a mapped `APIError` exposes it. It is the only value that lets support find your exact call in the
161
+ server-side logs, so include it verbatim in the ticket. Setting `MAILKUBE_LOG=debug` also writes it
162
+ on the response line, which is what makes a failure traceable from your own logs.
163
+
164
+ Categories: `BadRequestError` (400), `AuthenticationError` (403), `NotFoundError` (404),
165
+ `ConflictError` (409), `InvalidRequestError` (422), `RateLimitError` (429), `ServerError` (5xx),
166
+ and `APIError` for anything else, which is also the parent of all of them. A transport failure
167
+ raises `ConnectionError` and is deliberately **not** an API error.
168
+
169
+ `Mailkube::ErrorName` lists the documented `name` values for discoverability. It is **not** a
170
+ closed set: `error_name` stays a plain String, so a value this release has never heard of is
171
+ reported verbatim rather than crashing. Scheduling adds `scheduling_not_included` (403),
172
+ `scheduled_email_not_found` (404) and `scheduled_email_not_pending` (422).
173
+
174
+ ## Webhooks
175
+
176
+ `Mailkube::Webhooks.verify` checks the signature and returns a typed event:
177
+
178
+ ```ruby
179
+ event = Mailkube::Webhooks.verify(
180
+ payload: request.raw_post, # the RAW bytes, never re-encoded JSON
181
+ headers: request.headers,
182
+ secret: ENV.fetch("MAILKUBE_WEBHOOK_SECRET")
183
+ )
184
+
185
+ case event
186
+ when Mailkube::Events::EmailBouncedEvent
187
+ puts event.data.bounce.reason
188
+ when Mailkube::Events::UnknownEvent
189
+ puts "newer than this SDK: #{event.type}"
190
+ end
191
+ ```
192
+
193
+ Verify against the bytes you received. Never parse the body and re-encode it first: JSON
194
+ round-tripping reorders keys and normalizes whitespace, and the signature will not match. In Rails
195
+ that means `request.raw_post`, not `params`. `request.headers` works directly — the SDK accepts any
196
+ header mapping, including Rack's CGI-env spelling.
197
+
198
+ `Mailkube::Webhooks.verify_signature` is the signature check alone, if you want to parse yourself.
199
+ `X-Webhook-Id` is stable across retries; deduplicate on it.
200
+
201
+ ### Endpoint registration
202
+
203
+ When you create an endpoint, or re-point an existing one, mailkube probes it with
204
+ `GET <url>?hub.mode=subscribe&hub.challenge=<token>` and persists it **only** if the body echoes
205
+ that token verbatim with a 200. Skip this and no event is ever delivered. Both webhook examples
206
+ implement it.
207
+
208
+ ### Event types
209
+
210
+ | Type | Envelope | Nested block |
211
+ |---|---|---|
212
+ | `email.sent` | `EmailSentEvent` | `data.sent` |
213
+ | `email.delivered` | `EmailDeliveredEvent` | `data.delivery` |
214
+ | `email.bounced` | `EmailBouncedEvent` | `data.bounce` |
215
+ | `email.delivery_delayed` | `EmailDeliveryDelayedEvent` | `data.delay` |
216
+ | `email.suppressed` | `EmailSuppressedEvent` | `data.suppression` |
217
+ | `email.scheduled` | `EmailScheduledEvent` | `data.scheduled` |
218
+ | `email.failed` | `EmailFailedEvent` | `data.failed` |
219
+ | `email.opened` | `EmailOpenedEvent` | `data.open` |
220
+ | `email.clicked` | `EmailClickedEvent` | `data.click` |
221
+ | `domain.status` | `DomainStatusEvent` | `data.previous` |
222
+ | `webhook.status` | `WebhookStatusEvent` | `data.previous` |
223
+
224
+ Every `email.*` payload also carries the message context: `email_id`, `created_at`, `domain`,
225
+ `subject`, `to`, `from` and `tags`.
226
+
227
+ Two guarantees hold for every released version, so a platform change never breaks a running
228
+ receiver:
229
+
230
+ - **An unknown event type is not an error.** It parses as `Mailkube::Events::UnknownEvent`, whose
231
+ `data` is the raw hash.
232
+ - **Unknown fields are preserved**, at every nesting depth. `event[...]` and `event.data[...]`
233
+ reach anything this release does not model, and `event.to_h` round-trips to exactly what the
234
+ server sent.
235
+
236
+ ## Logging
237
+
238
+ Silent by default. Turn it on with any object responding to `#write`:
239
+
240
+ ```ruby
241
+ Mailkube.enable_logging # $stderr
242
+ Mailkube.enable_logging(device: Rails.logger) # or your own
243
+ ```
244
+
245
+ Or set `MAILKUBE_LOG`. It holds a **level**, not an on/off flag, exactly as in the python, node, Go
246
+ and PHP SDKs: `MAILKUBE_LOG=debug` (or `trace`, or `all`) turns the SDK on, and anything more
247
+ selective silences it — `MAILKUBE_LOG=warning` is a working way to say "logs, but not from the
248
+ SDK". This SDK emits one class of record, the request/response trace, and that record is
249
+ debug-level. An unrecognized value leaves logging off rather than raising, because this is read at
250
+ `require` time.
251
+
252
+ The `Authorization` and `Idempotency-Key` headers are redacted from every line, and no recipient
253
+ address, subject or body is ever written. The response line carries the server's request id when
254
+ one was sent.
255
+
256
+ This is a duck-typed sink rather than a stdlib `Logger` on purpose: `logger` stopped being a
257
+ default gem in Ruby 4.0, so requiring it here would break the zero-dependency claim — and a library
258
+ has no business installing handlers on its host's behalf.
259
+
260
+ ## Concurrency
261
+
262
+ Create one client and reuse it. It is frozen after construction and safe to share across threads,
263
+ and across fibers where your application has installed a scheduler. Ruby releases the GVL around
264
+ socket I/O, so plain threads give you real HTTP concurrency with no setup.
265
+
266
+ Each request opens its own connection. That is deliberate: sharing one `Net::HTTP` across callers
267
+ does not raise, it lets two callers interleave on one socket and hands one of them the other's
268
+ response body. If you want pooling, inject an adapter that pools — and make it safe under threads
269
+ *and* under a fiber scheduler.
270
+
271
+ ## Supported surface
272
+
273
+ These are covered by semantic versioning:
274
+
275
+ - `Mailkube.new`, `Mailkube.enable_logging`
276
+ - `client.emails`, `client.scheduled_emails` and their verbs
277
+ - the response models and `Mailkube::Attachment` / `Mailkube::Tag`
278
+ - the error hierarchy and `Mailkube::ErrorName`
279
+ - `Mailkube::Webhooks` and everything under `Mailkube::Events`
280
+ - the `http:` adapter contract: `#call(method:, url:, headers:, body:)` returning a
281
+ `Mailkube::HttpResponse` and raising only `Mailkube::ConnectionError`
282
+
283
+ Everything else — `Transport`, `RequestSpec`, `Config`, and the internals of `NetHttpAdapter` — is
284
+ marked `@api private` and may change in any release. Build on the list above.
285
+
286
+ ### Type signatures
287
+
288
+ RBS signatures ship in `sig/` and are packaged with the gem, so Steep and RBS-aware editors type
289
+ your calls without extra setup.
290
+
291
+ ## More examples
292
+
293
+ Runnable scripts in [`examples/`](examples/):
294
+
295
+ - [`simple_send.rb`](examples/simple_send.rb) — the smallest useful program
296
+ - [`send_with_attachments.rb`](examples/send_with_attachments.rb) — attach a file from raw bytes
297
+ - [`send_with_tags.rb`](examples/send_with_tags.rb) — tag a send for filtering and reporting
298
+ - [`send_with_template.rb`](examples/send_with_template.rb) — send from a saved template
299
+ - [`schedule_send.rb`](examples/schedule_send.rb) — schedule a send, then inspect the ack
300
+ - [`manage_scheduled_emails.rb`](examples/manage_scheduled_emails.rb) — list, paginate, retrieve,
301
+ reschedule and cancel
302
+ - [`schedule_batch.rb`](examples/schedule_batch.rb) — schedule a batch, then move or cancel it as
303
+ a unit
304
+ - [`webhook_receiver_sinatra.rb`](examples/webhook_receiver_sinatra.rb) — verify and dispatch
305
+ webhooks in Sinatra
306
+ - [`webhook_receiver_rails.rb`](examples/webhook_receiver_rails.rb) — the same in Rails, including
307
+ `request.raw_post` and background dispatch
308
+
309
+ ## Extending this SDK
310
+
311
+ Before adding a verb, a resource, a paginated listing or a webhook event, read
312
+ [`.rules/SDK_CONTRACT.md`](.rules/SDK_CONTRACT.md) (the decisions every mailkube SDK shares) and
313
+ [`.rules/SDK_DESIGN.md`](.rules/SDK_DESIGN.md) (how they are realized in Ruby). Both carry a
314
+ step-by-step checklist, and every checklist ends with adding a runnable example.
315
+
316
+ ## Contributing
317
+
318
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for the development setup and the quality gates every change
319
+ must pass. Security issues: see [SECURITY.md](SECURITY.md).
320
+
321
+ ## License
322
+
323
+ [Apache-2.0](LICENSE) © 2026 Mailtactic, Corp.
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mailkube
4
+ # The API client, and this gem's composition root.
5
+ #
6
+ # Create one and reuse it. It is frozen after construction and safe to share across threads
7
+ # (and across fibers, where a scheduler is installed); `spec/concurrency_spec.rb` proves it
8
+ # rather than asserting it.
9
+ #
10
+ # client = Mailkube.new # reads MAILKUBE_API_KEY
11
+ # email = client.emails.send(
12
+ # from: "Acme <hello@yourdomain.com>",
13
+ # to: "customer@example.com",
14
+ # subject: "Hello world",
15
+ # html: "<p>It works!</p>"
16
+ # )
17
+ #
18
+ # There are deliberately no built-in retries. A {RateLimitError} carries `retry_after` and a
19
+ # {ServerError} is safe to retry with backoff, so the calling application decides. Pass
20
+ # `idempotency_key:` to make a retry safe.
21
+ #
22
+ # This client is **synchronous**, which is the contract's sync-only case: concurrency here is
23
+ # the caller's concern rather than an API-surface decision. See `.rules/SDK_DESIGN.md`.
24
+ class Client
25
+ # @return [Resources::Emails] the emails namespace.
26
+ attr_reader :emails
27
+ # @return [Resources::ScheduledEmails] the scheduled-emails namespace.
28
+ attr_reader :scheduled_emails
29
+
30
+ # Create a client, resolving configuration from the arguments then the environment.
31
+ #
32
+ # @param api_key [String, nil] the API key; falls back to `MAILKUBE_API_KEY`.
33
+ # @param base_url [String, nil] the API base URL; falls back to `MAILKUBE_BASE_URL`.
34
+ # @param timeout [Integer, Float] the per-request open and read timeout in seconds.
35
+ # @param http [#call, nil] an HTTP adapter to use instead of the built-in {NetHttpAdapter}.
36
+ # This is the dependency-inversion seam the test suite injects through; when supplied,
37
+ # `timeout` is the adapter's business rather than this client's.
38
+ # @raise [ConfigurationError] when no API key is available.
39
+ def initialize(api_key: nil, base_url: nil, timeout: Config::DEFAULT_TIMEOUT, http: nil)
40
+ config = Config.new(api_key: api_key, base_url: base_url, timeout: timeout)
41
+ transport = Transport.new(config, http || NetHttpAdapter.new(timeout: timeout))
42
+
43
+ @config = config
44
+ @emails = Resources::Emails.new(transport)
45
+ @scheduled_emails = Resources::ScheduledEmails.new(transport)
46
+ freeze
47
+ end
48
+
49
+ # @return [String] the resolved API base URL.
50
+ def base_url = @config.base_url
51
+ end
52
+ end