mailkite 0.12.0 → 0.13.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 +4 -4
- data/lib/mailkite.rb +17 -5
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1271ec234bd17506cc9aa35f15262d7708bb827786df2da37065457b90e0f106
|
|
4
|
+
data.tar.gz: 490ce824dc5ff8097190a893db918f906945c4239822387a9e39f10e4fa85f29
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 621ff2176d573d2df21262f249e018b5e0acce0ab79d296ecc40ee9e2e5d031c60009bd7514ea292fa9bb4a76183ceecbb4d573837e6388b3194e8b4231bd167
|
|
7
|
+
data.tar.gz: 3515d606332f77ba60b3304f0d7945cf3e4cfaf77799ea719b9a3d6b20c9f3736f7bb12152020b3d5d31a35ce4a84157e3c5e4418467015d5d5861fc43262e84
|
data/lib/mailkite.rb
CHANGED
|
@@ -15,7 +15,7 @@ require "base64"
|
|
|
15
15
|
require "cgi"
|
|
16
16
|
|
|
17
17
|
module Mailkite
|
|
18
|
-
VERSION = "0.
|
|
18
|
+
VERSION = "0.13.0"
|
|
19
19
|
DEFAULT_BASE_URL = "https://api.mailkite.dev"
|
|
20
20
|
# Reject webhook events older than this (ms) to block replays. Pass 0 to disable.
|
|
21
21
|
DEFAULT_TOLERANCE_MS = 5 * 60 * 1000
|
|
@@ -151,16 +151,28 @@ module Mailkite
|
|
|
151
151
|
"DELETE" => Net::HTTP::Delete,
|
|
152
152
|
}.freeze
|
|
153
153
|
|
|
154
|
-
|
|
155
|
-
|
|
154
|
+
# Authenticate with a Bearer token. `api_key` (mk_live_…) and the `access_token:` keyword (an
|
|
155
|
+
# OAuth access token) are equivalent — both are Bearer credentials. For short-lived OAuth
|
|
156
|
+
# tokens pass `get_token:` — a callable invoked before each request for a fresh token (it wins):
|
|
157
|
+
# MailKite::Client.new("mk_live_...")
|
|
158
|
+
# MailKite::Client.new(access_token: my_oauth_token)
|
|
159
|
+
# MailKite::Client.new(get_token: -> { current_session.access_token })
|
|
160
|
+
def initialize(api_key = nil, base_url = DEFAULT_BASE_URL, access_token: nil, get_token: nil)
|
|
161
|
+
@api_key = api_key.nil? ? access_token : api_key
|
|
162
|
+
@get_token = get_token
|
|
156
163
|
@base_url = base_url.sub(%r{/+\z}, "")
|
|
157
164
|
end
|
|
158
165
|
|
|
166
|
+
# The Bearer token for one request: a fresh one from get_token if set, else the static key/token.
|
|
167
|
+
def token
|
|
168
|
+
@get_token ? @get_token.call : @api_key
|
|
169
|
+
end
|
|
170
|
+
|
|
159
171
|
# Low-level request. Every method below is a one-liner on top of this.
|
|
160
172
|
def request(method, path, body = nil)
|
|
161
173
|
uri = URI(@base_url + path)
|
|
162
174
|
req = VERBS.fetch(method).new(uri)
|
|
163
|
-
req["Authorization"] = "Bearer #{
|
|
175
|
+
req["Authorization"] = "Bearer #{token}"
|
|
164
176
|
unless body.nil?
|
|
165
177
|
req["Content-Type"] = "application/json"
|
|
166
178
|
req.body = JSON.generate(body)
|
|
@@ -178,7 +190,7 @@ module Mailkite
|
|
|
178
190
|
qs = query.map { |k, v| "#{CGI.escape(k)}=#{CGI.escape(v.to_s)}" }.join("&")
|
|
179
191
|
uri = URI("#{@base_url}/v1/attachments?#{qs}")
|
|
180
192
|
req = Net::HTTP::Post.new(uri)
|
|
181
|
-
req["Authorization"] = "Bearer #{
|
|
193
|
+
req["Authorization"] = "Bearer #{token}"
|
|
182
194
|
req["Content-Type"] = content_type
|
|
183
195
|
req.body = bytes
|
|
184
196
|
perform(req, uri)
|