unsent 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 95a31fbac0136fab785027f7812e7b3122ea28f79dcda16dd8bcf30782d6a788
4
+ data.tar.gz: 893a89d9dba0d1ae63f1c96fe62d30b99bee7029cda09aa8d4d88172f3875b53
5
+ SHA512:
6
+ metadata.gz: ee78bacbfaa8d1cafe58412d8fef02d342bc01542e255257c5bc2e73ea1a94d9d2a64bdf89939c786fd0f6d4c30c4ce58a6749118c1181ebb41661fe99bff98c
7
+ data.tar.gz: 2ea07e0707bc12b3db46e94470521d315df377f35fd6e2b060b10ca9fedb8e38619001abfc5022d78f890f7cb238a4d1f0110e30de26a98783a17c9f5a29dd04
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-11-19
4
+
5
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 TODO: Write your name
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,275 @@
1
+ # Unsent Ruby SDK
2
+
3
+ Official Ruby SDK for the [Unsent API](https://unsent.dev) - Send transactional emails with ease.
4
+
5
+ ## Prerequisites
6
+
7
+ - [Unsent API key](https://app.unsent.dev/dev-settings/api-keys)
8
+ - [Verified domain](https://app.unsent.dev/domains)
9
+ - Ruby 3.0 or higher
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'unsent'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```bash
22
+ bundle install
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```bash
28
+ gem install unsent
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ### Basic Setup
34
+
35
+ ```ruby
36
+ require 'unsent'
37
+
38
+ client = Unsent::Client.new('us_xxx')
39
+ ```
40
+
41
+ ### Environment Variables
42
+
43
+ You can also set your API key using environment variables:
44
+
45
+ ```ruby
46
+ # Set UNSENT_API_KEY in your environment
47
+ # Then initialize without passing the key
48
+ client = Unsent::Client.new
49
+ ```
50
+
51
+ ### Sending Emails
52
+
53
+ #### Simple Email
54
+
55
+ ```ruby
56
+ data, error = client.emails.send({
57
+ to: 'user@example.com',
58
+ from: 'no-reply@yourdomain.com',
59
+ subject: 'Welcome',
60
+ html: '<strong>Hello!</strong>'
61
+ })
62
+
63
+ if error
64
+ puts "Error: #{error}"
65
+ else
66
+ puts "Email sent! ID: #{data['id']}"
67
+ end
68
+ ```
69
+
70
+ #### With Attachments and Scheduling
71
+
72
+ ```ruby
73
+ require 'time'
74
+
75
+ data, error = client.emails.create({
76
+ to: 'user@example.com',
77
+ from: 'no-reply@yourdomain.com',
78
+ subject: 'Report',
79
+ text: 'See attached.',
80
+ attachments: [
81
+ {
82
+ filename: 'report.txt',
83
+ content: 'SGVsbG8gd29ybGQ=' # base64
84
+ }
85
+ ],
86
+ scheduledAt: (Time.now + 600).iso8601 # 10 minutes from now
87
+ })
88
+ ```
89
+
90
+ #### Batch Send
91
+
92
+ ```ruby
93
+ emails = [
94
+ {
95
+ to: 'a@example.com',
96
+ from: 'no-reply@yourdomain.com',
97
+ subject: 'A',
98
+ html: '<p>A</p>'
99
+ },
100
+ {
101
+ to: 'b@example.com',
102
+ from: 'no-reply@yourdomain.com',
103
+ subject: 'B',
104
+ html: '<p>B</p>'
105
+ }
106
+ ]
107
+
108
+ data, error = client.emails.batch(emails)
109
+ puts "Sent #{data['emails'].length} emails" if data
110
+ ```
111
+
112
+ ### Managing Emails
113
+
114
+ #### Get Email
115
+
116
+ ```ruby
117
+ email, error = client.emails.get('email_123')
118
+ puts "Email status: #{email['status']}" if email
119
+ ```
120
+
121
+ #### Update Schedule
122
+
123
+ ```ruby
124
+ data, error = client.emails.update('email_123', {
125
+ scheduledAt: (Time.now + 3600).iso8601 # 1 hour from now
126
+ })
127
+ ```
128
+
129
+ #### Cancel Scheduled Email
130
+
131
+ ```ruby
132
+ data, error = client.emails.cancel('email_123')
133
+ puts 'Email cancelled successfully' if data
134
+ ```
135
+
136
+ ### Contacts
137
+
138
+ #### Create Contact
139
+
140
+ ```ruby
141
+ data, error = client.contacts.create('book_123', {
142
+ email: 'user@example.com',
143
+ firstName: 'Jane',
144
+ metadata: {
145
+ plan: 'pro'
146
+ }
147
+ })
148
+ ```
149
+
150
+ #### Get Contact
151
+
152
+ ```ruby
153
+ contact, error = client.contacts.get('book_123', 'contact_456')
154
+ ```
155
+
156
+ #### Update Contact
157
+
158
+ ```ruby
159
+ data, error = client.contacts.update('book_123', 'contact_456', {
160
+ firstName: 'John',
161
+ metadata: {
162
+ plan: 'enterprise'
163
+ }
164
+ })
165
+ ```
166
+
167
+ #### Upsert Contact
168
+
169
+ ```ruby
170
+ data, error = client.contacts.upsert('book_123', 'contact_456', {
171
+ email: 'user@example.com',
172
+ firstName: 'Jane'
173
+ })
174
+ ```
175
+
176
+ #### Delete Contact
177
+
178
+ ```ruby
179
+ data, error = client.contacts.delete('book_123', 'contact_456')
180
+ ```
181
+
182
+ ### Campaigns
183
+
184
+ #### Create Campaign
185
+
186
+ ```ruby
187
+ data, error = client.campaigns.create({
188
+ name: 'Welcome Series',
189
+ subject: 'Welcome!',
190
+ html: '<p>Thanks for joining us!</p>',
191
+ from: 'welcome@yourdomain.com',
192
+ contactBookId: 'book_123'
193
+ })
194
+ ```
195
+
196
+ #### Schedule Campaign
197
+
198
+ ```ruby
199
+ data, error = client.campaigns.schedule('campaign_123', {
200
+ scheduledAt: '2024-12-01T10:00:00Z'
201
+ })
202
+ ```
203
+
204
+ #### Pause and Resume
205
+
206
+ ```ruby
207
+ # Pause
208
+ data, error = client.campaigns.pause('campaign_123')
209
+
210
+ # Resume
211
+ data, error = client.campaigns.resume('campaign_123')
212
+ ```
213
+
214
+ ### Domains
215
+
216
+ #### List Domains
217
+
218
+ ```ruby
219
+ domains, error = client.domains.list
220
+ domains.each do |domain|
221
+ puts "Domain: #{domain['domain']}, Status: #{domain['status']}"
222
+ end if domains
223
+ ```
224
+
225
+ #### Create Domain
226
+
227
+ ```ruby
228
+ data, error = client.domains.create({
229
+ domain: 'yourdomain.com'
230
+ })
231
+ ```
232
+
233
+ #### Verify Domain
234
+
235
+ ```ruby
236
+ data, error = client.domains.verify(123)
237
+ puts "Verification status: #{data['status']}" if data
238
+ ```
239
+
240
+ ## Error Handling
241
+
242
+ By default, the SDK raises `Unsent::HTTPError` for non-2xx responses:
243
+
244
+ ```ruby
245
+ begin
246
+ data, error = client.emails.get('email_123')
247
+ rescue Unsent::HTTPError => e
248
+ puts "HTTP #{e.status_code}: #{e.error['message']}"
249
+ end
250
+ ```
251
+
252
+ To handle errors as return values instead:
253
+
254
+ ```ruby
255
+ client = Unsent::Client.new('us_xxx', raise_on_error: false)
256
+
257
+ data, error = client.emails.get('email_123')
258
+ if error
259
+ puts "Error: #{error['message']}"
260
+ else
261
+ puts "Success!"
262
+ end
263
+ ```
264
+
265
+ ## Development
266
+
267
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests.
268
+
269
+ ## Contributing
270
+
271
+ Bug reports and pull requests are welcome on GitHub at https://github.com/souravsspace/unsent-ruby.
272
+
273
+ ## License
274
+
275
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ruby
4
+ module Sdk
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
data/lib/ruby/sdk.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "sdk/version"
4
+
5
+ module Ruby
6
+ module Sdk
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unsent
4
+ class Campaigns
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def create(payload)
10
+ @client.post("/campaigns", payload)
11
+ end
12
+
13
+ def get(campaign_id)
14
+ @client.get("/campaigns/#{campaign_id}")
15
+ end
16
+
17
+ def schedule(campaign_id, payload)
18
+ @client.post("/campaigns/#{campaign_id}/schedule", payload)
19
+ end
20
+
21
+ def pause(campaign_id)
22
+ @client.post("/campaigns/#{campaign_id}/pause", {})
23
+ end
24
+
25
+ def resume(campaign_id)
26
+ @client.post("/campaigns/#{campaign_id}/resume", {})
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "json"
5
+ require "uri"
6
+
7
+ module Unsent
8
+ class Client
9
+ DEFAULT_BASE_URL = "https://api.unsent.dev"
10
+
11
+ attr_reader :key, :url, :raise_on_error
12
+ attr_accessor :emails, :contacts, :campaigns, :domains
13
+
14
+ def initialize(key = nil, url: nil, raise_on_error: true)
15
+ @key = key || ENV["UNSENT_API_KEY"] || ENV["UNSENT_API_KEY"]
16
+ raise ArgumentError, "Missing API key. Pass it to Unsent::Client.new or set UNSENT_API_KEY environment variable" if @key.nil? || @key.empty?
17
+
18
+ base_url = url || ENV["UNSENT_BASE_URL"] || ENV["UNSENT_BASE_URL"] || DEFAULT_BASE_URL
19
+ @url = "#{base_url}/v1"
20
+ @raise_on_error = raise_on_error
21
+
22
+ # Initialize resource clients
23
+ @emails = Emails.new(self)
24
+ @contacts = Contacts.new(self)
25
+ @campaigns = Campaigns.new(self)
26
+ @domains = Domains.new(self)
27
+ end
28
+
29
+ def request(method, path, body = nil)
30
+ uri = URI("#{@url}#{path}")
31
+ http = Net::HTTP.new(uri.host, uri.port)
32
+ http.use_ssl = uri.scheme == "https"
33
+
34
+ request = case method.upcase
35
+ when "GET" then Net::HTTP::Get.new(uri)
36
+ when "POST" then Net::HTTP::Post.new(uri)
37
+ when "PUT" then Net::HTTP::Put.new(uri)
38
+ when "PATCH" then Net::HTTP::Patch.new(uri)
39
+ when "DELETE" then Net::HTTP::Delete.new(uri)
40
+ else raise ArgumentError, "Unsupported HTTP method: #{method}"
41
+ end
42
+
43
+ request["Authorization"] = "Bearer #{@key}"
44
+ request["Content-Type"] = "application/json"
45
+ request.body = body.to_json if body
46
+
47
+ response = http.request(request)
48
+ default_error = { "code" => "INTERNAL_SERVER_ERROR", "message" => response.message }
49
+
50
+ unless response.is_a?(Net::HTTPSuccess)
51
+ begin
52
+ payload = JSON.parse(response.body)
53
+ error = payload["error"] || default_error
54
+ rescue JSON::ParserError
55
+ error = default_error
56
+ end
57
+
58
+ raise HTTPError.new(response.code.to_i, error, method.upcase, path) if @raise_on_error
59
+ return [nil, error]
60
+ end
61
+
62
+ begin
63
+ data = JSON.parse(response.body)
64
+ [data, nil]
65
+ rescue JSON::ParserError
66
+ [nil, default_error]
67
+ end
68
+ end
69
+
70
+ def post(path, body)
71
+ request("POST", path, body)
72
+ end
73
+
74
+ def get(path)
75
+ request("GET", path)
76
+ end
77
+
78
+ def put(path, body)
79
+ request("PUT", path, body)
80
+ end
81
+
82
+ def patch(path, body)
83
+ request("PATCH", path, body)
84
+ end
85
+
86
+ def delete(path, body = nil)
87
+ request("DELETE", path, body)
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unsent
4
+ class Contacts
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def create(book_id, payload)
10
+ @client.post("/contactBooks/#{book_id}/contacts", payload)
11
+ end
12
+
13
+ def get(book_id, contact_id)
14
+ @client.get("/contactBooks/#{book_id}/contacts/#{contact_id}")
15
+ end
16
+
17
+ def update(book_id, contact_id, payload)
18
+ @client.patch("/contactBooks/#{book_id}/contacts/#{contact_id}", payload)
19
+ end
20
+
21
+ def upsert(book_id, contact_id, payload)
22
+ @client.put("/contactBooks/#{book_id}/contacts/#{contact_id}", payload)
23
+ end
24
+
25
+ def delete(book_id, contact_id)
26
+ @client.delete("/contactBooks/#{book_id}/contacts/#{contact_id}")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unsent
4
+ class Domains
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def list
10
+ @client.get("/domains")
11
+ end
12
+
13
+ def create(payload)
14
+ @client.post("/domains", payload)
15
+ end
16
+
17
+ def verify(domain_id)
18
+ @client.put("/domains/#{domain_id}/verify", {})
19
+ end
20
+
21
+ def get(domain_id)
22
+ @client.get("/domains/#{domain_id}")
23
+ end
24
+
25
+ def delete(domain_id)
26
+ @client.delete("/domains/#{domain_id}")
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unsent
4
+ class Emails
5
+ def initialize(client)
6
+ @client = client
7
+ end
8
+
9
+ def send(payload)
10
+ create(payload)
11
+ end
12
+
13
+ def create(payload)
14
+ # Normalize from_ to from
15
+ payload = payload.dup
16
+ payload[:from] = payload.delete(:from_) if payload.key?(:from_) && !payload.key?(:from)
17
+
18
+ # Convert scheduledAt to ISO 8601 if it's a Time object
19
+ if payload[:scheduledAt].is_a?(Time)
20
+ payload[:scheduledAt] = payload[:scheduledAt].iso8601
21
+ end
22
+
23
+ @client.post("/emails", payload)
24
+ end
25
+
26
+ def batch(emails)
27
+ items = emails.map do |email|
28
+ email = email.dup
29
+ email[:from] = email.delete(:from_) if email.key?(:from_) && !email.key?(:from)
30
+ email[:scheduledAt] = email[:scheduledAt].iso8601 if email[:scheduledAt].is_a?(Time)
31
+ email
32
+ end
33
+
34
+ @client.post("/emails/batch", items)
35
+ end
36
+
37
+ def get(email_id)
38
+ @client.get("/emails/#{email_id}")
39
+ end
40
+
41
+ def update(email_id, payload)
42
+ payload = payload.dup
43
+ payload[:scheduledAt] = payload[:scheduledAt].iso8601 if payload[:scheduledAt].is_a?(Time)
44
+ @client.patch("/emails/#{email_id}", payload)
45
+ end
46
+
47
+ def cancel(email_id)
48
+ @client.post("/emails/#{email_id}/cancel", {})
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unsent
4
+ class HTTPError < Error
5
+ attr_reader :status_code, :error, :method, :path
6
+
7
+ def initialize(status_code, error, method, path)
8
+ @status_code = status_code
9
+ @error = error
10
+ @method = method
11
+ @path = path
12
+ super(to_s)
13
+ end
14
+
15
+ def to_s
16
+ code = @error["code"] || "UNKNOWN_ERROR"
17
+ message = @error["message"] || ""
18
+ "#{@method} #{@path} -> #{@status_code} #{code}: #{message}"
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unsent
4
+ # Type definitions for documentation purposes
5
+ # Ruby is dynamically typed, so these are just for reference
6
+ module Types
7
+ # Email types
8
+ EmailCreate = Hash # { to:, from:, subject:, html:, text:, attachments:, scheduledAt: }
9
+ EmailCreateResponse = Hash # { id:, to:, from:, subject:, status:, createdAt: }
10
+ Email = Hash
11
+ EmailUpdate = Hash
12
+ EmailUpdateResponse = Hash
13
+ EmailCancelResponse = Hash
14
+ EmailBatchItem = Hash
15
+ EmailBatchResponse = Hash
16
+
17
+ # Contact types
18
+ Contact = Hash
19
+ ContactCreate = Hash
20
+ ContactCreateResponse = Hash
21
+ ContactUpdate = Hash
22
+ ContactUpdateResponse = Hash
23
+ ContactUpsert = Hash
24
+ ContactUpsertResponse = Hash
25
+ ContactDeleteResponse = Hash
26
+
27
+ # Campaign types
28
+ Campaign = Hash
29
+ CampaignCreate = Hash
30
+ CampaignCreateResponse = Hash
31
+ CampaignSchedule = Hash
32
+ CampaignScheduleResponse = Hash
33
+ CampaignActionResponse = Hash
34
+
35
+ # Domain types
36
+ Domain = Hash
37
+ DomainCreate = Hash
38
+ DomainCreateResponse = Hash
39
+ DomainVerifyResponse = Hash
40
+ DomainDeleteResponse = Hash
41
+
42
+ # Error type
43
+ APIError = Hash # { code:, message: }
44
+ end
45
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unsent
4
+ VERSION = "1.0.0"
5
+ end
data/lib/unsent.rb ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "unsent/version"
4
+ require_relative "unsent/client"
5
+ require_relative "unsent/emails"
6
+ require_relative "unsent/contacts"
7
+ require_relative "unsent/campaigns"
8
+ require_relative "unsent/domains"
9
+ require_relative "unsent/types"
10
+ require_relative "unsent/errors"
11
+
12
+ module Unsent
13
+ class Error < StandardError; end
14
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unsent
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - sourav
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-01 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Official Ruby SDK for the Unsent API. Send transactional emails, manage
13
+ contacts, campaigns, and domains.
14
+ email:
15
+ - hey@unsent.dev
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - CHANGELOG.md
21
+ - LICENSE.txt
22
+ - README.md
23
+ - lib/ruby/sdk.rb
24
+ - lib/ruby/sdk/version.rb
25
+ - lib/unsent.rb
26
+ - lib/unsent/campaigns.rb
27
+ - lib/unsent/client.rb
28
+ - lib/unsent/contacts.rb
29
+ - lib/unsent/domains.rb
30
+ - lib/unsent/emails.rb
31
+ - lib/unsent/errors.rb
32
+ - lib/unsent/types.rb
33
+ - lib/unsent/version.rb
34
+ homepage: https://unsent.dev
35
+ licenses:
36
+ - MIT
37
+ metadata:
38
+ homepage_uri: https://unsent.dev
39
+ source_code_uri: https://github.com/souravsspace/unsent-ruby
40
+ changelog_uri: https://github.com/souravsspace/unsent-ruby/blob/main/CHANGELOG.md
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 3.0.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.7.2
56
+ specification_version: 4
57
+ summary: Ruby SDK for the Unsent API - Send transactional emails with ease
58
+ test_files: []