serversmtp 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.
Files changed (4) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +18 -0
  3. data/lib/serversmtp.rb +172 -0
  4. metadata +46 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6721c84818aa8050f03a3506c944d5ef0e2e0531081726d4ebc2fde1d4b91509
4
+ data.tar.gz: 301ec5af3ba28451d411985dbfb97077751e6e8f4e63f90bb977b3fce17f3627
5
+ SHA512:
6
+ metadata.gz: 71f0fac36f7771385cb50471f5270bf0215f7db1434b6d7e062b86bc0f3e5300a746300da2ccf3e0a5da33848a69b2a3ddac110162deef622dbd3addf6641789
7
+ data.tar.gz: 01e90002beaf1ed4cfdda6cdf7dc07a2045ecd454f8e73133a3b9c51505d4eb107b069cba0d40c2c16ad2faa2db3cde91e2cfba797278502a616a98c78708b5c
data/README.md ADDED
@@ -0,0 +1,18 @@
1
+ # ServerSMTP Ruby SDK (`serversmtp`)
2
+
3
+ SDK oficial Ruby (`>= 3.0`, zero dependențe runtime) pentru [ServerSMTP.ro](https://serversmtp.ro).
4
+
5
+ ```ruby
6
+ require "serversmtp"
7
+
8
+ client = ServerSMTP::Client.new(ENV.fetch("SERVERSMTP_API_KEY"))
9
+ res = client.emails.send(
10
+ {
11
+ from: "Magazin <comenzi@magazin.ro>",
12
+ to: ["client@exemplu.ro"],
13
+ subject: "Comanda #1042 a fost confirmată",
14
+ html: "<p>Mulțumim!</p>"
15
+ },
16
+ idempotency_key: "comanda-1042"
17
+ )
18
+ ```
data/lib/serversmtp.rb ADDED
@@ -0,0 +1,172 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "net/http"
5
+ require "openssl"
6
+ require "securerandom"
7
+ require "uri"
8
+
9
+ module ServerSMTP
10
+ VERSION = "0.2.0"
11
+ DEFAULT_BASE_URL = "https://api.serversmtp.ro"
12
+
13
+ class Error < StandardError
14
+ attr_reader :status_code, :request_id, :fields
15
+
16
+ def initialize(message, status_code:, request_id: nil, fields: nil)
17
+ super(message)
18
+ @status_code = status_code
19
+ @request_id = request_id
20
+ @fields = fields || []
21
+ end
22
+ end
23
+
24
+ module Webhooks
25
+ module_function
26
+
27
+ def verify_signature(payload, timestamp_header, signature_header, secret)
28
+ return false if timestamp_header.to_s.empty? || secret.to_s.empty?
29
+ return false unless signature_header.to_s.start_with?("v1=")
30
+
31
+ expected = "v1=" + OpenSSL::HMAC.hexdigest("SHA256", secret, "#{timestamp_header}.#{payload}")
32
+ return false unless expected.bytesize == signature_header.bytesize
33
+
34
+ OpenSSL.fixed_length_secure_compare(expected, signature_header)
35
+ end
36
+ end
37
+
38
+ class Client
39
+ attr_reader :emails, :sms, :events, :templates, :topics, :suppressions, :campaigns, :broadcasts, :webhooks, :domains
40
+
41
+ def initialize(api_key, base_url: DEFAULT_BASE_URL, timeout: 15)
42
+ @api_key = api_key.to_s.strip
43
+ @base_url = base_url.sub(%r{/+\z}, "")
44
+ @timeout = timeout
45
+ @emails = EmailsResource.new(self)
46
+ @sms = SmsResource.new(self)
47
+ @events = EventsResource.new(self)
48
+ @templates = TemplatesResource.new(self)
49
+ @topics = TopicsResource.new(self)
50
+ @suppressions = SuppressionsResource.new(self)
51
+ @campaigns = CampaignsResource.new(self)
52
+ @broadcasts = @campaigns
53
+ @webhooks = WebhooksResource.new(self)
54
+ @domains = DomainsResource.new(self)
55
+ end
56
+
57
+ def validate_email(email)
58
+ request(:post, "/v1/validate", body: { email: email })
59
+ end
60
+
61
+ def request(method, path, body: nil, idempotency_key: nil, require_idem: false)
62
+ uri = URI.parse("#{@base_url}#{path}")
63
+ http = Net::HTTP.new(uri.host, uri.port)
64
+ http.use_ssl = uri.scheme == "https"
65
+ http.open_timeout = @timeout
66
+ http.read_timeout = @timeout
67
+
68
+ req_class = { get: Net::HTTP::Get, post: Net::HTTP::Post, patch: Net::HTTP::Patch, delete: Net::HTTP::Delete }.fetch(method)
69
+ req = req_class.new(uri.request_uri)
70
+ req["Authorization"] = "Bearer #{@api_key}"
71
+ req["User-Agent"] = "serversmtp-ruby/#{VERSION}"
72
+ used_key = idempotency_key || (require_idem ? SecureRandom.uuid : nil)
73
+ req["Idempotency-Key"] = used_key if used_key
74
+
75
+ if body
76
+ req["Content-Type"] = "application/json"
77
+ req.body = JSON.generate(body)
78
+ end
79
+
80
+ res = http.request(req)
81
+ parsed = res.body && !res.body.empty? ? JSON.parse(res.body) : {}
82
+ status = res.code.to_i
83
+ if status >= 400
84
+ raise Error.new(
85
+ parsed["message"] || "HTTP #{status}",
86
+ status_code: status,
87
+ request_id: parsed["requestId"],
88
+ fields: parsed["fields"]
89
+ )
90
+ end
91
+ { "data" => parsed, "idempotency_key" => used_key }
92
+ end
93
+ end
94
+
95
+ class EmailsResource
96
+ def initialize(client) = @client = client
97
+
98
+ def send(payload, idempotency_key: nil)
99
+ @client.request(:post, "/v1/send", body: payload, idempotency_key: idempotency_key, require_idem: true)
100
+ end
101
+
102
+ def send_batch(messages, idempotency_key: nil)
103
+ @client.request(:post, "/v1/send/batch", body: { messages: messages }, idempotency_key: idempotency_key, require_idem: true)
104
+ end
105
+
106
+ def schedule(payload, idempotency_key: nil)
107
+ @client.request(:post, "/v1/send/scheduled", body: payload, idempotency_key: idempotency_key, require_idem: true)
108
+ end
109
+ end
110
+
111
+ class SmsResource
112
+ def initialize(client) = @client = client
113
+
114
+ def send(payload, idempotency_key: nil)
115
+ @client.request(:post, "/v1/sms/send", body: payload, idempotency_key: idempotency_key, require_idem: true)
116
+ end
117
+ end
118
+
119
+ class EventsResource
120
+ def initialize(client) = @client = client
121
+
122
+ def trigger(payload)
123
+ @client.request(:post, "/v1/events", body: payload)
124
+ end
125
+ end
126
+
127
+ class TemplatesResource
128
+ def initialize(client) = @client = client
129
+ def list = @client.request(:get, "/v1/templates")
130
+ def get(id) = @client.request(:get, "/v1/templates/#{URI.encode_uri_component(id.to_s)}")
131
+ def create(payload) = @client.request(:post, "/v1/templates", body: payload)
132
+ def update(id, payload) = @client.request(:patch, "/v1/templates/#{URI.encode_uri_component(id.to_s)}", body: payload)
133
+ def delete(id) = @client.request(:delete, "/v1/templates/#{URI.encode_uri_component(id.to_s)}")
134
+ end
135
+
136
+ class TopicsResource
137
+ def initialize(client) = @client = client
138
+ def list = @client.request(:get, "/v1/topics")
139
+ def create(payload) = @client.request(:post, "/v1/topics", body: payload)
140
+ def update(id, payload) = @client.request(:patch, "/v1/topics/#{URI.encode_uri_component(id.to_s)}", body: payload)
141
+ def delete(id) = @client.request(:delete, "/v1/topics/#{URI.encode_uri_component(id.to_s)}")
142
+ end
143
+
144
+ class SuppressionsResource
145
+ def initialize(client) = @client = client
146
+ def list = @client.request(:get, "/v1/suppressions")
147
+ def add(email, reason: nil) = @client.request(:post, "/v1/suppressions", body: reason ? { email: email, reason: reason } : { email: email })
148
+ def delete(email) = @client.request(:delete, "/v1/suppressions/#{URI.encode_uri_component(email.to_s)}")
149
+ end
150
+
151
+ class CampaignsResource
152
+ def initialize(client) = @client = client
153
+ def list = @client.request(:get, "/v1/campaigns")
154
+ def create(payload) = @client.request(:post, "/v1/campaigns", body: payload)
155
+ def send_campaign(id) = @client.request(:post, "/v1/campaigns/#{URI.encode_uri_component(id.to_s)}/send", body: {})
156
+ end
157
+
158
+ class WebhooksResource
159
+ def initialize(client) = @client = client
160
+ def list = @client.request(:get, "/v1/webhooks")
161
+ def create(payload) = @client.request(:post, "/v1/webhooks", body: payload)
162
+ def delete(id) = @client.request(:delete, "/v1/webhooks/#{URI.encode_uri_component(id.to_s)}")
163
+ def events = @client.request(:get, "/v1/webhooks/events")
164
+ def replay(event_id) = @client.request(:post, "/v1/webhooks/events/#{URI.encode_uri_component(event_id.to_s)}/replay", body: {})
165
+ end
166
+
167
+ class DomainsResource
168
+ def initialize(client) = @client = client
169
+ def list = @client.request(:get, "/v1/domains")
170
+ def get(id) = @client.request(:get, "/v1/domains/#{URI.encode_uri_component(id.to_s)}")
171
+ end
172
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: serversmtp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - THINK AI SRL
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-12-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - suport@serversmtp.ro
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - README.md
21
+ - lib/serversmtp.rb
22
+ homepage: https://serversmtp.ro/docs#biblioteci
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 3.0.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.5.22
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Official Ruby SDK for ServerSMTP.ro (Transactional Email, Batch, Scheduled
45
+ & SMS)
46
+ test_files: []