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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7accd26109657135edc4585a488a46b3a758c7a95834720d4717bbb7bfce71f1
4
- data.tar.gz: 98ce0168009e901c94512f7dde1c16379e32722c08d726f4ed271a67f0ff8816
3
+ metadata.gz: '08a4d71d05c883b4047bcfae8d9db5739f24cecac1b339f42943df14e959b8a6'
4
+ data.tar.gz: 4edb02eb7d59a33a3ad90ad240a2433d00e6d8db319e80bf41fbdddc5d6bd76c
5
5
  SHA512:
6
- metadata.gz: 3879514eb421eeb9aca7c11bd3f3abb14cbc68f3f65ec75a4be670e37852e016abfbe538bde6ebdaabcdbdb7c6ef10c7686ac1a8ffa19391967d28c5da6b7e07
7
- data.tar.gz: b89045584d9474301687ad800b4198b5de5959939760814c85d8a7b06785f11c5fe4e01b6624dfb8e695346694f50ca904c6f1ddd4a7e8a2f2b67f3342ff38cb
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
@@ -1,4 +1,4 @@
1
- Copyright Frank Groeneveld
1
+ Copyright Toolsfactory B.V.
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
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.1"
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,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.1.1
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-01 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: