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
- #checkDomainAvailability(domain) ⇒ Object
- #createDomain(body) ⇒ Object
- #createRoute(body) ⇒ Object
- #deleteDomain(id) ⇒ Object
- #deleteWebhook(id) ⇒ Object
- #getDomain(id) ⇒ Object
- #getMessage(id) ⇒ Object
-
#initialize(api_key, base_url = DEFAULT_BASE_URL) ⇒ Client
constructor
A new instance of Client.
-
#listDomains ⇒ Object
— Domains ——————————————————–.
-
#listMessages ⇒ Object
— Messages & deliveries —————————————–.
-
#listRoutes ⇒ Object
— Routes ———————————————————.
- #registerDomain(body) ⇒ Object
-
#request(method, path, body = nil) ⇒ Object
Low-level request.
- #retryDelivery(id) ⇒ Object
-
#send(message) ⇒ Object
— Sending ——————————————————–.
- #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
#checkDomainAvailability(domain) ⇒ Object
139 140 141 |
# File 'lib/mailkite.rb', line 139 def checkDomainAvailability(domain) request("GET", "/api/domains/register/check?domain=#{CGI.escape(domain)}") end |
#createDomain(body) ⇒ Object
111 112 113 |
# File 'lib/mailkite.rb', line 111 def createDomain(body) request("POST", "/api/domains", body) end |
#createRoute(body) ⇒ Object
152 153 154 |
# File 'lib/mailkite.rb', line 152 def createRoute(body) request("POST", "/api/routes", body) end |
#deleteDomain(id) ⇒ Object
119 120 121 |
# File 'lib/mailkite.rb', line 119 def deleteDomain(id) request("DELETE", "/api/domains/#{id}") end |
#deleteWebhook(id) ⇒ Object
131 132 133 |
# File 'lib/mailkite.rb', line 131 def deleteWebhook(id) request("DELETE", "/api/domains/#{id}/webhook") end |
#getDomain(id) ⇒ Object
115 116 117 |
# File 'lib/mailkite.rb', line 115 def getDomain(id) request("GET", "/api/domains/#{id}") end |
#getMessage(id) ⇒ Object
161 162 163 |
# File 'lib/mailkite.rb', line 161 def getMessage(id) request("GET", "/api/messages/#{id}") end |
#listDomains ⇒ Object
— Domains ——————————————————–
107 108 109 |
# File 'lib/mailkite.rb', line 107 def listDomains request("GET", "/api/domains") end |
#listMessages ⇒ Object
— Messages & deliveries —————————————–
157 158 159 |
# File 'lib/mailkite.rb', line 157 def listMessages request("GET", "/api/messages") end |
#listRoutes ⇒ Object
— Routes ———————————————————
148 149 150 |
# File 'lib/mailkite.rb', line 148 def listRoutes request("GET", "/api/routes") end |
#registerDomain(body) ⇒ Object
143 144 145 |
# File 'lib/mailkite.rb', line 143 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
165 166 167 |
# File 'lib/mailkite.rb', line 165 def retryDelivery(id) request("POST", "/api/deliveries/#{id}/retry") end |
#send(message) ⇒ Object
— Sending ——————————————————–
102 103 104 |
# File 'lib/mailkite.rb', line 102 def send() request("POST", "/v1/send", ) end |
#setWebhook(id, body) ⇒ Object
127 128 129 |
# File 'lib/mailkite.rb', line 127 def setWebhook(id, body) request("PUT", "/api/domains/#{id}/webhook", body) end |
#testWebhook(id) ⇒ Object
135 136 137 |
# File 'lib/mailkite.rb', line 135 def testWebhook(id) request("POST", "/api/domains/#{id}/webhook/test") end |
#verifyDomain(id) ⇒ Object
123 124 125 |
# File 'lib/mailkite.rb', line 123 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.
172 173 174 |
# File 'lib/mailkite.rb', line 172 def verifyWebhook(signature, payload, secret, tolerance_ms = DEFAULT_TOLERANCE_MS) Mailkite.verify_webhook(signature, payload, secret, tolerance_ms) end |