sidemail-delivery 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6bac5ee4f015725a6442178f1e67e7b9b2ecb335bd731d9b426af39c9ee76968
4
- data.tar.gz: 05a87d3b2fe317d9fbb598ab07ef37c23a813c541b96d40518e4ec2ec3cdb8a1
3
+ metadata.gz: '08a4d71d05c883b4047bcfae8d9db5739f24cecac1b339f42943df14e959b8a6'
4
+ data.tar.gz: 4edb02eb7d59a33a3ad90ad240a2433d00e6d8db319e80bf41fbdddc5d6bd76c
5
5
  SHA512:
6
- metadata.gz: df1abd94dd8ac857b49694d1443cb66de75629f787d1a74a218fed1ee945d21e519e7515c4bb2246f409a4b8c296c3215b4c4fac0a5948367c64f841bf8d7c20
7
- data.tar.gz: a725dc1dac4e9aef432b1ae21b0c282f1b7cf22e808088f696b906bad4b18cb0f99edfb9250c24fecdc5aa41528ea505b4f049f81917970dc5b69e8aaa2d9db1
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/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,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
@@ -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.0"
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.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-07 00:00:00.000000000 Z
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: