sidemail-delivery 0.1.1 → 0.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 +4 -4
- data/CHANGELOG.md +28 -0
- data/MIT-LICENSE +1 -1
- data/README.md +11 -1
- data/lib/sidemail/delivery/exception.rb +6 -0
- data/lib/sidemail/delivery/method.rb +73 -0
- data/lib/sidemail/delivery/settings.rb +7 -0
- data/lib/sidemail/delivery/version.rb +1 -1
- data/lib/sidemail/delivery.rb +4 -53
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '08a4d71d05c883b4047bcfae8d9db5739f24cecac1b339f42943df14e959b8a6'
|
4
|
+
data.tar.gz: 4edb02eb7d59a33a3ad90ad240a2433d00e6d8db319e80bf41fbdddc5d6bd76c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47b372fec3fb722db0fbc579756923f55213f9d053be3cf20cc806f4f08f36981af57f76c6fbac3980aecda7a6c25651c2f5ebe153ca6c869a10455b0f88ee65
|
7
|
+
data.tar.gz: c621158247f34da1babccbfb909cf62301fd9e5849e240862f81aaa079a3cd3b56209500627ee09b6a591156e799731fa59cf917c71e79602da58538b67bd2be
|
data/CHANGELOG.md
CHANGED
@@ -0,0 +1,28 @@
|
|
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.0] - 2024-02-08
|
15
|
+
### Added
|
16
|
+
- Support for Sidemail templates and template properties.
|
17
|
+
|
18
|
+
## [0.1.2] - 2024-02-07
|
19
|
+
### Added
|
20
|
+
- Extended the information displayed by `Sidemail::Delivery::Exception`.
|
21
|
+
|
22
|
+
## [0.1.1] - 2024-02-01
|
23
|
+
### Fixed
|
24
|
+
- Homepage URL in gemspec.
|
25
|
+
|
26
|
+
## [0.1.0] - 2024-02-01
|
27
|
+
### Added
|
28
|
+
- Deliver ActionMailer mails through Sidemail API.
|
data/MIT-LICENSE
CHANGED
data/README.md
CHANGED
@@ -31,8 +31,18 @@ Rails.application.configure do
|
|
31
31
|
end
|
32
32
|
```
|
33
33
|
|
34
|
-
|
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,73 @@
|
|
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] = convert_template_props(mail[:template_props])
|
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
|
+
# converts all values that are not a string to a string for the Sidemail API
|
47
|
+
def convert_template_props(template_props)
|
48
|
+
template_props.unparsed_value.transform_values do |value|
|
49
|
+
case value
|
50
|
+
when String
|
51
|
+
value
|
52
|
+
when Float, Integer
|
53
|
+
value.to_s
|
54
|
+
else
|
55
|
+
value.to_json
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def post_to_api(addresses, payload)
|
61
|
+
https = Net::HTTP.new(URL.host, URL.port)
|
62
|
+
https.use_ssl = true
|
63
|
+
|
64
|
+
request = Net::HTTP::Post.new(URL)
|
65
|
+
request["Content-Type"] = "application/json"
|
66
|
+
request["Authorization"] = "Bearer #{Sidemail::Delivery.settings.api_key}"
|
67
|
+
request.body = JSON.generate(addresses.map { |to| {toAddress: to}.merge(payload) })
|
68
|
+
|
69
|
+
https.request(request)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
data/lib/sidemail/delivery.rb
CHANGED
@@ -1,5 +1,8 @@
|
|
1
|
-
require "sidemail/delivery/
|
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,57 +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
|
-
raise Sidemail::Delivery::Exception.new(json["errorCode"]) if json.key?("errorCode")
|
47
|
-
end
|
48
|
-
rescue JSON::ParserError
|
49
|
-
raise Sidemail::Delivery::Exception.new("Unable to parse response")
|
50
|
-
end
|
51
|
-
|
52
|
-
protected
|
53
|
-
|
54
|
-
def post_to_api(addresses, payload)
|
55
|
-
https = Net::HTTP.new(URL.host, URL.port)
|
56
|
-
https.use_ssl = true
|
57
|
-
|
58
|
-
request = Net::HTTP::Post.new(URL)
|
59
|
-
request["Content-Type"] = "application/json"
|
60
|
-
request["Authorization"] = "Bearer #{Sidemail::Delivery.settings.api_key}"
|
61
|
-
request.body = JSON.generate(addresses.map { |to| {toAddress: to}.merge(payload) })
|
62
|
-
|
63
|
-
https.request(request)
|
64
|
-
end
|
65
|
-
end
|
66
17
|
end
|
67
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.
|
4
|
+
version: 0.2.0
|
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-
|
11
|
+
date: 2024-02-08 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:
|