stalsend-sdk 0.1.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +12 -0
- data/lib/stalsend.rb +58 -0
- metadata +39 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 860605716f42034fdea8a9323a8b527432e2d59d94fb2d8078a154af23e11e81
|
|
4
|
+
data.tar.gz: 5407f82c04541c1c617427b2c23f48e20441b8053a0d20d51d443c4d646cf3bb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 32bc13aa81e1f91d1cdd9ca92221b1531ab380fb5e04702a91b3d8e551cd974da032a9f867593766ccbc6a7f49e021624330fd98576033381799fc301cca75ad
|
|
7
|
+
data.tar.gz: 860e766d96c4065e48f563383fe93b9bb2dc58c8502722016802a1c3efd62b4bbe2570889091602c7fd8b927992dcf3f744ef11dd41e9ae1a7e8d993e0676411
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 StalSend
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# StalSend Ruby SDK
|
|
2
|
+
|
|
3
|
+
Server-side Ruby client for transactional email, batch sending, email logs, usage, and OTP. Requires Ruby 2.7+; uses the standard library.
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
require "stalsend"
|
|
7
|
+
client = StalSend::Client.new(api_key: ENV.fetch("STALSEND_API_KEY"))
|
|
8
|
+
client.send_email({to: "customer@example.com", subject: "Order update", text: "Your order shipped."})
|
|
9
|
+
client.email_logs(limit: 20)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Batch sending accepts 1–100 messages and generates a distinct idempotency key per message. Reuse a single message's original key when retrying. Keep keys server-side. Package name prepared as `stalsend-sdk`; publishing requires the StalSend RubyGems account.
|
data/lib/stalsend.rb
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "uri"
|
|
4
|
+
require "securerandom"
|
|
5
|
+
|
|
6
|
+
module StalSend
|
|
7
|
+
class Error < StandardError
|
|
8
|
+
attr_reader :status, :code, :retry_after
|
|
9
|
+
def initialize(status, code, retry_after = nil)
|
|
10
|
+
super(code); @status = status; @code = code; @retry_after = retry_after
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Client
|
|
15
|
+
def initialize(api_key:, base_url: "https://api.stalsend.com/api/v1", timeout: 15)
|
|
16
|
+
raise ArgumentError, "api_key is required" if api_key.to_s.strip.empty?
|
|
17
|
+
@api_key, @base_url, @timeout = api_key.strip, base_url.sub(%r{/+$}, ""), timeout
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def send_email(message, idempotency_key: SecureRandom.urlsafe_base64(24, false))
|
|
21
|
+
request("/email/send", :post, message, "Idempotency-Key" => idempotency_key)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def send_batch(messages)
|
|
25
|
+
raise ArgumentError, "send_batch requires 1..100 messages" unless (1..100).cover?(messages.length)
|
|
26
|
+
batch = messages.map { |m| m.transform_keys(&:to_s).merge("idempotencyKey" => m["idempotencyKey"] || m[:idempotencyKey] || SecureRandom.urlsafe_base64(24, false)) }
|
|
27
|
+
request("/email/batch", :post, {"messages" => batch})
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def email_logs(limit: 50, cursor: nil, status: nil, query: nil)
|
|
31
|
+
params = {"limit" => limit, "cursor" => cursor, "status" => status, "q" => query}.compact
|
|
32
|
+
request("/email/logs?#{URI.encode_www_form(params)}", :get)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def usage; request("/usage", :get); end
|
|
36
|
+
def send_otp(email:, purpose:, context:); request("/otp/send", :post, {email: email, purpose: purpose, context: context}); end
|
|
37
|
+
def resend_otp(challenge_id:); request("/otp/resend", :post, {challengeId: challenge_id}); end
|
|
38
|
+
def verify_otp(challenge_id:, code:, purpose:, context:); request("/otp/verify", :post, {challengeId: challenge_id, code: code, purpose: purpose, context: context}); end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
def request(path, method, body = nil, extra_headers = {})
|
|
42
|
+
uri = URI("#{@base_url}#{path}")
|
|
43
|
+
klass = method == :post ? Net::HTTP::Post : Net::HTTP::Get
|
|
44
|
+
req = klass.new(uri)
|
|
45
|
+
req["Authorization"] = "Bearer #{@api_key}"
|
|
46
|
+
req["Accept"] = "application/json"
|
|
47
|
+
req["Content-Type"] = "application/json" if body
|
|
48
|
+
extra_headers.each { |k, v| req[k] = v }
|
|
49
|
+
req.body = JSON.generate(body) if body
|
|
50
|
+
http = Net::HTTP.new(uri.host, uri.port); http.use_ssl = uri.scheme == "https"; http.read_timeout = @timeout
|
|
51
|
+
res = http.request(req)
|
|
52
|
+
return nil if res.code.to_i == 204
|
|
53
|
+
payload = JSON.parse(res.body.empty? ? "{}" : res.body) rescue {}
|
|
54
|
+
raise Error.new(res.code.to_i, payload.fetch("error", "request_failed"), res["Retry-After"]) unless res.is_a?(Net::HTTPSuccess)
|
|
55
|
+
payload
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: stalsend-sdk
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- StalSend
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
executables: []
|
|
13
|
+
extensions: []
|
|
14
|
+
extra_rdoc_files: []
|
|
15
|
+
files:
|
|
16
|
+
- LICENSE
|
|
17
|
+
- README.md
|
|
18
|
+
- lib/stalsend.rb
|
|
19
|
+
licenses:
|
|
20
|
+
- MIT
|
|
21
|
+
metadata: {}
|
|
22
|
+
rdoc_options: []
|
|
23
|
+
require_paths:
|
|
24
|
+
- lib
|
|
25
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
26
|
+
requirements:
|
|
27
|
+
- - ">="
|
|
28
|
+
- !ruby/object:Gem::Version
|
|
29
|
+
version: '2.7'
|
|
30
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
31
|
+
requirements:
|
|
32
|
+
- - ">="
|
|
33
|
+
- !ruby/object:Gem::Version
|
|
34
|
+
version: '0'
|
|
35
|
+
requirements: []
|
|
36
|
+
rubygems_version: 4.0.20
|
|
37
|
+
specification_version: 4
|
|
38
|
+
summary: Official Ruby client for the StalSend email API
|
|
39
|
+
test_files: []
|