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
- #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 ———————————————————.
-
#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.
74 75 76 77 |
# File 'lib/mailkite.rb', line 74 def initialize(api_key, base_url = DEFAULT_BASE_URL) @api_key = api_key @base_url = base_url.sub(%r{/+\z}, "") end |
Instance Method Details
#createDomain(body) ⇒ Object
110 111 112 |
# File 'lib/mailkite.rb', line 110 def createDomain(body) request("POST", "/api/domains", body) end |
#createRoute(body) ⇒ Object
143 144 145 |
# File 'lib/mailkite.rb', line 143 def createRoute(body) request("POST", "/api/routes", body) end |
#deleteDomain(id) ⇒ Object
118 119 120 |
# File 'lib/mailkite.rb', line 118 def deleteDomain(id) request("DELETE", "/api/domains/#{id}") end |
#deleteWebhook(id) ⇒ Object
130 131 132 |
# File 'lib/mailkite.rb', line 130 def deleteWebhook(id) request("DELETE", "/api/domains/#{id}/webhook") end |
#getDomain(id) ⇒ Object
114 115 116 |
# File 'lib/mailkite.rb', line 114 def getDomain(id) request("GET", "/api/domains/#{id}") end |
#getMessage(id) ⇒ Object
152 153 154 |
# File 'lib/mailkite.rb', line 152 def getMessage(id) request("GET", "/api/messages/#{id}") end |
#listDomains ⇒ Object
— Domains ——————————————————–
106 107 108 |
# File 'lib/mailkite.rb', line 106 def listDomains request("GET", "/api/domains") end |
#listMessages ⇒ Object
— Messages & deliveries —————————————–
148 149 150 |
# File 'lib/mailkite.rb', line 148 def listMessages request("GET", "/api/messages") end |
#listRoutes ⇒ Object
— Routes ———————————————————
139 140 141 |
# File 'lib/mailkite.rb', line 139 def listRoutes request("GET", "/api/routes") end |
#request(method, path, body = nil) ⇒ Object
Low-level request. Every method below is a one-liner on top of this.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/mailkite.rb', line 80 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
156 157 158 |
# File 'lib/mailkite.rb', line 156 def retryDelivery(id) request("POST", "/api/deliveries/#{id}/retry") end |
#send(message) ⇒ Object
— Sending ——————————————————–
101 102 103 |
# File 'lib/mailkite.rb', line 101 def send() request("POST", "/v1/send", ) end |
#setWebhook(id, body) ⇒ Object
126 127 128 |
# File 'lib/mailkite.rb', line 126 def setWebhook(id, body) request("PUT", "/api/domains/#{id}/webhook", body) end |
#testWebhook(id) ⇒ Object
134 135 136 |
# File 'lib/mailkite.rb', line 134 def testWebhook(id) request("POST", "/api/domains/#{id}/webhook/test") end |
#verifyDomain(id) ⇒ Object
122 123 124 |
# File 'lib/mailkite.rb', line 122 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.
163 164 165 |
# File 'lib/mailkite.rb', line 163 def verifyWebhook(signature, payload, secret, tolerance_ms = DEFAULT_TOLERANCE_MS) Mailkite.verify_webhook(signature, payload, secret, tolerance_ms) end |