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
- #deleteDomain(id) ⇒ Object
- #deleteWebhook(id) ⇒ 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
-
#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
- #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.
75 76 77 78 |
# File 'lib/mailkite.rb', line 75 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.
111 112 113 |
# File 'lib/mailkite.rb', line 111 def agent() request("POST", "/v1/agent", ) end |
#checkDomainAvailability(domain) ⇒ Object
154 155 156 |
# File 'lib/mailkite.rb', line 154 def checkDomainAvailability(domain) request("GET", "/api/domains/register/check?domain=#{CGI.escape(domain)}") end |
#createDomain(body) ⇒ Object
126 127 128 |
# File 'lib/mailkite.rb', line 126 def createDomain(body) request("POST", "/api/domains", body) end |
#createRoute(body) ⇒ Object
167 168 169 |
# File 'lib/mailkite.rb', line 167 def createRoute(body) request("POST", "/api/routes", body) end |
#createTemplate(body) ⇒ Object
184 185 186 |
# File 'lib/mailkite.rb', line 184 def createTemplate(body) request("POST", "/api/templates", body) end |
#deleteDomain(id) ⇒ Object
134 135 136 |
# File 'lib/mailkite.rb', line 134 def deleteDomain(id) request("DELETE", "/api/domains/#{id}") end |
#deleteWebhook(id) ⇒ Object
146 147 148 |
# File 'lib/mailkite.rb', line 146 def deleteWebhook(id) request("DELETE", "/api/domains/#{id}/webhook") end |
#getDomain(id) ⇒ Object
130 131 132 |
# File 'lib/mailkite.rb', line 130 def getDomain(id) request("GET", "/api/domains/#{id}") end |
#getMessage(id) ⇒ Object
193 194 195 |
# File 'lib/mailkite.rb', line 193 def getMessage(id) request("GET", "/api/messages/#{id}") end |
#getTemplate(id) ⇒ Object
180 181 182 |
# File 'lib/mailkite.rb', line 180 def getTemplate(id) request("GET", "/api/templates/#{id}") end |
#listBaseTemplates ⇒ Object
176 177 178 |
# File 'lib/mailkite.rb', line 176 def listBaseTemplates request("GET", "/api/templates/base") end |
#listDomains ⇒ Object
— Domains ——————————————————–
122 123 124 |
# File 'lib/mailkite.rb', line 122 def listDomains request("GET", "/api/domains") end |
#listMessages ⇒ Object
— Messages & deliveries —————————————–
189 190 191 |
# File 'lib/mailkite.rb', line 189 def listMessages request("GET", "/api/messages") end |
#listRoutes ⇒ Object
— Routes ———————————————————
163 164 165 |
# File 'lib/mailkite.rb', line 163 def listRoutes request("GET", "/api/routes") end |
#listTemplates ⇒ Object
— Templates ——————————————————
172 173 174 |
# File 'lib/mailkite.rb', line 172 def listTemplates request("GET", "/api/templates") end |
#registerDomain(body) ⇒ Object
158 159 160 |
# File 'lib/mailkite.rb', line 158 def registerDomain(body) request("POST", "/api/domains/register", body) end |
#request(method, path, body = nil) ⇒ Object
Low-level request. Every method below is a one-liner on top of this.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mailkite.rb', line 81 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
197 198 199 |
# File 'lib/mailkite.rb', line 197 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.
117 118 119 |
# File 'lib/mailkite.rb', line 117 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.
105 106 107 |
# File 'lib/mailkite.rb', line 105 def send() request("POST", "/v1/send", ) end |
#setWebhook(id, body) ⇒ Object
142 143 144 |
# File 'lib/mailkite.rb', line 142 def setWebhook(id, body) request("PUT", "/api/domains/#{id}/webhook", body) end |
#testWebhook(id) ⇒ Object
150 151 152 |
# File 'lib/mailkite.rb', line 150 def testWebhook(id) request("POST", "/api/domains/#{id}/webhook/test") end |
#verifyDomain(id) ⇒ Object
138 139 140 |
# File 'lib/mailkite.rb', line 138 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.
204 205 206 |
# File 'lib/mailkite.rb', line 204 def verifyWebhook(signature, payload, secret, tolerance_ms = DEFAULT_TOLERANCE_MS) Mailkite.verify_webhook(signature, payload, secret, tolerance_ms) end |