Class: Mailkite::Client
- Inherits:
-
Object
- Object
- Mailkite::Client
- Defined in:
- lib/mailkite.rb
Constant Summary collapse
- VERBS =
{ "GET" => Net::HTTP::Get, "POST" => Net::HTTP::Post, "PUT" => Net::HTTP::Put, "DELETE" => Net::HTTP::Delete, }.freeze
Instance Method Summary collapse
-
#agent(message) ⇒ Object
Run an inbound message through an AI agent.
- #checkDomainAvailability(domain) ⇒ Object
- #createDomain(body) ⇒ Object
- #createRoute(body) ⇒ Object
- #createTemplate(body) ⇒ Object
- #decrypt(envelope, private_key) ⇒ Object
- #deleteDomain(id) ⇒ Object
- #deleteWebhook(id) ⇒ Object
- #encrypt(plaintext, public_key) ⇒ Object
- #getDomain(id) ⇒ Object
- #getMessage(id) ⇒ Object
- #getTemplate(id) ⇒ Object
-
#initialize(api_key, base_url = DEFAULT_BASE_URL) ⇒ Client
constructor
A new instance of Client.
- #listBaseTemplates ⇒ Object
-
#listDomains ⇒ Object
— Domains ——————————————————–.
-
#listMessages ⇒ Object
— Messages & deliveries —————————————–.
-
#listRoutes ⇒ Object
— Routes ———————————————————.
-
#listTemplates ⇒ Object
— Templates ——————————————————.
- #registerDomain(body) ⇒ Object
- #reply_block_sender ⇒ Object
- #reply_drop ⇒ Object
-
#reply_ok ⇒ Object
Instance wrappers around the module-level crypto helpers.
- #reply_spam ⇒ Object
-
#request(method, path, body = nil) ⇒ Object
Low-level request.
- #retryDelivery(id) ⇒ Object
-
#route(message) ⇒ Object
Route an inbound message to its configured destination.
-
#send(message) ⇒ Object
— Sending ——————————————————– message keys: from, to, text/html, etc.
- #setWebhook(id, body) ⇒ Object
- #testWebhook(id) ⇒ Object
-
#uploadAttachment(file) ⇒ Object
Upload a file (base64 ‘content`) and get back a secure, time-limited URL to reference as a send() attachment ({ filename, url }) or link inline — instead of base64-inlining large files on every send.
- #verifyDomain(id) ⇒ Object
-
#verifyWebhook(signature, payload, secret, tolerance_ms = DEFAULT_TOLERANCE_MS) ⇒ Object
— Webhooks ——————————————————- Instance wrapper around Mailkite.verify_webhook, so you can verify on an existing client.
Constructor Details
#initialize(api_key, base_url = DEFAULT_BASE_URL) ⇒ Client
Returns a new instance of Client.
153 154 155 156 |
# File 'lib/mailkite.rb', line 153 def initialize(api_key, base_url = DEFAULT_BASE_URL) @api_key = api_key @base_url = base_url.sub(%r{/+\z}, "") end |
Instance Method Details
#agent(message) ⇒ Object
Run an inbound message through an AI agent. message keys: text (required), plus optional subject/from/html/routeId/address/model.
197 198 199 |
# File 'lib/mailkite.rb', line 197 def agent() request("POST", "/v1/agent", ) end |
#checkDomainAvailability(domain) ⇒ Object
240 241 242 |
# File 'lib/mailkite.rb', line 240 def checkDomainAvailability(domain) request("GET", "/api/domains/register/check?domain=#{CGI.escape(domain)}") end |
#createDomain(body) ⇒ Object
212 213 214 |
# File 'lib/mailkite.rb', line 212 def createDomain(body) request("POST", "/api/domains", body) end |
#createRoute(body) ⇒ Object
253 254 255 |
# File 'lib/mailkite.rb', line 253 def createRoute(body) request("POST", "/api/routes", body) end |
#createTemplate(body) ⇒ Object
270 271 272 |
# File 'lib/mailkite.rb', line 270 def createTemplate(body) request("POST", "/api/templates", body) end |
#decrypt(envelope, private_key) ⇒ Object
315 316 317 |
# File 'lib/mailkite.rb', line 315 def decrypt(envelope, private_key) Mailkite.decrypt(envelope, private_key) end |
#deleteDomain(id) ⇒ Object
220 221 222 |
# File 'lib/mailkite.rb', line 220 def deleteDomain(id) request("DELETE", "/api/domains/#{id}") end |
#deleteWebhook(id) ⇒ Object
232 233 234 |
# File 'lib/mailkite.rb', line 232 def deleteWebhook(id) request("DELETE", "/api/domains/#{id}/webhook") end |
#encrypt(plaintext, public_key) ⇒ Object
311 312 313 |
# File 'lib/mailkite.rb', line 311 def encrypt(plaintext, public_key) Mailkite.encrypt(plaintext, public_key) end |
#getDomain(id) ⇒ Object
216 217 218 |
# File 'lib/mailkite.rb', line 216 def getDomain(id) request("GET", "/api/domains/#{id}") end |
#getMessage(id) ⇒ Object
279 280 281 |
# File 'lib/mailkite.rb', line 279 def getMessage(id) request("GET", "/api/messages/#{id}") end |
#getTemplate(id) ⇒ Object
266 267 268 |
# File 'lib/mailkite.rb', line 266 def getTemplate(id) request("GET", "/api/templates/#{id}") end |
#listBaseTemplates ⇒ Object
262 263 264 |
# File 'lib/mailkite.rb', line 262 def listBaseTemplates request("GET", "/api/templates/base") end |
#listDomains ⇒ Object
— Domains ——————————————————–
208 209 210 |
# File 'lib/mailkite.rb', line 208 def listDomains request("GET", "/api/domains") end |
#listMessages ⇒ Object
— Messages & deliveries —————————————–
275 276 277 |
# File 'lib/mailkite.rb', line 275 def listMessages request("GET", "/api/messages") end |
#listRoutes ⇒ Object
— Routes ———————————————————
249 250 251 |
# File 'lib/mailkite.rb', line 249 def listRoutes request("GET", "/api/routes") end |
#listTemplates ⇒ Object
— Templates ——————————————————
258 259 260 |
# File 'lib/mailkite.rb', line 258 def listTemplates request("GET", "/api/templates") end |
#registerDomain(body) ⇒ Object
244 245 246 |
# File 'lib/mailkite.rb', line 244 def registerDomain(body) request("POST", "/api/domains/register", body) end |
#reply_block_sender ⇒ Object
307 308 309 |
# File 'lib/mailkite.rb', line 307 def reply_block_sender Mailkite.reply_block_sender end |
#reply_drop ⇒ Object
303 304 305 |
# File 'lib/mailkite.rb', line 303 def reply_drop Mailkite.reply_drop end |
#reply_ok ⇒ Object
Instance wrappers around the module-level crypto helpers. No network call.
295 296 297 |
# File 'lib/mailkite.rb', line 295 def reply_ok Mailkite.reply_ok end |
#reply_spam ⇒ Object
299 300 301 |
# File 'lib/mailkite.rb', line 299 def reply_spam Mailkite.reply_spam end |
#request(method, path, body = nil) ⇒ Object
Low-level request. Every method below is a one-liner on top of this.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/mailkite.rb', line 159 def request(method, path, body = nil) uri = URI(@base_url + path) req = VERBS.fetch(method).new(uri) req["Authorization"] = "Bearer #{@api_key}" unless body.nil? req["Content-Type"] = "application/json" req.body = JSON.generate(body) end res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") { |http| http.request(req) } text = res.body data = text && !text.empty? ? JSON.parse(text) : nil code = res.code.to_i unless code >= 200 && code < 300 = data.is_a?(Hash) ? data["error"] : nil raise Error.new(code, || res. || "HTTP #{code}", data) end data end |
#retryDelivery(id) ⇒ Object
283 284 285 |
# File 'lib/mailkite.rb', line 283 def retryDelivery(id) request("POST", "/api/deliveries/#{id}/retry") end |
#route(message) ⇒ Object
Route an inbound message to its configured destination. message keys: from (required), plus optional routeId/address/subject/text/html.
203 204 205 |
# File 'lib/mailkite.rb', line 203 def route() request("POST", "/v1/route", ) end |
#send(message) ⇒ Object
— Sending ——————————————————– message keys: from, to, text/html, etc. ‘subject` is optional when a template supplies it. Pass `templateId` to render a stored template and `templateData` (a hash) to fill its variables.
183 184 185 |
# File 'lib/mailkite.rb', line 183 def send() request("POST", "/v1/send", ) end |
#setWebhook(id, body) ⇒ Object
228 229 230 |
# File 'lib/mailkite.rb', line 228 def setWebhook(id, body) request("PUT", "/api/domains/#{id}/webhook", body) end |
#testWebhook(id) ⇒ Object
236 237 238 |
# File 'lib/mailkite.rb', line 236 def testWebhook(id) request("POST", "/api/domains/#{id}/webhook/test") end |
#uploadAttachment(file) ⇒ Object
Upload a file (base64 ‘content`) and get back a secure, time-limited URL to reference as a send() attachment ({ filename, url }) or link inline —instead of base64-inlining large files on every send. `file` keys: filename, content (base64), plus optional contentType and retentionDays.
191 192 193 |
# File 'lib/mailkite.rb', line 191 def uploadAttachment(file) request("POST", "/v1/attachments", file) end |
#verifyDomain(id) ⇒ Object
224 225 226 |
# File 'lib/mailkite.rb', line 224 def verifyDomain(id) request("POST", "/api/domains/#{id}/verify") end |
#verifyWebhook(signature, payload, secret, tolerance_ms = DEFAULT_TOLERANCE_MS) ⇒ Object
— Webhooks ——————————————————- Instance wrapper around Mailkite.verify_webhook, so you can verify on an existing client. No network call; no API key required.
290 291 292 |
# File 'lib/mailkite.rb', line 290 def verifyWebhook(signature, payload, secret, tolerance_ms = DEFAULT_TOLERANCE_MS) Mailkite.verify_webhook(signature, payload, secret, tolerance_ms) end |