Skip to content

Commit db0c765

Browse files
committed
Deprecate Mailtrap::Mail::FromTemplate
1 parent 9429154 commit db0c765

File tree

5 files changed

+72
-37
lines changed

5 files changed

+72
-37
lines changed

lib/mailtrap/client.rb

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,11 @@ def initialize( # rubocop:disable Metrics/ParameterLists
5454
end
5555

5656
# Sends an email
57-
# @param mail [Mail::Base] The email to send
58-
# @return [Hash, nil] The JSON response
57+
# @param mail [#to_json] The email to send
58+
# @return [Hash] The JSON response
5959
# @!macro api_errors
6060
# @raise [Mailtrap::MailSizeError] If the message is too large
61-
# @raise [ArgumentError] If the mail is not a Mail::Base object
6261
def send(mail)
63-
raise ArgumentError, 'should be Mailtrap::Mail::Base object' unless mail.is_a? Mail::Base
64-
6562
perform_request(:post, api_host, send_path, mail)
6663
end
6764

lib/mailtrap/mail/base.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
module Mailtrap
66
module Mail
77
class Base
8-
attr_accessor :from, :to, :reply_to, :cc, :bcc, :headers, :custom_variables, :subject, :text, :html, :category
8+
attr_accessor :from, :to, :reply_to, :cc, :bcc, :headers, :custom_variables, :subject, :text, :html, :category,
9+
:template_uuid, :template_variables
910
attr_reader :attachments
1011

1112
def initialize( # rubocop:disable Metrics/ParameterLists
@@ -20,7 +21,9 @@ def initialize( # rubocop:disable Metrics/ParameterLists
2021
attachments: [],
2122
headers: {},
2223
custom_variables: {},
23-
category: nil
24+
category: nil,
25+
template_uuid: nil,
26+
template_variables: nil
2427
)
2528
@from = from
2629
@to = to
@@ -34,6 +37,8 @@ def initialize( # rubocop:disable Metrics/ParameterLists
3437
@headers = headers
3538
@custom_variables = custom_variables
3639
@category = category
40+
@template_uuid = template_uuid
41+
@template_variables = template_variables
3742
end
3843

3944
def as_json
@@ -50,7 +55,9 @@ def as_json
5055
# TODO: update headers and custom_variables with as_json method
5156
'headers' => headers,
5257
'custom_variables' => custom_variables,
53-
'category' => category
58+
'category' => category,
59+
'template_uuid' => template_uuid,
60+
'template_variables' => template_variables
5461
}.compact
5562
end
5663

lib/mailtrap/mail/from_template.rb

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
module Mailtrap
44
module Mail
5+
# @deprecated Use Mailtrap::Mail::Base
56
class FromTemplate < Base
6-
attr_accessor :template_uuid, :template_variables
7-
87
def initialize( # rubocop:disable Metrics/ParameterLists
98
from: nil,
109
to: [],
@@ -17,27 +16,7 @@ def initialize( # rubocop:disable Metrics/ParameterLists
1716
template_uuid: nil,
1817
template_variables: {}
1918
)
20-
super(
21-
from:,
22-
to:,
23-
reply_to:,
24-
cc:,
25-
bcc:,
26-
attachments:,
27-
headers:,
28-
custom_variables:
29-
)
30-
@template_uuid = template_uuid
31-
@template_variables = template_variables
32-
end
33-
34-
def as_json
35-
super.merge(
36-
{
37-
'template_uuid' => template_uuid,
38-
'template_variables' => template_variables
39-
}
40-
).compact
19+
super
4120
end
4221
end
4322
end

spec/fixtures/vcr_cassettes/Mailtrap_Client/_send/when_mail_is_hash/sends_emails.yml

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/mailtrap/client_spec.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@
5757
end
5858
end
5959

60-
context 'when mail object is not a Mailtrap::Mail::Base' do
61-
let(:mail) { 'it-a-string' }
62-
63-
it { expect { send }.to raise_error(ArgumentError, 'should be Mailtrap::Mail::Base object') }
64-
end
65-
6660
context 'with an alternative host' do
6761
let(:client) do
6862
described_class.new(api_key:, api_host: 'alternative.host.mailtrap.io', api_port: 8080)
@@ -120,6 +114,27 @@
120114
end
121115
end
122116

117+
context 'when mail is hash' do
118+
let(:mail) do
119+
{
120+
from: { email: '[email protected]', name: 'Mailtrap Test' },
121+
to: [
122+
{ email: '[email protected]' }
123+
],
124+
subject: 'You are awesome!',
125+
text: 'Congrats for sending test email with Mailtrap!',
126+
category: 'Integration Test',
127+
attachments: [
128+
{ content: Base64.strict_encode64("hello world"), filename: 'attachment.txt' }
129+
]
130+
}
131+
end
132+
133+
it 'sends emails' do
134+
expect(send).to eq({ message_ids: ['4c2446b6-e0f9-11ec-9487-0a58a9feac02'], success: true })
135+
end
136+
end
137+
123138
context 'when template' do
124139
let(:mail) do
125140
Mailtrap::Mail::FromTemplate.new(

0 commit comments

Comments
 (0)