Skip to content

Commit b90d58d

Browse files
authored
Merge pull request #8 from railsware/mail-class-refactoring
Refactor Mail class: move to the top level to be used with both sandbox and sending APIs
2 parents b8f34c8 + 0efa545 commit b90d58d

File tree

22 files changed

+134
-176
lines changed

22 files changed

+134
-176
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Or install it yourself as:
3030
require 'mailtrap'
3131

3232
# create mail object
33-
mail = Mailtrap::Sending::Mail.new(
33+
mail = Mailtrap::Mail::Base.new(
3434
from: { email: '[email protected]', name: 'Mailtrap Test' },
3535
to: [
3636
{ email: '[email protected]' }
@@ -50,7 +50,7 @@ client.send(mail)
5050
require 'mailtrap'
5151
require 'base64'
5252

53-
mail = Mailtrap::Sending::Mail.new(
53+
mail = Mailtrap::Mail::Base.new(
5454
from: { email: '[email protected]', name: 'Mailtrap Test' },
5555
to: [
5656
{ email: '[email protected]', name: 'Your name' }
@@ -93,7 +93,7 @@ client.send(mail)
9393
require 'mailtrap'
9494

9595
# create mail object
96-
mail = Mailtrap::Sending::MailFromTemplate.new(
96+
mail = Mailtrap::Mail::FromTemplate.new(
9797
from: { email: '[email protected]', name: 'Mailtrap Test' },
9898
to: [
9999
{ email: '[email protected]' }

lib/mailtrap.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative 'mailtrap/action_mailer' if defined? ActionMailer
4+
require_relative 'mailtrap/mail'
45
require_relative 'mailtrap/sending'
56
require_relative 'mailtrap/version'
67

lib/mailtrap/action_mailer/delivery_method.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize(settings)
1010
end
1111

1212
def deliver!(message)
13-
mail = Mailtrap::Sending::Convert.from_message(message)
13+
mail = Mailtrap::Mail.from_message(message)
1414

1515
client.send(mail)
1616
end

lib/mailtrap/mail.rb

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# frozen_string_literal: true
2+
3+
require 'base64'
4+
5+
require_relative 'mail/base'
6+
require_relative 'mail/from_template'
7+
8+
module Mailtrap
9+
module Mail
10+
class << self
11+
def from_message(message) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity
12+
Mailtrap::Mail::Base.new(
13+
from: prepare_address(address_list(message['from'])&.addresses&.first),
14+
to: prepare_addresses(address_list(message['to'])&.addresses),
15+
cc: prepare_addresses(address_list(message['cc'])&.addresses),
16+
bcc: prepare_addresses(address_list(message['bcc'])&.addresses),
17+
subject: message.subject,
18+
text: prepare_text_part(message),
19+
html: prepare_html_part(message),
20+
headers: prepare_headers(message),
21+
attachments: prepare_attachments(message.attachments),
22+
category: message['category']&.unparsed_value,
23+
custom_variables: message['custom_variables']&.unparsed_value
24+
)
25+
end
26+
27+
private
28+
29+
PROCESSED_HEADERS = %w[
30+
from
31+
to
32+
cc
33+
bcc
34+
subject
35+
category
36+
customvariables
37+
contenttype
38+
].freeze
39+
40+
def address_list(header)
41+
header.respond_to?(:element) ? header.element : header&.address_list
42+
end
43+
44+
def prepare_addresses(addresses)
45+
Array(addresses).map { |address| prepare_address(address) }
46+
end
47+
48+
def prepare_headers(message)
49+
message
50+
.header_fields
51+
.reject { |header| PROCESSED_HEADERS.include?(header.name.downcase.delete('-')) }
52+
.to_h { |header| [header.name, header.value] }
53+
.compact
54+
end
55+
56+
def prepare_address(address)
57+
{
58+
email: address.address,
59+
name: address.display_name
60+
}.compact
61+
end
62+
63+
def prepare_attachments(attachments_list = [])
64+
attachments_list.map do |attachment|
65+
{
66+
content: Base64.strict_encode64(attachment.body.decoded),
67+
type: attachment.mime_type,
68+
filename: attachment.filename,
69+
disposition: attachment.header[:content_disposition]&.disposition_type,
70+
content_id: attachment&.cid
71+
}.compact
72+
end
73+
end
74+
75+
def prepare_html_part(message)
76+
return message.body.decoded if message.mime_type == 'text/html'
77+
78+
message.html_part&.decoded
79+
end
80+
81+
def prepare_text_part(message)
82+
return message.body.decoded if message.mime_type == 'text/plain' || message.mime_type.nil?
83+
84+
message.text_part&.decoded
85+
end
86+
end
87+
end
88+
end

lib/mailtrap/sending/base.rb renamed to lib/mailtrap/mail/base.rb

+19-7
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,51 @@
33
require 'json'
44

55
module Mailtrap
6-
module Sending
6+
module Mail
77
class Base
8-
attr_accessor :from, :to, :cc, :bcc, :headers, :custom_variables
8+
attr_accessor :from, :to, :cc, :bcc, :headers, :custom_variables, :subject, :text, :html, :category
99
attr_reader :attachments
1010

11-
def initialize( # rubocop:disable Metrics/ParameterLists
11+
def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength
1212
from: nil,
1313
to: [],
1414
cc: [],
1515
bcc: [],
16+
subject: nil,
17+
text: nil,
18+
html: nil,
1619
attachments: [],
1720
headers: {},
18-
custom_variables: {}
21+
custom_variables: {},
22+
category: nil
1923
)
2024
@from = from
2125
@to = to
2226
@cc = cc
2327
@bcc = bcc
28+
@subject = subject
29+
@text = text
30+
@html = html
2431
self.attachments = attachments
2532
@headers = headers
2633
@custom_variables = custom_variables
34+
@category = category
2735
end
2836

29-
def as_json
37+
def as_json # rubocop:disable Metrics/MethodLength
3038
{
31-
'to' => to,
3239
'from' => from,
40+
'to' => to,
3341
'cc' => cc,
3442
'bcc' => bcc,
43+
'subject' => subject,
44+
'text' => text,
45+
'html' => html,
3546
'attachments' => attachments.map(&:as_json),
3647
# TODO: update headers and custom_variables with as_json method
3748
'headers' => headers,
38-
'custom_variables' => custom_variables
49+
'custom_variables' => custom_variables,
50+
'category' => category
3951
}.compact
4052
end
4153

lib/mailtrap/sending/mail_from_template.rb renamed to lib/mailtrap/mail/from_template.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# frozen_string_literal: true
22

33
module Mailtrap
4-
module Sending
5-
class MailFromTemplate < Base
4+
module Mail
5+
class FromTemplate < Base
66
attr_accessor :template_uuid, :template_variables
77

88
def initialize( # rubocop:disable Metrics/ParameterLists, Metrics/MethodLength

lib/mailtrap/sending.rb

-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# frozen_string_literal: true
22

33
require_relative 'sending/attachment'
4-
require_relative 'sending/base'
54
require_relative 'sending/client'
6-
require_relative 'sending/convert'
7-
require_relative 'sending/mail'
8-
require_relative 'sending/mail_from_template'
95

106
module Mailtrap
117
module Sending

lib/mailtrap/sending/client.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def initialize(api_key: ENV.fetch('MAILTRAP_API_KEY'), api_host: API_HOST, api_p
1919
end
2020

2121
def send(mail)
22-
raise ArgumentError, 'should be Mailtrap::Sending::Base object' unless mail.is_a? Base
22+
raise ArgumentError, 'should be Mailtrap::Mail::Base object' unless mail.is_a? Mail::Base
2323

2424
request = post_request('/api/send', mail.to_json)
2525
response = http_client.request(request)

lib/mailtrap/sending/convert.rb

-87
This file was deleted.

lib/mailtrap/sending/mail.rb

-48
This file was deleted.

spec/fixtures/vcr_cassettes/Mailtrap_ActionMailer_DeliveryMethod/_deliver_/converts_the_message_and_sends_via_API.yml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/Mailtrap_Sending_Client/_send/when_mail/when_all_params_are_set/sending_is_successful.yml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/Mailtrap_Sending_Client/_send/when_mail/when_api_key_is_incorrect/raises_authorization_error_with_array_of_errors.yml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/fixtures/vcr_cassettes/Mailtrap_Sending_Client/_send/when_mail/when_no_subject_and_no_text_set/raises_sending_error_with_array_of_errors.yml

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)