mailcatchr 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 +19 -0
- data/lib/mailcatchr/client.rb +199 -0
- data/lib/mailcatchr/version.rb +5 -0
- data/lib/mailcatchr.rb +4 -0
- metadata +53 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 6a2ca14ae31421f4ceda9e7c502d0a28204a44f6ea615cb8e6537fab40616b32
|
|
4
|
+
data.tar.gz: d14bd4cd6dc7583868a11bd63dab70c3421e85ee407478aae888788406b93d0b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 64da53362c5342ad858e1e64b7318c495e8a6744831c205223e916fe65440d62e2f481e309ac73ba3751374d488f237f640667f87cade3c2a8bf3f4f79a91d6f
|
|
7
|
+
data.tar.gz: 427474bc8948edbbe3beaf61eca3954ebc7c90cc9f0f3b048556b37fe0f07d9285fc52bdb366e644dff442ab4699df726308c72797d47c9a83b68020458c3e24
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Flahive Precision Engineering Pty. Ltd.
|
|
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,19 @@
|
|
|
1
|
+
# mailcatchr (Ruby)
|
|
2
|
+
|
|
3
|
+
Client for the [mailcatchr](https://mailcatchr.com/) API. Standard library only; Ruby 3.0+.
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
gem "mailcatchr"
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
mc = Mailcatchr::Client.new(api_key: ENV.fetch("MAILCATCHR_API_KEY"))
|
|
11
|
+
|
|
12
|
+
summary = mc.messages.wait_for(mailbox_id, to: address, subject: "Reset your password", timeout_seconds: 30)
|
|
13
|
+
raise "the reset email did not arrive" if summary.nil?
|
|
14
|
+
message = mc.messages.get(mailbox_id, summary.id)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Errors raise `Mailcatchr::ApiError` with `status` and `body`. `wait_for` returns `nil` on timeout. See https://docs.mailcatchr.com/reference/sdks/.
|
|
18
|
+
|
|
19
|
+
Tests: `ruby -Ilib -Itest test/client_test.rb`.
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
# Hand-written client over the mailcatchr v1 public API, mirroring the
|
|
8
|
+
# TypeScript SDK: mailboxes (list, create), messages (list, get, wait_for),
|
|
9
|
+
# send, and TOTP codes. Standard library only.
|
|
10
|
+
module Mailcatchr
|
|
11
|
+
DEFAULT_BASE_URL = "https://api.mailcatchr.com"
|
|
12
|
+
USER_AGENT = "mailcatchr-sdk-ruby/#{VERSION}"
|
|
13
|
+
|
|
14
|
+
# A non-2xx answer from the API. +body+ is the parsed JSON, the raw text, or nil.
|
|
15
|
+
class ApiError < StandardError
|
|
16
|
+
attr_reader :status, :body
|
|
17
|
+
|
|
18
|
+
def initialize(status, body)
|
|
19
|
+
super("mailcatchr API #{status}")
|
|
20
|
+
@status = status
|
|
21
|
+
@body = body
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
Mailbox = Struct.new(:id, :workspace_id, :group_id, :name, :subdomain, :retention_days, :outbound_enabled,
|
|
26
|
+
:daily_inbound_used, :created_at, keyword_init: true) do
|
|
27
|
+
def self.from_h(h)
|
|
28
|
+
new(id: h["id"], workspace_id: h["workspaceId"], group_id: h["groupId"], name: h["name"], subdomain: h["subdomain"],
|
|
29
|
+
retention_days: h["retentionDays"], outbound_enabled: h["outboundEnabled"], daily_inbound_used: h["dailyInboundUsed"],
|
|
30
|
+
created_at: h["createdAt"])
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
MessageSummary = Struct.new(:id, :from, :to, :subject, :received_at, :size_bytes, :attachment_count, keyword_init: true) do
|
|
35
|
+
def self.from_h(h)
|
|
36
|
+
new(id: h["id"], from: h["from"], to: h["to"], subject: h["subject"], received_at: h["receivedAt"],
|
|
37
|
+
size_bytes: h["sizeBytes"], attachment_count: h["attachmentCount"])
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
MessageAttachment = Struct.new(:id, :filename, :content_type, :content_id, :inline, :size_bytes, :virus_scan_status,
|
|
42
|
+
keyword_init: true) do
|
|
43
|
+
def self.from_h(h)
|
|
44
|
+
new(id: h["id"], filename: h["filename"], content_type: h["contentType"], content_id: h["contentId"],
|
|
45
|
+
inline: h["isInline"], size_bytes: h["sizeBytes"], virus_scan_status: h["virusScanStatus"].to_s)
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
MessageDetail = Struct.new(:id, :from, :to, :cc, :bcc, :reply_to, :subject, :text_body, :html_body, :headers_json,
|
|
50
|
+
:spf_result, :envelope_from, :remote_ip, :received_at, :size_bytes, :attachments,
|
|
51
|
+
keyword_init: true) do
|
|
52
|
+
def self.from_h(h)
|
|
53
|
+
new(id: h["id"], from: h["from"], to: h["to"], cc: h["cc"], bcc: h["bcc"], reply_to: h["replyTo"], subject: h["subject"],
|
|
54
|
+
text_body: h["textBody"], html_body: h["htmlBody"], headers_json: h["headersJson"], spf_result: h["spfResult"],
|
|
55
|
+
envelope_from: h["envelopeFrom"], remote_ip: h["remoteIp"], received_at: h["receivedAt"], size_bytes: h["sizeBytes"],
|
|
56
|
+
attachments: (h["attachments"] || []).map { |a| MessageAttachment.from_h(a) })
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
SendResult = Struct.new(:outbound_send_id, :message_id, :status, keyword_init: true) do
|
|
61
|
+
def self.from_h(h)
|
|
62
|
+
new(outbound_send_id: h["outboundSendId"], message_id: h["messageId"], status: h["status"])
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
TotpCode = Struct.new(:code, :expires_at, :expires_in_seconds, keyword_init: true) do
|
|
67
|
+
def self.from_h(h)
|
|
68
|
+
new(code: h["code"], expires_at: h["expiresAt"], expires_in_seconds: h["expiresInSeconds"])
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Query string for the params, dropping nil and empty values; "" when nothing remains.
|
|
73
|
+
def self.build_query(params)
|
|
74
|
+
kept = params.reject { |_, v| v.nil? || v.to_s.empty? }
|
|
75
|
+
kept.empty? ? "" : "?#{URI.encode_www_form(kept)}"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# JSON when it parses, the text otherwise, nil when empty.
|
|
79
|
+
def self.parse_error_body(raw)
|
|
80
|
+
return nil if raw.nil? || raw.empty?
|
|
81
|
+
|
|
82
|
+
JSON.parse(raw)
|
|
83
|
+
rescue JSON::ParserError
|
|
84
|
+
raw
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# @api private
|
|
88
|
+
class Transport
|
|
89
|
+
def initialize(api_key:, base_url:, timeout:)
|
|
90
|
+
@api_key = api_key
|
|
91
|
+
@base_url = base_url.sub(%r{/+\z}, "")
|
|
92
|
+
@timeout = timeout
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def request(method, path, body: nil, timeout: nil)
|
|
96
|
+
uri = URI.parse("#{@base_url}#{path}")
|
|
97
|
+
req = build(method, uri)
|
|
98
|
+
req["Authorization"] = "Bearer #{@api_key}"
|
|
99
|
+
req["Accept"] = "application/json"
|
|
100
|
+
req["Content-Type"] = "application/json"
|
|
101
|
+
req["User-Agent"] = USER_AGENT
|
|
102
|
+
req.body = JSON.generate(body) unless body.nil?
|
|
103
|
+
|
|
104
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
105
|
+
http.use_ssl = uri.scheme == "https"
|
|
106
|
+
http.open_timeout = 10
|
|
107
|
+
http.read_timeout = timeout || @timeout
|
|
108
|
+
res = http.request(req)
|
|
109
|
+
|
|
110
|
+
status = res.code.to_i
|
|
111
|
+
return nil if status == 204
|
|
112
|
+
raise ApiError.new(status, Mailcatchr.parse_error_body(res.body)) unless (200..299).cover?(status)
|
|
113
|
+
return nil if res.body.nil? || res.body.empty?
|
|
114
|
+
|
|
115
|
+
JSON.parse(res.body)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
private
|
|
119
|
+
|
|
120
|
+
def build(method, uri)
|
|
121
|
+
case method
|
|
122
|
+
when :get then Net::HTTP::Get.new(uri)
|
|
123
|
+
when :post then Net::HTTP::Post.new(uri)
|
|
124
|
+
else raise ArgumentError, "unsupported method #{method}"
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
class Mailboxes
|
|
130
|
+
def initialize(transport)
|
|
131
|
+
@t = transport
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# Every mailbox the key can read.
|
|
135
|
+
def list
|
|
136
|
+
@t.request(:get, "/v1/mailboxes").map { |m| Mailbox.from_h(m) }
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def create(workspace_id, name:, retention_days: nil, group_id: nil)
|
|
140
|
+
body = { "name" => name, "retentionDays" => retention_days, "groupId" => group_id }
|
|
141
|
+
Mailbox.from_h(@t.request(:post, "/v1/workspaces/#{workspace_id}/mailboxes/", body: body))
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
class Messages
|
|
146
|
+
def initialize(transport)
|
|
147
|
+
@t = transport
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def list(mailbox_id, limit: nil, before: nil)
|
|
151
|
+
qs = Mailcatchr.build_query("limit" => limit, "before" => before)
|
|
152
|
+
@t.request(:get, "/v1/mailboxes/#{mailbox_id}/messages#{qs}").map { |m| MessageSummary.from_h(m) }
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def get(mailbox_id, message_id)
|
|
156
|
+
MessageDetail.from_h(@t.request(:get, "/v1/mailboxes/#{mailbox_id}/messages/#{message_id}"))
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Long-polls until a message matching every filter arrives. Returns nil when the server's timeout fires first.
|
|
160
|
+
def wait_for(mailbox_id, to: nil, subject: nil, from: nil, since: nil, timeout_seconds: 30)
|
|
161
|
+
qs = Mailcatchr.build_query("subject" => subject, "from" => from, "to" => to, "since" => since,
|
|
162
|
+
"timeout" => timeout_seconds)
|
|
163
|
+
# The server holds the connection for up to timeout_seconds; leave headroom for the round trip.
|
|
164
|
+
body = @t.request(:get, "/v1/mailboxes/#{mailbox_id}/messages/await#{qs}", timeout: timeout_seconds + 15)
|
|
165
|
+
body.nil? ? nil : MessageSummary.from_h(body)
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
class Totps
|
|
170
|
+
def initialize(transport)
|
|
171
|
+
@t = transport
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
# The current code for a stored authenticator.
|
|
175
|
+
def code(totp_id)
|
|
176
|
+
TotpCode.from_h(@t.request(:get, "/v1/totp/#{totp_id}/code"))
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
class Client
|
|
181
|
+
attr_reader :mailboxes, :messages, :totps
|
|
182
|
+
|
|
183
|
+
def initialize(api_key:, base_url: DEFAULT_BASE_URL, timeout: 30)
|
|
184
|
+
raise ArgumentError, "api_key is required" if api_key.nil? || api_key.strip.empty?
|
|
185
|
+
|
|
186
|
+
@t = Transport.new(api_key: api_key, base_url: base_url, timeout: timeout)
|
|
187
|
+
@mailboxes = Mailboxes.new(@t)
|
|
188
|
+
@messages = Messages.new(@t)
|
|
189
|
+
@totps = Totps.new(@t)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Queues an outbound email from the mailbox.
|
|
193
|
+
def send_message(mailbox_id, from:, to:, subject:, text_body: nil, html_body: nil, cc: nil, bcc: nil, reply_to: nil)
|
|
194
|
+
body = { "from" => from, "to" => to, "cc" => cc, "bcc" => bcc, "replyTo" => reply_to, "subject" => subject,
|
|
195
|
+
"textBody" => text_body, "htmlBody" => html_body }
|
|
196
|
+
SendResult.from_h(@t.request(:post, "/v1/mailboxes/#{mailbox_id}/send", body: body))
|
|
197
|
+
end
|
|
198
|
+
end
|
|
199
|
+
end
|
data/lib/mailcatchr.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: mailcatchr
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- mailcatchr
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description:
|
|
14
|
+
email:
|
|
15
|
+
- support@mailcatchr.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- LICENSE
|
|
21
|
+
- README.md
|
|
22
|
+
- lib/mailcatchr.rb
|
|
23
|
+
- lib/mailcatchr/client.rb
|
|
24
|
+
- lib/mailcatchr/version.rb
|
|
25
|
+
homepage: https://docs.mailcatchr.com/
|
|
26
|
+
licenses:
|
|
27
|
+
- MIT
|
|
28
|
+
metadata:
|
|
29
|
+
homepage_uri: https://docs.mailcatchr.com/
|
|
30
|
+
documentation_uri: https://docs.mailcatchr.com/reference/sdks/
|
|
31
|
+
source_code_uri: https://github.com/mailcatchr/mailcatchr-sdk
|
|
32
|
+
bug_tracker_uri: https://github.com/mailcatchr/mailcatchr-sdk/issues
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '3.0'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.5.22
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: 'Client for the mailcatchr API: catchall test inboxes for automated email
|
|
52
|
+
testing.'
|
|
53
|
+
test_files: []
|