sendkit 1.0.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/README.md +72 -0
- data/lib/sendkit/client.rb +46 -0
- data/lib/sendkit/emails.rb +33 -0
- data/lib/sendkit/error.rb +13 -0
- data/lib/sendkit/version.rb +5 -0
- data/lib/sendkit.rb +6 -0
- metadata +50 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 45d843cea3a566a7e3089bb891825509074d890b0b7cc714723a46047df19d5a
|
|
4
|
+
data.tar.gz: a6adac6a88844352ac1b0924a17890998d16d441c6b2d927324895d1a29d06dd
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 9dd53e79481b91be5f1a9142aa3858545bfd84d62402d5fa7f2b9303b140084e607fcdf669d6ef8698383e9eeed7240f06ded52cf70a38e3474adb21d83ac77f
|
|
7
|
+
data.tar.gz: 9eafe75b279d011de7d87f7b1c59fb0226a67b5ecf90ecc424e0739c8e05e7c8f7373a88bd4dad7fc2dc52068319c594b706c85d2910fff5f84fa245eab7c264
|
data/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# SendKit Ruby SDK
|
|
2
|
+
|
|
3
|
+
Official Ruby SDK for the [SendKit](https://sendkit.com) email API.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
gem install sendkit
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Or add to your Gemfile:
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
gem "sendkit"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
### Create a Client
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
require "sendkit"
|
|
23
|
+
|
|
24
|
+
client = SendKit::Client.new("sk_your_api_key")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Send an Email
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
result = client.emails.send(
|
|
31
|
+
from: "you@example.com",
|
|
32
|
+
to: "recipient@example.com",
|
|
33
|
+
subject: "Hello from SendKit",
|
|
34
|
+
html: "<h1>Welcome!</h1>"
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
puts result["id"]
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
### Send a MIME Email
|
|
41
|
+
|
|
42
|
+
```ruby
|
|
43
|
+
result = client.emails.send_mime(
|
|
44
|
+
envelope_from: "you@example.com",
|
|
45
|
+
envelope_to: "recipient@example.com",
|
|
46
|
+
raw_message: mime_string
|
|
47
|
+
)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Error Handling
|
|
51
|
+
|
|
52
|
+
API errors raise `SendKit::Error`:
|
|
53
|
+
|
|
54
|
+
```ruby
|
|
55
|
+
begin
|
|
56
|
+
client.emails.send(...)
|
|
57
|
+
rescue SendKit::Error => e
|
|
58
|
+
puts e.name # e.g. "validation_error"
|
|
59
|
+
puts e.message # e.g. "The to field is required."
|
|
60
|
+
puts e.status_code # e.g. 422
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Configuration
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
# Read API key from SENDKIT_API_KEY environment variable
|
|
68
|
+
client = SendKit::Client.new
|
|
69
|
+
|
|
70
|
+
# Custom base URL
|
|
71
|
+
client = SendKit::Client.new("sk_...", base_url: "https://custom.api.com")
|
|
72
|
+
```
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require "net/http"
|
|
5
|
+
require "uri"
|
|
6
|
+
|
|
7
|
+
module SendKit
|
|
8
|
+
class Client
|
|
9
|
+
DEFAULT_BASE_URL = "https://api.sendkit.com"
|
|
10
|
+
|
|
11
|
+
attr_reader :emails
|
|
12
|
+
|
|
13
|
+
def initialize(api_key = nil, base_url: DEFAULT_BASE_URL)
|
|
14
|
+
@api_key = api_key || ENV["SENDKIT_API_KEY"]
|
|
15
|
+
|
|
16
|
+
raise ArgumentError, 'Missing API key. Pass it to the constructor `SendKit::Client.new("sk_...")` or set the SENDKIT_API_KEY environment variable.' if @api_key.nil? || @api_key.empty?
|
|
17
|
+
|
|
18
|
+
@base_url = base_url
|
|
19
|
+
@emails = Emails.new(self)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def post(path, body)
|
|
23
|
+
uri = URI("#{@base_url}#{path}")
|
|
24
|
+
request = Net::HTTP::Post.new(uri)
|
|
25
|
+
request["Authorization"] = "Bearer #{@api_key}"
|
|
26
|
+
request["Content-Type"] = "application/json"
|
|
27
|
+
request.body = JSON.generate(body)
|
|
28
|
+
|
|
29
|
+
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
|
|
30
|
+
http.request(request)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
parsed = JSON.parse(response.body)
|
|
34
|
+
|
|
35
|
+
unless response.is_a?(Net::HTTPSuccess)
|
|
36
|
+
raise Error.new(
|
|
37
|
+
parsed["message"] || response.message,
|
|
38
|
+
name: parsed["name"] || "application_error",
|
|
39
|
+
status_code: response.code.to_i
|
|
40
|
+
)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
parsed
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SendKit
|
|
4
|
+
class Emails
|
|
5
|
+
def initialize(client)
|
|
6
|
+
@client = client
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def send(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil,
|
|
10
|
+
reply_to: nil, headers: nil, tags: nil, scheduled_at: nil, attachments: nil)
|
|
11
|
+
payload = {from: from, to: to, subject: subject}
|
|
12
|
+
payload[:html] = html if html
|
|
13
|
+
payload[:text] = text if text
|
|
14
|
+
payload[:cc] = cc if cc
|
|
15
|
+
payload[:bcc] = bcc if bcc
|
|
16
|
+
payload[:reply_to] = reply_to if reply_to
|
|
17
|
+
payload[:headers] = headers if headers
|
|
18
|
+
payload[:tags] = tags if tags
|
|
19
|
+
payload[:scheduled_at] = scheduled_at if scheduled_at
|
|
20
|
+
payload[:attachments] = attachments if attachments
|
|
21
|
+
|
|
22
|
+
@client.post("/v1/emails", payload)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def send_mime(envelope_from:, envelope_to:, raw_message:)
|
|
26
|
+
@client.post("/v1/emails/mime", {
|
|
27
|
+
envelope_from: envelope_from,
|
|
28
|
+
envelope_to: envelope_to,
|
|
29
|
+
raw_message: raw_message
|
|
30
|
+
})
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module SendKit
|
|
4
|
+
class Error < StandardError
|
|
5
|
+
attr_reader :name, :status_code
|
|
6
|
+
|
|
7
|
+
def initialize(message, name: "application_error", status_code: nil)
|
|
8
|
+
super(message)
|
|
9
|
+
@name = name
|
|
10
|
+
@status_code = status_code
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
data/lib/sendkit.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: sendkit
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- SendKit
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: Official Ruby SDK for the SendKit email API. Send transactional emails
|
|
13
|
+
with a simple, zero-dependency client.
|
|
14
|
+
email:
|
|
15
|
+
- support@sendkit.com
|
|
16
|
+
executables: []
|
|
17
|
+
extensions: []
|
|
18
|
+
extra_rdoc_files: []
|
|
19
|
+
files:
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/sendkit.rb
|
|
22
|
+
- lib/sendkit/client.rb
|
|
23
|
+
- lib/sendkit/emails.rb
|
|
24
|
+
- lib/sendkit/error.rb
|
|
25
|
+
- lib/sendkit/version.rb
|
|
26
|
+
homepage: https://github.com/sendkitdev/sendkit-ruby
|
|
27
|
+
licenses:
|
|
28
|
+
- MIT
|
|
29
|
+
metadata:
|
|
30
|
+
homepage_uri: https://github.com/sendkitdev/sendkit-ruby
|
|
31
|
+
source_code_uri: https://github.com/sendkitdev/sendkit-ruby
|
|
32
|
+
changelog_uri: https://github.com/sendkitdev/sendkit-ruby/releases
|
|
33
|
+
rdoc_options: []
|
|
34
|
+
require_paths:
|
|
35
|
+
- lib
|
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ">="
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.1'
|
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: '0'
|
|
46
|
+
requirements: []
|
|
47
|
+
rubygems_version: 4.0.3
|
|
48
|
+
specification_version: 4
|
|
49
|
+
summary: Ruby SDK for the SendKit email API
|
|
50
|
+
test_files: []
|