helo-email-sdk 1.0.1 → 1.2.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afb5076cf98aa9c91576e55fe946cd31ec2c3db4f2c1b7d9fa57d1d0f73a39e7
4
- data.tar.gz: 404f3097b870c14fb708f09509eee2663a42b50bfb20f262a868c22a0188a5e7
3
+ metadata.gz: 28a1530bbcbb83039f3b9ad42de952e4304dcd84f928968e9eddfcdac0a14087
4
+ data.tar.gz: 831fc165e69374debae288d5c55d7046683c8cbdb30b550e656f55609b0ceeb3
5
5
  SHA512:
6
- metadata.gz: 25922786dfda9de69a3941b1b6ae32c9a92f8a84b53404d90d29632374cab2683f65410c05775a5bed9920020b04cd10ace4cd890b926a2d043b37f41f73470f
7
- data.tar.gz: 4915fa1d5b7ea81981887177c13dfc43dda6384cc84d86ba2f32e239e83db459bdfaef08cb1ffe0fcbaee2977a772d40da3c9562771080dcdcdf02511b6b449c
6
+ metadata.gz: 9addab2c0ddcd644ed94886ef93b7f32ebe84431987ebb96dee85b017cb6a6c2acf960c4ec57f4eda6d30dede78e18e3e85edd98ce8a525daaa8119069621364
7
+ data.tar.gz: 287062d7c0942f5e50904a1d3748e4e03127a53f4811d53d58930ca490bf926a636ddc0bca8efe4aee6cccc4cb2ecca17bcd09b8c0430f9d3ff277443b75b5fe
data/README.md CHANGED
@@ -25,7 +25,6 @@ require "helo-email-sdk"
25
25
 
26
26
  Helo.configure do |config|
27
27
  config.api_key = ENV.fetch("HELO_API_KEY")
28
- config.base_url = "https://api.helohq.com" # optional, this is the default
29
28
  end
30
29
  ```
31
30
 
@@ -60,6 +59,52 @@ rescue Helo::APIError => e
60
59
  end
61
60
  ```
62
61
 
62
+ ## Rails (ActionMailer)
63
+
64
+ The gem registers a `:helo` delivery method with ActionMailer, so existing
65
+ mailers send through the API without any changes. Configure the SDK in an initializer:
66
+
67
+ ```ruby
68
+ # config/initializers/helo.rb
69
+ Helo.configure do |config|
70
+ config.api_key = Rails.application.credentials.helo_api_key
71
+ end
72
+ ```
73
+
74
+ and switch the delivery method:
75
+
76
+ ```ruby
77
+ # config/environments/production.rb
78
+ config.action_mailer.delivery_method = :helo
79
+ ```
80
+
81
+ Plain ActionMailer fields map to the API: `from`, `to`, `cc`, `bcc`, `reply_to`, `subject`, the
82
+ text and HTML parts, attachments (`attachments.inline` ones keep their content id, so
83
+ `image_tag attachments["logo.png"].url` works), and any custom headers. API-specific options go
84
+ in `delivery_method_options`, which other delivery methods (`:test`, letter_opener, SMTP) ignore:
85
+
86
+ ```ruby
87
+ class UserMailer < ApplicationMailer
88
+ def welcome(user)
89
+ headers["X-Custom-Header"] = "value" # custom headers are sent as-is
90
+ mail(
91
+ to: user.email,
92
+ subject: "Welcome!",
93
+ delivery_method_options: {
94
+ tags: ["welcome"],
95
+ metadata: { user_id: user.id.to_s },
96
+ tracking: { opens: true, links: false },
97
+ idempotency_key: "welcome-#{user.id}", # retries won't send twice
98
+ channel_id: "..."
99
+ }
100
+ )
101
+ end
102
+ end
103
+ ```
104
+
105
+ After delivery, `message[:helo_message_id].value` is the id the API assigned the
106
+ message, and failed requests raise `Helo::APIError` (subject to `config.action_mailer.raise_delivery_errors`).
107
+
63
108
  ## Webhook signature verification
64
109
 
65
110
  Webhook deliveries are signed with the endpoint's signing key. Verify every delivery before
@@ -122,4 +167,5 @@ To compute a signature yourself — signing a fixture in tests, for example —
122
167
  - [Helo::Broadcasts](docs/Broadcasts.md)
123
168
  - [Helo::Statistics](docs/Statistics.md)
124
169
  - [Helo::Suppressions](docs/Suppressions.md)
170
+ - [Helo::Templates](docs/Templates.md)
125
171
  - [Helo::Webhooks](docs/Webhooks.md)
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class Templates < API
5
+ def list(request_data = {})
6
+ request = TemplatesListRequest.new(request_data)
7
+ response = @client.request(:get, "/templates", params: request.to_params)
8
+ PaginatedResponseOfTemplateSummaryResponse.from_hash(response.body)
9
+ end
10
+
11
+ def create(request_data)
12
+ request = CreateTemplateRequest.new(request_data)
13
+ response = @client.request(:post, "/templates", body: request.to_params)
14
+ TemplateResponse.from_hash(response.body)
15
+ end
16
+
17
+ def render_draft(request_data)
18
+ request = RenderDraftRequest.new(request_data)
19
+ response = @client.request(:post, "/templates/render", body: request.to_params)
20
+ RenderTemplateResponse.from_hash(response.body)
21
+ end
22
+
23
+ def retrieve(id)
24
+ response = @client.request(:get, "/templates/#{id}")
25
+ TemplateResponse.from_hash(response.body)
26
+ end
27
+
28
+ def update(id, request_data)
29
+ request = UpdateTemplateRequest.new(request_data)
30
+ response = @client.request(:patch, "/templates/#{id}", body: request.to_params)
31
+ TemplateResponse.from_hash(response.body)
32
+ end
33
+
34
+ def delete(id)
35
+ response = @client.request(:delete, "/templates/#{id}")
36
+ nil
37
+ end
38
+
39
+ def render(id, request_data)
40
+ request = RenderTemplateRequest.new(request_data)
41
+ response = @client.request(:post, "/templates/#{id}/render", body: request.to_params)
42
+ RenderTemplateResponse.from_hash(response.body)
43
+ end
44
+
45
+ class_methods :list, :create, :render_draft, :retrieve, :update, :delete, :render
46
+ end
47
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ # ActionMailer delivery method, registered as :helo by the Railtie.
5
+ class Mailer
6
+ STANDARD_HEADERS = %w[
7
+ from to cc bcc reply-to subject date message-id mime-version
8
+ content-type content-transfer-encoding content-disposition content-id
9
+ sender return-path
10
+ ].freeze
11
+
12
+ # Where the API's id for the message is recorded after sending
13
+ MESSAGE_ID_FIELD = "helo-message-id"
14
+
15
+ attr_reader :settings
16
+
17
+ # Receives mail(delivery_method_options: { ... }) as settings. return_response makes
18
+ # Mail::Message#deliver! return our response.
19
+ def initialize(settings = {})
20
+ @settings = settings.merge(return_response: true)
21
+ end
22
+
23
+ def deliver!(mail)
24
+ raise ArgumentError, "Helo is not configured: call Helo.configure" unless API.default_client
25
+
26
+ response = Sending.send_transactional(
27
+ build_request(mail),
28
+ channel_id: settings[:channel_id],
29
+ idempotency_key: settings[:idempotency_key]
30
+ )
31
+ if response&.message_id
32
+ mail[MESSAGE_ID_FIELD] = nil # Mail would otherwise add a second field when resending
33
+ mail[MESSAGE_ID_FIELD] = response.message_id
34
+ end
35
+ response
36
+ end
37
+
38
+ private
39
+
40
+ def build_request(mail)
41
+ {
42
+ from: addresses(mail[:from]).first,
43
+ to: addresses(mail[:to]),
44
+ cc: addresses(mail[:cc]),
45
+ bcc: addresses(mail[:bcc]),
46
+ reply_to: addresses(mail[:reply_to]),
47
+ subject: mail.subject,
48
+ html: body(mail, "text/html"),
49
+ text: body(mail, "text/plain"),
50
+ attachments: attachments(mail),
51
+ headers: custom_headers(mail),
52
+ tags: settings[:tags],
53
+ metadata: settings[:metadata],
54
+ tracking: settings[:tracking]
55
+ }.compact_blank
56
+ end
57
+
58
+ def addresses(field)
59
+ return [] unless field
60
+ raise ArgumentError, "Invalid #{field.name} address: #{field.value}" unless field.respond_to?(:addrs)
61
+
62
+ field.addrs.map { |address| { email: address.address, name: address.display_name }.compact }
63
+ end
64
+
65
+ def body(mail, mime_type)
66
+ if mail.multipart?
67
+ part = mime_type == "text/html" ? mail.html_part : mail.text_part
68
+ part&.decoded
69
+ elsif (mail.mime_type || "text/plain") == mime_type
70
+ mail.decoded
71
+ end
72
+ end
73
+
74
+ def attachments(mail)
75
+ mail.attachments.map do |attachment|
76
+ {
77
+ content: [attachment.decoded].pack("m0"),
78
+ file_name: attachment.filename,
79
+ content_type: attachment.mime_type,
80
+ disposition: attachment.inline? ? "inline" : "attachment",
81
+ content_id: (attachment.cid if attachment.inline?)
82
+ }.compact
83
+ end
84
+ end
85
+
86
+ def custom_headers(mail)
87
+ mail.header_fields.each_with_object({}) do |field, headers|
88
+ next if STANDARD_HEADERS.include?(field.name.downcase) || field.name.downcase == MESSAGE_ID_FIELD
89
+
90
+ headers[field.name] = field.value.to_s
91
+ end
92
+ end
93
+ end
94
+ end
@@ -6,5 +6,6 @@ module Helo
6
6
 
7
7
  api_attribute :name, :string, key: "name"
8
8
  api_attribute :channel_ids, key: "channelIds"
9
+ api_attribute :enable_custom_tracking, :boolean, key: "enableCustomTracking"
9
10
  end
10
11
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class CreateTemplateRequest
5
+ include Helo::Core::Model
6
+
7
+ api_attribute :name, :string, key: "name"
8
+ api_attribute :subject, :string, key: "subject"
9
+ api_attribute :html, :string, key: "html"
10
+ api_attribute :mjml, :string, key: "mjml"
11
+ api_attribute :text, :string, key: "text"
12
+ api_attribute :channel_ids, key: "channelIds"
13
+ end
14
+ end
@@ -7,5 +7,6 @@ module Helo
7
7
  api_attribute :domain_key_active, Helo::Core::ModelType.new("Helo::DnsRecordResponse"), key: "domainKeyActive"
8
8
  api_attribute :domain_key_pending, Helo::Core::ModelType.new("Helo::DnsRecordResponse"), key: "domainKeyPending"
9
9
  api_attribute :return_path, Helo::Core::ArrayType.new("Helo::DnsRecordResponse"), key: "returnPath"
10
+ api_attribute :tracking, Helo::Core::ModelType.new("Helo::DnsRecordResponse"), key: "tracking"
10
11
  end
11
12
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class PaginatedResponseOfTemplateSummaryResponse
5
+ include Helo::Core::Model
6
+
7
+ api_attribute :total_count, :integer, key: "totalCount"
8
+ api_attribute :results, Helo::Core::ArrayType.new("Helo::TemplateSummaryResponse"), key: "results"
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class RenderDraftRequest
5
+ include Helo::Core::Model
6
+
7
+ api_attribute :subject, :string, key: "subject"
8
+ api_attribute :html, :string, key: "html"
9
+ api_attribute :mjml, :string, key: "mjml"
10
+ api_attribute :text, :string, key: "text"
11
+ api_attribute :inline_styles, :boolean, key: "inlineStyles"
12
+ api_attribute :data, key: "data"
13
+ end
14
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class RenderTemplateRequest
5
+ include Helo::Core::Model
6
+
7
+ api_attribute :inline_styles, :boolean, key: "inlineStyles"
8
+ api_attribute :data, key: "data"
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class RenderTemplateResponse
5
+ include Helo::Core::Model
6
+
7
+ api_attribute :subject, :string, key: "subject"
8
+ api_attribute :html, :string, key: "html"
9
+ api_attribute :text, :string, key: "text"
10
+ end
11
+ end
@@ -4,6 +4,7 @@ module Helo
4
4
  class SendBroadcastRequestTemplate
5
5
  include Helo::Core::Model
6
6
 
7
+ api_attribute :id, :string, key: "id"
7
8
  api_attribute :subject, :string, key: "subject"
8
9
  api_attribute :html, :string, key: "html"
9
10
  api_attribute :text, :string, key: "text"
@@ -4,6 +4,7 @@ module Helo
4
4
  class SendMessageRequestTemplate
5
5
  include Helo::Core::Model
6
6
 
7
+ api_attribute :id, :string, key: "id"
7
8
  api_attribute :subject, :string, key: "subject"
8
9
  api_attribute :html, :string, key: "html"
9
10
  api_attribute :text, :string, key: "text"
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class TemplateResponse
5
+ include Helo::Core::Model
6
+
7
+ api_attribute :id, :string, key: "id"
8
+ api_attribute :created_at, :datetime, key: "createdAt"
9
+ api_attribute :updated_at, :datetime, key: "updatedAt"
10
+ api_attribute :version, :integer, key: "version"
11
+ api_attribute :name, :string, key: "name"
12
+ api_attribute :subject, :string, key: "subject"
13
+ api_attribute :html, :string, key: "html"
14
+ api_attribute :mjml, :string, key: "mjml"
15
+ api_attribute :text, :string, key: "text"
16
+ api_attribute :channel_ids, key: "channelIds"
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class TemplateSummaryResponse
5
+ include Helo::Core::Model
6
+
7
+ api_attribute :id, :string, key: "id"
8
+ api_attribute :created_at, :datetime, key: "createdAt"
9
+ api_attribute :updated_at, :datetime, key: "updatedAt"
10
+ api_attribute :name, :string, key: "name"
11
+ api_attribute :subject, :string, key: "subject"
12
+ api_attribute :channel_ids, key: "channelIds"
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ # Generated request model for query parameters
5
+ class TemplatesListRequest
6
+ include Helo::Core::Model
7
+
8
+ api_attribute :limit, :integer, key: "limit"
9
+ api_attribute :offset, :integer, key: "offset"
10
+ api_attribute :name, :string, key: "name"
11
+ api_attribute :channel_ids, key: "channelIds", explode: false
12
+ end
13
+ end
@@ -5,5 +5,6 @@ module Helo
5
5
  include Helo::Core::Model
6
6
 
7
7
  api_attribute :channel_ids, key: "channelIds"
8
+ api_attribute :enable_custom_tracking, :boolean, key: "enableCustomTracking"
8
9
  end
9
10
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class UpdateTemplateRequest
5
+ include Helo::Core::Model
6
+
7
+ api_attribute :name, :string, key: "name"
8
+ api_attribute :subject, :string, key: "subject"
9
+ api_attribute :html, :string, key: "html", nullable: true
10
+ api_attribute :mjml, :string, key: "mjml", nullable: true
11
+ api_attribute :text, :string, key: "text", nullable: true
12
+ api_attribute :channel_ids, key: "channelIds", nullable: true
13
+ api_attribute :version, :integer, key: "version"
14
+ end
15
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Helo
4
+ class Railtie < ::Rails::Railtie
5
+ ActiveSupport.on_load(:action_mailer) do
6
+ add_delivery_method :helo, Helo::Mailer
7
+ end
8
+ end
9
+ end
data/lib/helo/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Helo
4
- VERSION = "1.0.1"
4
+ VERSION = "1.2.0"
5
5
  end
data/lib/helo.rb CHANGED
@@ -18,6 +18,8 @@ require_relative "helo/api_error"
18
18
  require_relative "helo/configuration"
19
19
  require_relative "helo/client"
20
20
  require_relative "helo/webhook_signatures"
21
+ require_relative "helo/mailer"
22
+ require_relative "helo/railtie" if defined?(Rails::Railtie)
21
23
 
22
24
 
23
25
  module Helo
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helo-email-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Helo Email
@@ -37,6 +37,20 @@ dependencies:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '2.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: actionmailer
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '7.0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '7.0'
40
54
  - !ruby/object:Gem::Dependency
41
55
  name: minitest
42
56
  requirement: !ruby/object:Gem::Requirement
@@ -97,6 +111,7 @@ files:
97
111
  - lib/helo/apis/sending.rb
98
112
  - lib/helo/apis/statistics.rb
99
113
  - lib/helo/apis/suppressions.rb
114
+ - lib/helo/apis/templates.rb
100
115
  - lib/helo/apis/webhooks.rb
101
116
  - lib/helo/client.rb
102
117
  - lib/helo/configuration.rb
@@ -117,6 +132,7 @@ files:
117
132
  - lib/helo/enums/message_status.rb
118
133
  - lib/helo/enums/suppression_reason.rb
119
134
  - lib/helo/enums/webhook_event.rb
135
+ - lib/helo/mailer.rb
120
136
  - lib/helo/models/activity_event.rb
121
137
  - lib/helo/models/activity_list_events_request.rb
122
138
  - lib/helo/models/activity_list_messages_request.rb
@@ -146,6 +162,7 @@ files:
146
162
  - lib/helo/models/create_domain_request.rb
147
163
  - lib/helo/models/create_suppressions_request.rb
148
164
  - lib/helo/models/create_suppressions_response.rb
165
+ - lib/helo/models/create_template_request.rb
149
166
  - lib/helo/models/create_webhook_request.rb
150
167
  - lib/helo/models/delivered_details.rb
151
168
  - lib/helo/models/delivery_stats.rb
@@ -183,6 +200,7 @@ files:
183
200
  - lib/helo/models/paginated_response_of_broadcast_suppression.rb
184
201
  - lib/helo/models/paginated_response_of_domain_response.rb
185
202
  - lib/helo/models/paginated_response_of_suppression_response.rb
203
+ - lib/helo/models/paginated_response_of_template_summary_response.rb
186
204
  - lib/helo/models/pagination_result_of_channel_basic_response.rb
187
205
  - lib/helo/models/pagination_result_of_webhook_response.rb
188
206
  - lib/helo/models/recipient_complained_webhook_payload.rb
@@ -193,6 +211,9 @@ files:
193
211
  - lib/helo/models/remove_suppression_result.rb
194
212
  - lib/helo/models/remove_suppressions_request.rb
195
213
  - lib/helo/models/remove_suppressions_response.rb
214
+ - lib/helo/models/render_draft_request.rb
215
+ - lib/helo/models/render_template_request.rb
216
+ - lib/helo/models/render_template_response.rb
196
217
  - lib/helo/models/resubscribed_details.rb
197
218
  - lib/helo/models/return_path_domain_verification_failed_payload.rb
198
219
  - lib/helo/models/return_path_domain_verified_payload.rb
@@ -220,10 +241,14 @@ files:
220
241
  - lib/helo/models/suppression_response.rb
221
242
  - lib/helo/models/suppression_result.rb
222
243
  - lib/helo/models/suppressions_list_request.rb
244
+ - lib/helo/models/template_response.rb
245
+ - lib/helo/models/template_summary_response.rb
246
+ - lib/helo/models/templates_list_request.rb
223
247
  - lib/helo/models/unsubscribed_details.rb
224
248
  - lib/helo/models/update_channel_request.rb
225
249
  - lib/helo/models/update_channel_tracking.rb
226
250
  - lib/helo/models/update_domain_request.rb
251
+ - lib/helo/models/update_template_request.rb
227
252
  - lib/helo/models/update_webhook_request.rb
228
253
  - lib/helo/models/validation_error.rb
229
254
  - lib/helo/models/webhook_header.rb
@@ -231,6 +256,7 @@ files:
231
256
  - lib/helo/models/webhook_payload_common.rb
232
257
  - lib/helo/models/webhook_response.rb
233
258
  - lib/helo/models/webhooks_list_request.rb
259
+ - lib/helo/railtie.rb
234
260
  - lib/helo/version.rb
235
261
  - lib/helo/webhook_signatures.rb
236
262
  homepage: https://rubygems.org/gems/helo-email-sdk