brevo-rails 0.0.6.5 → 1.0.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: 3d7fad1139fd4c77d3008d8b7e6076e491ba9ce80ad4b46d48c1fa0cfa8334a6
4
- data.tar.gz: 4a50af13145ad5153c332f70a63e808a4991a8a0ce6857257ea4cf30c0162ea8
3
+ metadata.gz: f38c8e0d586324f814cc56cc4d4e15e3bfc25f8bad4825910ed3cd6d3a3d0d48
4
+ data.tar.gz: 563e8f88f66b57c3484d22630315f7e41816251d2d9c6f3d4654689ce3b540c0
5
5
  SHA512:
6
- metadata.gz: e015063069a4f2bb7c2d62e9d8fdd90e8a754dc723b5ffef6f988925c6129e7984003348fd9a4be4f1c481dde46c05f00ed68034982d0501c191d7e746c545b0
7
- data.tar.gz: 33fb4da44a7b890755a875ca3764d1f588ec65b011de51375efff68574238e5dccdfbbcba0cbc1dd41867bb742265a17ced6578c5bb25f2ed55158eec9840542
6
+ metadata.gz: 216694871f682f0dfeb9adf44f8601c6caa6d496aabb3cacf90f47a8c141efa101669a4ad9ce91a69c5cae6ba6f427ff2aadfbb66dc7ef3bcf8a3b1e939a7629
7
+ data.tar.gz: dd25cde5e9364be90405b4adee32de1a94e4aa64d635170ee25135701856ce76456d20952d2b67ea76a0a9b8c49210db986d3d97c161d987150825d88d897b16
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ *.gem
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # Unofficial Brevo Action Mailer adapter
2
+
3
+ brevo_rails is an Action Mailer adapter for using Brevo in Ruby on Rails application. It uses Brevo gem to make API request to Brevo service.
4
+
5
+ ## Installation
6
+
7
+ In your Gemfile
8
+
9
+ ```ruby
10
+ gem 'brevo-rails'
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ Add these lines in your configuration file.
16
+
17
+ ```ruby
18
+ config.action_mailer.delivery_method = :brevo
19
+ config.action_mailer.brevo_settings = {
20
+ api_key: ENV.fetch('BREVO_API_KEY'),
21
+ }
22
+ ```
23
+
24
+
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/brevo_rails/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "brevo-rails"
7
+ s.version = BrevoRails::VERSION
8
+ s.summary = "Send transactional email with Brevo"
9
+ s.description = "Send transactional email with Brevo"
10
+ s.authors = ["Franck D'agostini"]
11
+ s.email = "franck.dagostini@gmail.com"
12
+ s.files = ["lib/brevo_rails.rb"]
13
+ s.homepage = "https://rubygems.org/gems/brevo_rails"
14
+ s.license = "MIT"
15
+ s.required_ruby_version = '>= 2.7.0'
16
+ s.require_paths = ['lib']
17
+
18
+ s.files = `git ls-files -z`.split("\x0")
19
+
20
+ s.add_development_dependency 'debug', '~> 1.0'
21
+ s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
22
+ s.add_development_dependency 'mail', '~> 2.5', '>= 2.5.4'
23
+ s.add_runtime_dependency "brevo", "~> 2.0"
24
+ s.add_runtime_dependency "activesupport", "> 4.3"
25
+
26
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrevoRails
4
+ module ActionMailer
5
+ class DeliveryMethod
6
+ attr_accessor :settings
7
+
8
+ ALLOWED_PARAMS = %i[api_key].freeze
9
+
10
+ def initialize(settings)
11
+ self.settings = settings
12
+ end
13
+
14
+ def deliver!(message)
15
+ mail = BrevoRails::Mail.from_message(message)
16
+
17
+ api.send_transac_email(mail)
18
+ end
19
+
20
+ private
21
+
22
+ def api
23
+ @api ||= Brevo::TransactionalEmailsApi.new(api_client)
24
+ end
25
+
26
+ def api_client
27
+ @api_client ||= Brevo::ApiClient.new(configuration)
28
+ end
29
+
30
+ def configuration
31
+ @configuration ||= Brevo::Configuration.new.tap do |config|
32
+ config.api_key['api-key'] = settings.fetch(:api_key)
33
+ config.debugging = settings.fetch(:debugging, false)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrevoRails
4
+ module ActionMailer
5
+ class Railtie < Rails::Railtie
6
+ initializer 'brevo_action_mailer.add_delivery_method', before: 'action_mailer.set_configs' do
7
+ ActiveSupport.on_load(:action_mailer) do
8
+ ::ActionMailer::Base.add_delivery_method(:brevo, BrevoRails::ActionMailer::DeliveryMethod)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'action_mailer/delivery_method'
4
+ require_relative 'action_mailer/railtie' if defined? Rails
5
+
6
+ module BrevoRails
7
+ module ActionMailer; end
8
+ end
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'base64'
4
+ require 'active_support/core_ext/hash/keys'
5
+
6
+ module BrevoRails
7
+ class Mail
8
+
9
+ class << self
10
+ def from_message(message)
11
+ params = {
12
+ sender: prepare_sender(address_list(message['from'])&.addresses&.first),
13
+ to: prepare_to(address_list(message['to'])&.addresses),
14
+ subject: message.subject,
15
+ 'textContent': prepare_text_part(message),
16
+ 'htmlContent': prepare_html_part(message),
17
+ headers: prepare_headers(message),
18
+ }.stringify_keys
19
+
20
+ params['cc'] = prepare_cc(address_list(message['cc'])&.addresses) if message['cc']
21
+ params['bcc'] = prepare_bcc(address_list(message['bcc'])&.addresses) if message['bcc']
22
+ params['attachment'] = prepare_attachments(message.attachments) if message.attachments.any?
23
+
24
+ Brevo::SendSmtpEmail.new(params)
25
+ end
26
+
27
+ private
28
+
29
+ PROCESSED_HEADERS = %w[
30
+ from
31
+ to
32
+ cc
33
+ bcc
34
+ subject
35
+ ].freeze
36
+
37
+ def prepare_headers(message)
38
+ message
39
+ .header_fields
40
+ .reject { |header| PROCESSED_HEADERS.include?(header.name.downcase.delete('-')) }
41
+ .to_h { |header| [header.name, header.value] }
42
+ .compact
43
+ end
44
+
45
+ def prepare_sender(sender)
46
+ Brevo::SendSmtpEmailSender.new(prepare_address(sender))
47
+ end
48
+
49
+ def prepare_to(addresses=[])
50
+ Array(addresses).map do |address|
51
+ Brevo::SendSmtpEmailTo.new(prepare_address(address))
52
+ end
53
+ end
54
+
55
+ def prepare_cc(addresses=[])
56
+ Array(addresses).map do |address|
57
+ Brevo::SendSmtpEmailCc.new(prepare_address(address))
58
+ end
59
+ end
60
+
61
+ def prepare_bcc(addresses=[])
62
+ Array(addresses).map do |address|
63
+ Brevo::SendSmtpEmailBcc.new(prepare_address(address))
64
+ end
65
+ end
66
+
67
+ def prepare_address(address)
68
+ {
69
+ email: address.address,
70
+ name: address.display_name,
71
+ }.stringify_keys.compact
72
+ end
73
+
74
+ def address_list(header)
75
+ header.respond_to?(:element) ? header.element : header&.address_list
76
+ end
77
+
78
+ # def prepare_addresses(addresses)
79
+ # Array(addresses).map { |address| prepare_address(address) }
80
+ # end
81
+
82
+ def prepare_html_part(message)
83
+ return message.body.decoded if message.mime_type == 'text/html'
84
+
85
+ message.html_part&.decoded
86
+ end
87
+
88
+ def prepare_text_part(message)
89
+ return message.body.decoded if message.mime_type == 'text/plain' || message.mime_type.nil?
90
+
91
+ message.text_part&.decoded
92
+ end
93
+
94
+ def prepare_attachments(attachments_list = [])
95
+ attachments_list.map do |attachment|
96
+ params = {
97
+ content: Base64.strict_encode64(attachment.body.decoded),
98
+ name: attachment.filename,
99
+ }.stringify_keys
100
+ Brevo::SendSmtpEmailAttachment.new(params)
101
+ end
102
+ end
103
+
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrevoRails
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ require 'brevo_rails/action_mailer/delivery_method'
4
+
5
+ RSpec.describe BrevoRails::ActionMailer::DeliveryMethod do
6
+ let(:message) do
7
+ Mail.new do
8
+ from 'Tester <test@example.com>'
9
+ to 'you@example.com, her@example.com'
10
+ subject 'This is a test email'
11
+ end
12
+ end
13
+
14
+ describe '#initialize' do
15
+ it 'sets the api_key' do
16
+ subject = described_class.new(api_key: '123')
17
+ expect(subject.settings[:api_key]).to eq('123')
18
+ end
19
+ end
20
+
21
+ describe '#deliver!' do
22
+ it 'calls send_transac_email' do
23
+ mail = double(Mail)
24
+ expect(BrevoRails::Mail).to receive(:from_message).and_return(mail)
25
+ expect_any_instance_of(Brevo::TransactionalEmailsApi).to receive(:send_transac_email).with(mail)
26
+ described_class.new(api_key: '123').deliver!(message)
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,91 @@
1
+ require 'spec_helper'
2
+
3
+
4
+ RSpec.describe BrevoRails::Mail do
5
+ let(:text_part) do
6
+ Mail::Part.new do
7
+ body 'This is plain text'
8
+ end
9
+ end
10
+
11
+ let(:html_part) do
12
+ Mail::Part.new do
13
+ content_type 'text/html; charset=UTF-8'
14
+ body '<h1>This is HTML</h1>'
15
+ end
16
+ end
17
+
18
+ let(:message) do
19
+ Mail.new do
20
+ from 'Tester <test@example.com>'
21
+ to 'you@example.com, her@example.com'
22
+ subject 'This is a test email'
23
+ headers 'X-Custom-Header' => 'Custom Value'
24
+ end
25
+ end
26
+
27
+ let(:message_with_attachments) do
28
+ Mail.new do
29
+ from 'Tester <test@example.com>'
30
+ to 'you@example.com, her@example.com'
31
+ subject 'This is a test email'
32
+ headers 'X-Custom-Header' => 'Custom Value'
33
+ attachments['test.png'] = File.read('spec/fixtures/test.png')
34
+ end
35
+ end
36
+
37
+ before do
38
+ message.text_part = text_part
39
+ message.html_part = html_part
40
+ end
41
+
42
+ describe '.from_message' do
43
+ subject { BrevoRails::Mail.from_message(message) }
44
+
45
+ it 'returns a new instance of Brevo::SendSmtpEmail' do
46
+ expect(subject).to be_a(Brevo::SendSmtpEmail)
47
+ end
48
+
49
+ it 'sets the mail subject' do
50
+ expect(subject.subject).to eq('This is a test email')
51
+ end
52
+
53
+ it 'sets the mail sender' do
54
+ expect(subject.sender).to be_a(Brevo::SendSmtpEmailSender)
55
+ expect(subject.sender.email).to eq('test@example.com')
56
+ expect(subject.sender.name).to eq('Tester')
57
+ end
58
+
59
+ it 'sets the mail to' do
60
+ expect(subject.to).to be_a(Array)
61
+ expect(subject.to.first).to be_a(Brevo::SendSmtpEmailTo)
62
+ expect(subject.to.map(&:email)).to include('you@example.com')
63
+ expect(subject.to.map(&:email)).to include('her@example.com')
64
+ end
65
+
66
+ it 'sets the mail text content' do
67
+ expect(subject.text_content).to eq('This is plain text')
68
+ expect(subject.html_content).to eq('<h1>This is HTML</h1>')
69
+ end
70
+
71
+ it 'sets the mail headers' do
72
+ custom_header = subject.headers.fetch('X-Custom-Header')
73
+ expect(custom_header).to eq('Custom Value')
74
+ end
75
+
76
+ it 'sets attachments' do
77
+ expect(subject.attachment).to be_nil
78
+ end
79
+
80
+ context 'with attachments' do
81
+ subject { BrevoRails::Mail.from_message(message_with_attachments) }
82
+
83
+ it 'sets attachments' do
84
+ expect(subject.attachment).to be_a(Array)
85
+ expect(subject.attachment.size).to eq(1)
86
+ expect(subject.attachment.first.name).to eq('test.png')
87
+ end
88
+ end
89
+
90
+ end
91
+ end
Binary file
@@ -0,0 +1,5 @@
1
+ require 'mail'
2
+ require 'brevo_rails'
3
+
4
+ RSpec.configure do |config|
5
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brevo-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6.5
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Franck D'agostini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-07 00:00:00.000000000 Z
11
+ date: 2024-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: debug
@@ -44,6 +44,26 @@ dependencies:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
46
  version: 3.6.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: mail
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '2.5'
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: 2.5.4
57
+ type: :development
58
+ prerelease: false
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '2.5'
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 2.5.4
47
67
  - !ruby/object:Gem::Dependency
48
68
  name: brevo
49
69
  requirement: !ruby/object:Gem::Requirement
@@ -78,7 +98,19 @@ executables: []
78
98
  extensions: []
79
99
  extra_rdoc_files: []
80
100
  files:
101
+ - ".gitignore"
102
+ - README.md
103
+ - brevo_rails.gemspec
81
104
  - lib/brevo_rails.rb
105
+ - lib/brevo_rails/action_mailer.rb
106
+ - lib/brevo_rails/action_mailer/delivery_method.rb
107
+ - lib/brevo_rails/action_mailer/railtie.rb
108
+ - lib/brevo_rails/mail.rb
109
+ - lib/brevo_rails/version.rb
110
+ - spec/brevo_rails/action_mailer/delivery_method_spec.rb
111
+ - spec/brevo_rails/mail_spec.rb
112
+ - spec/fixtures/test.png
113
+ - spec/spec_helper.rb
82
114
  homepage: https://rubygems.org/gems/brevo_rails
83
115
  licenses:
84
116
  - MIT