sidemail-delivery 0.1.2 → 0.2.1

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: 6bac5ee4f015725a6442178f1e67e7b9b2ecb335bd731d9b426af39c9ee76968
4
- data.tar.gz: 05a87d3b2fe317d9fbb598ab07ef37c23a813c541b96d40518e4ec2ec3cdb8a1
3
+ metadata.gz: e822a3942340d3d666202a702ba36cbeeb393462d7a4956b5591032821b36ea8
4
+ data.tar.gz: 992694a05e74f6fc5808bd1e8a542ce6664aa4aeab9ec8e2dabaefc661627540
5
5
  SHA512:
6
- metadata.gz: df1abd94dd8ac857b49694d1443cb66de75629f787d1a74a218fed1ee945d21e519e7515c4bb2246f409a4b8c296c3215b4c4fac0a5948367c64f841bf8d7c20
7
- data.tar.gz: a725dc1dac4e9aef432b1ae21b0c282f1b7cf22e808088f696b906bad4b18cb0f99edfb9250c24fecdc5aa41528ea505b4f049f81917970dc5b69e8aaa2d9db1
6
+ metadata.gz: 24904cd8867f4182d50db9fabaefd80f6d3de74edebe82e5a42b7b8f1cf8c36bce64bf11e0c2e421366d66203172be5af1414019377d497a6f29cd14a5eb8171
7
+ data.tar.gz: 3929ba4a6eb3bba0986e9e8b2eb829b8d6c90e0e01577ffab5e2bada94a7f6b10cc5c9a2da497a1f6675e26fe8a678935b7116eb5c7b5618c4310289f3d4e45c
data/CHANGELOG.md CHANGED
@@ -0,0 +1,32 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+ ### Added
10
+ ### Fixed
11
+ ### Changed
12
+ ### Removed
13
+
14
+ ## [0.2.1] - 2024-02-08
15
+ ### Fixed
16
+ - Nested structures in template properties.
17
+
18
+ ## [0.2.0] - 2024-02-08
19
+ ### Added
20
+ - Support for Sidemail templates and template properties.
21
+
22
+ ## [0.1.2] - 2024-02-07
23
+ ### Added
24
+ - Extended the information displayed by `Sidemail::Delivery::Exception`.
25
+
26
+ ## [0.1.1] - 2024-02-01
27
+ ### Fixed
28
+ - Homepage URL in gemspec.
29
+
30
+ ## [0.1.0] - 2024-02-01
31
+ ### Added
32
+ - Deliver ActionMailer mails through Sidemail API.
data/README.md CHANGED
@@ -31,8 +31,18 @@ Rails.application.configure do
31
31
  end
32
32
  ```
33
33
 
34
- Finally you might need to contact Sidemail support to enable HTML sending through their API.
34
+ ## Usage
35
+
36
+ After the installation your existing mailers will automatically send their html/text views through Sidemail. You might
37
+ need to contact Sidemail support to enable HTML sending through their API.
35
38
  They disable it by default to prevent abuse.
36
39
 
40
+ If you want to use templates you can modify your mailers to include the template name and properties in the mail call
41
+ and pass an empty body to avoid the need for having a mailer view. For example:
42
+
43
+ ```ruby
44
+ mail(to: "somebody@example.com", template: "my-template-name", template_props: { name: "Somebody" }, body: "")
45
+ ```
46
+
37
47
  ## License
38
48
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ module Sidemail
2
+ module Delivery
3
+ class Exception < StandardError
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sidemail
4
+ module Delivery
5
+ class Method
6
+ URL = URI("https://api.sidemail.io/v1/email/send")
7
+
8
+ def initialize(_options)
9
+ # empty but unused, to avoid errors
10
+ end
11
+
12
+ def deliver!(mail)
13
+ response = post_to_api(mail[:to].addrs.map(&:address), payload(mail))
14
+ JSON.parse(response.body).tap do |json|
15
+ break unless json.key?("errorCode")
16
+
17
+ raise Sidemail::Delivery::Exception.new("#{json["errorCode"]}: #{json["developerMessage"]}")
18
+ end
19
+ rescue JSON::ParserError
20
+ raise Sidemail::Delivery::Exception.new("Unable to parse response")
21
+ end
22
+
23
+ protected
24
+
25
+ def payload(mail)
26
+ result = {
27
+ fromName: mail[:from].addrs.first.display_name,
28
+ fromAddress: mail[:from].addrs.first.address
29
+ }
30
+ result[:subject] = mail[:subject] if mail[:subject].present?
31
+ if mail[:template].present?
32
+ result[:templateName] = mail[:template]
33
+ result[:templateProps] = mail[:template_props].unparsed_value
34
+ else
35
+ result[:html] = mail.html_part.decoded
36
+ result[:text] = mail.text_part.decoded
37
+ end
38
+ if mail.attachments.size.positive?
39
+ result[:attachments] = mail.attachments.map do |attachment|
40
+ {name: attachment.filename, content: Base64.strict_encode64(attachment.body.decoded)}
41
+ end
42
+ end
43
+ result
44
+ end
45
+
46
+ def post_to_api(addresses, payload)
47
+ https = Net::HTTP.new(URL.host, URL.port)
48
+ https.use_ssl = true
49
+
50
+ request = Net::HTTP::Post.new(URL)
51
+ request["Content-Type"] = "application/json"
52
+ request["Authorization"] = "Bearer #{Sidemail::Delivery.settings.api_key}"
53
+ request.body = JSON.generate(addresses.map { |to| {toAddress: to}.merge(payload) })
54
+
55
+ https.request(request)
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,7 @@
1
+ module Sidemail
2
+ module Delivery
3
+ class Settings
4
+ attr_accessor :api_key
5
+ end
6
+ end
7
+ end
@@ -1,5 +1,5 @@
1
1
  module Sidemail
2
2
  module Delivery
3
- VERSION = "0.1.2"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
@@ -1,5 +1,8 @@
1
- require "sidemail/delivery/version"
1
+ require "sidemail/delivery/exception"
2
+ require "sidemail/delivery/method"
2
3
  require "sidemail/delivery/railtie"
4
+ require "sidemail/delivery/settings"
5
+ require "sidemail/delivery/version"
3
6
 
4
7
  module Sidemail
5
8
  module Delivery
@@ -11,59 +14,5 @@ module Sidemail
11
14
  self.settings ||= Settings.new
12
15
  yield(settings)
13
16
  end
14
-
15
- class Settings
16
- attr_accessor :api_key
17
- end
18
-
19
- class Exception < StandardError
20
- end
21
-
22
- class Method
23
- URL = URI("https://api.sidemail.io/v1/email/send")
24
-
25
- def initialize(_options)
26
- # empty but unused, to avoid errors
27
- end
28
-
29
- def deliver!(mail)
30
- payload = {
31
- fromName: mail[:from].addrs.first.display_name,
32
- fromAddress: mail[:from].addrs.first.address,
33
- html: mail.html_part.decoded,
34
- text: mail.text_part.decoded,
35
- subject: mail[:subject]
36
- }
37
- if mail.attachments.size.positive?
38
- payload[:attachments] = mail.attachments.map do |attachment|
39
- {name: attachment.filename, content: Base64.strict_encode64(attachment.body.decoded)}
40
- end
41
- end
42
-
43
- response = post_to_api(mail[:to].addrs.map(&:address), payload)
44
-
45
- JSON.parse(response.body).tap do |json|
46
- break unless json.key?("errorCode")
47
-
48
- raise Sidemail::Delivery::Exception.new("#{json["errorCode"]}: #{json["developerMessage"]}")
49
- end
50
- rescue JSON::ParserError
51
- raise Sidemail::Delivery::Exception.new("Unable to parse response")
52
- end
53
-
54
- protected
55
-
56
- def post_to_api(addresses, payload)
57
- https = Net::HTTP.new(URL.host, URL.port)
58
- https.use_ssl = true
59
-
60
- request = Net::HTTP::Post.new(URL)
61
- request["Content-Type"] = "application/json"
62
- request["Authorization"] = "Bearer #{Sidemail::Delivery.settings.api_key}"
63
- request.body = JSON.generate(addresses.map { |to| {toAddress: to}.merge(payload) })
64
-
65
- https.request(request)
66
- end
67
- end
68
17
  end
69
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sidemail-delivery
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Frank Groeneveld
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-02-07 00:00:00.000000000 Z
11
+ date: 2024-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -37,7 +37,10 @@ files:
37
37
  - README.md
38
38
  - Rakefile
39
39
  - lib/sidemail/delivery.rb
40
+ - lib/sidemail/delivery/exception.rb
41
+ - lib/sidemail/delivery/method.rb
40
42
  - lib/sidemail/delivery/railtie.rb
43
+ - lib/sidemail/delivery/settings.rb
41
44
  - lib/sidemail/delivery/version.rb
42
45
  homepage: https://github.com/toolsfactory-nl/sidemail-delivery
43
46
  licenses:
@@ -61,7 +64,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
64
  - !ruby/object:Gem::Version
62
65
  version: '0'
63
66
  requirements: []
64
- rubygems_version: 3.4.10
67
+ rubygems_version: 3.5.11
65
68
  signing_key:
66
69
  specification_version: 4
67
70
  summary: Rails delivery method for Sidemail.