mail_plugger 1.3.0 → 1.4.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: f909bb4d7136983266dbbe01519966ec0a3f5166cd7576401de5129e8fb6d255
4
- data.tar.gz: 79a650bdbe95199bb28ff1dddefbf405d09e2dcf661d8bb0557b7c4594d85cc4
3
+ metadata.gz: a4e017acfdc5bd84be87de9bb9b213dbeaf5356436b896be22c2fdd8cf0f484e
4
+ data.tar.gz: 75dfedda8f0d97436469c5cbdb29b8316cb1e00a9a479168950b342870c4cf2b
5
5
  SHA512:
6
- metadata.gz: 82416b31f9a56713434d37055caf55c83d361ea0bb22f1d643116106bc13cb5803974d9f89945ef938471fb63626742299353f40673df24f49f2ef098172daee
7
- data.tar.gz: 2ee3ad7f530aad9e8dcf864bef0dc17c3dd23fea193fa796cffd676bf917f11d36a0af95632edb46fd2bb0267c12035f95b4783dd80fea1cff2125d5fcfc90f6
6
+ metadata.gz: a7229b6a397f883683331bb341be7356061367362bfd8d8d2f3c82b46c9a774355c60ba85d643b19cf25284e6335c46f6bdcd06aa821613662bec7ff04fde833
7
+ data.tar.gz: dbe3dcd8e421074b5df9d1144672b1cebe852e3a06f46d6273505ca0833fe31f650bfe8e5d40ccc57cff17e698a75ac84af3821b2f5b07572bd559e70241eebb
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Change log
2
2
 
3
+ ## 1.4.0 (2021-06-08)
4
+
5
+ ### New features
6
+
7
+ * Add SMTP support. With this modifications we can use SMTP and API delivery methods as well.
8
+
9
+ ### Changes
10
+
11
+ * Update bundler and gems.
12
+
13
+
3
14
  ## 1.3.0 (2021-04-14)
4
15
 
5
16
  ### New features
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  [![Maintainability](https://api.codeclimate.com/v1/badges/137881380fc475b4a836/maintainability)](https://codeclimate.com/github/MailToolbox/mail_plugger/maintainability)
8
8
  [![Test Coverage](https://api.codeclimate.com/v1/badges/137881380fc475b4a836/test_coverage)](https://codeclimate.com/github/MailToolbox/mail_plugger/test_coverage)
9
9
 
10
- **MailPlugger** helps you to use different mail providers' **API**. You can use any APIs which one would like to use. It allows you to send different emails with different APIs. Also it can help to move between providers, load balacing or cost management.
10
+ **MailPlugger** helps you to use different mail providers' **SMTP** and **API**. You can use any SMTPs and APIs which one would like to use. It allows you to send different emails with different SMTPs and APIs. Also it can help to move between providers, load balacing or cost management.
11
11
 
12
12
  ## Installation
13
13
 
@@ -31,16 +31,18 @@ Or install it yourself as:
31
31
  - [How to use MailPlugger::DeliveryMethod class](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_delivery_method.md)
32
32
  - [How to use MailPlugger in a Ruby script or IRB console](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_in_script_or_console.md)
33
33
  - [How to use MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_in_ruby_on_rails.md)
34
- - [How to add API specific options to the mailer method in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_special_options_in_ruby_on_rails.md)
35
- - [How to add attachments to the mailer method in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_attachments_in_ruby_on_rails.md)
36
- - [How to use more delivey systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_more_delivery_system_in_ruby_on_rails.md)
37
- - [How to use one delivey system with more send methods in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_one_delivery_system_with_more_send_methods_in_ruby_on_rails.md)
38
- - [How to use AWS SES with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_aws_ses_in_ruby_on_rails.md)
39
- - [How to use SparkPost with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_sparkpost_in_ruby_on_rails.md)
40
- - [How to use SendGrid with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_sendgrid_in_ruby_on_rails.md)
41
- - [How to use Mandrill with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_mandrill_in_ruby_on_rails.md)
42
- - [How to use Postmark with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_postmark_in_ruby_on_rails.md)
43
- - [How to use Mailgun with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_mailgun_in_ruby_on_rails.md)
34
+ - [How to use more SMTP delivery systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_more_smtp_delivery_systems_in_ruby_on_rails.md)
35
+ - [How to combine SMTP and API delivery systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_smtp_and_api_delivery_systems_in_ruby_on_rails.md)
36
+ - [How to add API specific options to the mailer method in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_api_specific_options_in_ruby_on_rails.md)
37
+ - [How to use API delivery system that add attachments to the mailer method in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_api_attachments_in_ruby_on_rails.md)
38
+ - [How to use more API delivery systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_more_api_delivery_systems_in_ruby_on_rails.md)
39
+ - [How to use one API delivery system with more send methods in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_one_api_delivery_system_with_more_send_methods_in_ruby_on_rails.md)
40
+ - [How to use AWS SES API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_aws_ses_api_in_ruby_on_rails.md)
41
+ - [How to use SparkPost API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_sparkpost_api_in_ruby_on_rails.md)
42
+ - [How to use SendGrid API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_sendgrid_api_in_ruby_on_rails.md)
43
+ - [How to use Mandrill API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_mandrill_api_in_ruby_on_rails.md)
44
+ - [How to use Postmark API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_postmark_api_in_ruby_on_rails.md)
45
+ - [How to use Mailgun API with MailPlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_mailgun_api_in_ruby_on_rails.md)
44
46
 
45
47
  # F<img src="https://raw.githubusercontent.com/MailToolbox/mail_plugger/main/images/fake_plugger800x500.png" height="22" />kePlugger
46
48
 
@@ -51,6 +53,7 @@ Or install it yourself as:
51
53
  - [How to use FakePlugger::DeliveryMethod class](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_delivery_method.md)
52
54
  - [How to use FakePlugger in a Ruby script or IRB console](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_in_script_or_console.md)
53
55
  - [How to use FakePlugger in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_in_ruby_on_rails.md)
56
+ - [How to use FakePlugger with more delivery systems in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_with_more_delivery_systems_in_ruby_on_rails.md)
54
57
  - [How to use FakePlugger with MailGrabber in Ruby on Rails](https://github.com/MailToolbox/mail_plugger/blob/main/docs/usage_of_fake_plugger_with_mail_grabber_in_ruby_on_rails.md)
55
58
 
56
59
  ## Development
@@ -38,15 +38,20 @@ module FakePlugger
38
38
  def initialize(options = {})
39
39
  super
40
40
 
41
- @debug = options[:debug] ||
42
- settings[:fake_plugger_debug] || false
43
- @raw_message = options[:raw_message] ||
44
- settings[:fake_plugger_raw_message] || false
45
- @response = options[:response] || settings[:fake_plugger_response]
46
- @use_mail_grabber = options[:use_mail_grabber] ||
47
- settings[:fake_plugger_use_mail_grabber] || false
41
+ @debug = options[:debug]
42
+
43
+ @raw_message = options[:raw_message]
44
+
45
+ @response = options[:response]
46
+
47
+ @use_mail_grabber = options[:use_mail_grabber]
48
48
  end
49
49
 
50
+ # Using SMTP:
51
+ # Mock send message via SMTP protocol if the 'delivery_settings' contains a
52
+ # 'smtp_settings' key and the value is a hash with the settings.
53
+ #
54
+ # Using API:
50
55
  # Mock send message with the given client if the message parameter is a
51
56
  # Mail::Message object. If 'response' parameter is nil then it will extract
52
57
  # those information from the Mail::Message object which was provided in the
@@ -55,6 +60,8 @@ module FakePlugger
55
60
  # it won't call the 'deliver' method.
56
61
  # If the 'response' parameter is a hash with 'return_delivery_data: true'
57
62
  # then it will retrun with the extracted delivery data.
63
+ #
64
+ #
58
65
  # If the 'response' parameter is not nil then retruns with that given data
59
66
  # without call any other methods.
60
67
  # Except if 'debug' is true. In this case it will call those methods which
@@ -69,6 +76,48 @@ module FakePlugger
69
76
  #
70
77
  # @example
71
78
  #
79
+ # # Using SMTP:
80
+ #
81
+ # MailPlugger.plug_in('test_smtp_client') do |smtp|
82
+ # smtp.delivery_settings = {
83
+ # smtp_settings: {
84
+ # address: 'smtp.server.com',
85
+ # port: 587,
86
+ # domain: 'test.domain.com',
87
+ # enable_starttls_auto: true,
88
+ # user_name: 'test_user',
89
+ # password: '1234',
90
+ # authentication: :plain
91
+ # }
92
+ # }
93
+ # end
94
+ #
95
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
96
+ # subject: 'Test email', body: 'Test email body')
97
+ #
98
+ # FakePlugger::DeliveryMethod.new.deliver!(message)
99
+ #
100
+ # # or
101
+ #
102
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
103
+ # subject: 'Test email', body: 'Test email body')
104
+ #
105
+ # FakePlugger::DeliveryMethod.new(
106
+ # delivery_settings: {
107
+ # smtp_settings: {
108
+ # address: 'smtp.server.com',
109
+ # port: 587,
110
+ # domain: 'test.domain.com',
111
+ # enable_starttls_auto: true,
112
+ # user_name: 'test_user',
113
+ # password: '1234',
114
+ # authentication: :plain
115
+ # }
116
+ # }
117
+ # ).deliver!(message)
118
+ #
119
+ # # Using API:
120
+ #
72
121
  # MailPlugger.plug_in('test_api_client') do |api|
73
122
  # api.delivery_options = %i[from to subject body]
74
123
  # api.delivery_settings = {
@@ -107,6 +156,8 @@ module FakePlugger
107
156
 
108
157
  @message = message
109
158
 
159
+ update_settings
160
+
110
161
  call_extra_options
111
162
 
112
163
  return_with_response
@@ -115,7 +166,7 @@ module FakePlugger
115
166
  private
116
167
 
117
168
  # Call extra options like show debug informations, show raw message,
118
- # use mail grabber
169
+ # use mail grabber.
119
170
  def call_extra_options
120
171
  show_debug_info if @debug
121
172
  show_raw_message if @raw_message
@@ -125,18 +176,8 @@ module FakePlugger
125
176
  MailGrabber::DeliveryMethod.new.deliver!(@message)
126
177
  end
127
178
 
128
- # Return with a response which depends on the conditions.
129
- def return_with_response
130
- return client.new(delivery_data) if @response.nil?
131
- if @response.is_a?(Hash) && @response[:return_delivery_data]
132
- return delivery_data
133
- end
134
-
135
- @response
136
- end
137
-
138
- # Show debug informations from variables and methods.
139
- def show_debug_info
179
+ # Debug informations for API
180
+ def debug_info_for_api
140
181
  puts <<~DEBUG_INFO
141
182
 
142
183
  ===================== FakePlugger::DeliveryMethod =====================
@@ -170,6 +211,62 @@ module FakePlugger
170
211
  DEBUG_INFO
171
212
  end
172
213
 
214
+ # Debug information for SMTP
215
+ def debug_info_for_smtp
216
+ puts <<~DEBUG_INFO
217
+
218
+ ===================== FakePlugger::DeliveryMethod =====================
219
+
220
+ ------------------------------ Variables ------------------------------
221
+
222
+ ==> @delivery_settings: #{@delivery_settings.inspect}
223
+
224
+ ==> @default_delivery_system: #{@default_delivery_system.inspect}
225
+
226
+ ==> @message: #{@message.inspect}
227
+
228
+ ------------------------------- Methods -------------------------------
229
+
230
+ ==> delivery_system: #{delivery_system.inspect}
231
+
232
+ ==> settings: #{settings.inspect}
233
+
234
+ =======================================================================
235
+
236
+ DEBUG_INFO
237
+ end
238
+
239
+ # Prepare delivery. It depends on that is SMTP or API.
240
+ def prepare_delivery
241
+ if send_via_smtp?
242
+ @message.delivery_method :smtp, settings[:smtp_settings]
243
+ @message
244
+ else
245
+ client.new(delivery_data)
246
+ end
247
+ end
248
+
249
+ # Check that it should retrun with the delivery data.
250
+ def return_delivery_data?
251
+ !send_via_smtp? &&
252
+ @response.is_a?(Hash) &&
253
+ @response[:return_delivery_data]
254
+ end
255
+
256
+ # Return with a response which depends on the conditions.
257
+ def return_with_response
258
+ return prepare_delivery if @response.nil?
259
+
260
+ return delivery_data if return_delivery_data?
261
+
262
+ @response
263
+ end
264
+
265
+ # Show debug informations from variables and methods.
266
+ def show_debug_info
267
+ send_via_smtp? ? debug_info_for_smtp : debug_info_for_api
268
+ end
269
+
173
270
  # Show raw message for debug purpose.
174
271
  def show_raw_message
175
272
  puts <<~RAW_MESSAGE
@@ -182,5 +279,18 @@ module FakePlugger
182
279
 
183
280
  RAW_MESSAGE
184
281
  end
282
+
283
+ # Extract settings values and update attributes.
284
+ def update_settings
285
+ # rubocop:disable Naming/MemoizedInstanceVariableName
286
+ @debug ||= settings[:fake_plugger_debug] || false
287
+
288
+ @raw_message ||= settings[:fake_plugger_raw_message] || false
289
+
290
+ @response ||= settings[:fake_plugger_response]
291
+
292
+ @use_mail_grabber ||= settings[:fake_plugger_use_mail_grabber] || false
293
+ # rubocop:enable Naming/MemoizedInstanceVariableName
294
+ end
185
295
  end
186
296
  end
data/lib/mail_plugger.rb CHANGED
@@ -13,14 +13,35 @@ require 'fake_plugger/railtie' if defined?(Rails)
13
13
 
14
14
  module MailPlugger
15
15
  class << self
16
- attr_reader :client, :delivery_options, :delivery_settings
16
+ attr_reader :client,
17
+ :delivery_options,
18
+ :delivery_settings,
19
+ :delivery_systems
17
20
 
18
- # Plug in defined API(s) class.
21
+ # Plug in SMTP(s) or defined API(s) class.
19
22
  #
20
- # @param [String/Symbol] delivery_system the name of the API
23
+ # @param [String/Symbol] delivery_system the name of the SMTP/API
21
24
  #
22
25
  # @example using Rails `config/initializers/mail_plugger.rb`
23
26
  #
27
+ # # Using SMTP:
28
+ #
29
+ # MailPlugger.plug_in('smtp_client') do |smtp|
30
+ # smtp.delivery_settings = {
31
+ # smtp_settings: {
32
+ # address: 'smtp.server.com',
33
+ # port: 587,
34
+ # domain: 'test.domain.com',
35
+ # enable_starttls_auto: true,
36
+ # user_name: 'test_user',
37
+ # password: '1234',
38
+ # authentication: :plain
39
+ # }
40
+ # }
41
+ # end
42
+ #
43
+ # # Using API:
44
+ #
24
45
  # # The defined API class should have an 'initialize' and a 'deliver'
25
46
  # # method.
26
47
  # class DefinedApiClientClass
@@ -75,6 +96,7 @@ module MailPlugger
75
96
  check_value(delivery_system)
76
97
 
77
98
  @delivery_system = delivery_system
99
+ (@delivery_systems ||= []) << delivery_system
78
100
 
79
101
  yield self
80
102
  rescue NoMethodError => e
@@ -83,7 +105,7 @@ module MailPlugger
83
105
 
84
106
  # Define 'client', 'delivery_options' and 'delivery_settings' setter
85
107
  # methods. These methods are generating a hash where the key is the
86
- # 'delivery_system'. This let us to set/use more than one API.
108
+ # 'delivery_system'. This let us to set/use more than one STMP/API.
87
109
  %w[client delivery_options delivery_settings].each do |method|
88
110
  define_method "#{method}=" do |value|
89
111
  variable = instance_variable_get("@#{method}")
@@ -97,7 +119,7 @@ module MailPlugger
97
119
  # Check 'delivery_system' is valid or not. If it's not valid then
98
120
  # it will raise an error.
99
121
  #
100
- # @param [String/Symbol] delivery_system the name of the API
122
+ # @param [String/Symbol] delivery_system the name of the SMTP/API
101
123
  def check_value(delivery_system)
102
124
  if delivery_system.nil?
103
125
  raise Error::WrongDeliverySystem, '"delivery_system" is nil'
@@ -30,12 +30,19 @@ module MailPlugger
30
30
  @delivery_settings = options[:delivery_settings] ||
31
31
  MailPlugger.delivery_settings
32
32
 
33
+ @delivery_systems = MailPlugger.delivery_systems
34
+
33
35
  @default_delivery_system = options[:default_delivery_system] ||
34
36
  default_delivery_system_get
35
37
 
36
38
  @message = nil
37
39
  end
38
40
 
41
+ # Using SMTP:
42
+ # Send message via SMTP protocol if the 'delivery_settings' contains a
43
+ # 'smtp_settings' key and the value is a hash with the settings.
44
+ #
45
+ # Using API:
39
46
  # Send message with the given client if the message parameter is a
40
47
  # Mail::Message object. Before doing that extract those information from the
41
48
  # Mail::Message object which was provided in the 'delivery_options'. After
@@ -44,10 +51,52 @@ module MailPlugger
44
51
  #
45
52
  # @param [Mail::Message] message what we would like to send
46
53
  #
47
- # @return [Mail::Message/Hash] depend on delivery_settings and method calls
54
+ # @return [Mail::Message/Hash] depends on delivery_settings and method calls
48
55
  #
49
56
  # @example
50
57
  #
58
+ # # Using SMTP:
59
+ #
60
+ # MailPlugger.plug_in('test_smtp_client') do |smtp|
61
+ # smtp.delivery_settings = {
62
+ # smtp_settings: {
63
+ # address: 'smtp.server.com',
64
+ # port: 587,
65
+ # domain: 'test.domain.com',
66
+ # enable_starttls_auto: true,
67
+ # user_name: 'test_user',
68
+ # password: '1234',
69
+ # authentication: :plain
70
+ # }
71
+ # }
72
+ # end
73
+ #
74
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
75
+ # subject: 'Test email', body: 'Test email body')
76
+ #
77
+ # MailPlugger::DeliveryMethod.new.deliver!(message)
78
+ #
79
+ # # or
80
+ #
81
+ # message = Mail.new(from: 'from@example.com', to: 'to@example.com',
82
+ # subject: 'Test email', body: 'Test email body')
83
+ #
84
+ # MailPlugger::DeliveryMethod.new(
85
+ # delivery_settings: {
86
+ # smtp_settings: {
87
+ # address: 'smtp.server.com',
88
+ # port: 587,
89
+ # domain: 'test.domain.com',
90
+ # enable_starttls_auto: true,
91
+ # user_name: 'test_user',
92
+ # password: '1234',
93
+ # authentication: :plain
94
+ # }
95
+ # }
96
+ # ).deliver!(message)
97
+ #
98
+ # # Using API:
99
+ #
51
100
  # MailPlugger.plug_in('test_api_client') do |api|
52
101
  # api.delivery_options = %i[from to subject body]
53
102
  # api.client = DefinedApiClientClass
@@ -76,7 +125,12 @@ module MailPlugger
76
125
 
77
126
  @message = message
78
127
 
79
- client.new(delivery_data).deliver
128
+ if send_via_smtp?
129
+ message.delivery_method :smtp, settings[:smtp_settings]
130
+ message.deliver!
131
+ else
132
+ client.new(delivery_data).deliver
133
+ end
80
134
  end
81
135
  end
82
136
  end
@@ -5,6 +5,15 @@ require 'mail/indifferent_hash'
5
5
 
6
6
  module MailPlugger
7
7
  module MailHelper
8
+ DELIVERY_SETTINGS_KEYS = %i[
9
+ fake_plugger_debug
10
+ fake_plugger_raw_message
11
+ fake_plugger_response
12
+ fake_plugger_use_mail_grabber
13
+ return_response
14
+ smtp_settings
15
+ ].freeze
16
+
8
17
  # Check the version of a gem.
9
18
  #
10
19
  # @param [String] gem_name the name of the gem
@@ -64,11 +73,15 @@ module MailPlugger
64
73
  end
65
74
 
66
75
  # Tries to set up a default delivery system, if the 'delivery_system'
67
- # wasn't defined in the Mail::Message object and 'delivery_options' and/or
68
- # 'client' is a hash. Which means the MailPlugger.plugin method was used,
69
- # probably.
76
+ # wasn't defined in the Mail::Message object and we're using
77
+ # MailPlugger.plug_in then it returns with first key of the stored
78
+ # 'delivery_systems'.
79
+ # When we are't using MailPlugger.plug_in method and 'delivery_options',
80
+ # 'client' and/or 'delivery_settings' is a hash, then it tries to get the
81
+ # 'delivery_system' from the hashes.
82
+ # Otherwise it returns with nil.
70
83
  #
71
- # @return [Stirng] the first key of the 'delivery_options' or 'client'
84
+ # @return [Stirng/NilClass] the first key from the extracted keys or nil
72
85
  def default_delivery_system_get
73
86
  extract_keys&.first
74
87
  end
@@ -91,7 +104,8 @@ module MailPlugger
91
104
 
92
105
  # Extract 'delivery_system' from the Mail::Message object or if it's not
93
106
  # defined then use the default one. If it's still nil and one of the
94
- # 'delivery_options' or 'client' is a hash then raise error.
107
+ # 'delivery_options', 'client' and/or 'delivery_settings' is a hash and
108
+ # 'delivery_settings' doesn't contain 'delivery_system' then raise error.
95
109
  #
96
110
  # @return [String] with the name of the delivery system
97
111
  def delivery_system
@@ -104,12 +118,13 @@ module MailPlugger
104
118
  @delivery_system
105
119
  end
106
120
 
107
- # Check the given 'delivery_options' or 'client' are hashes and
108
- # if one of that does then check the 'delivery_system' is valid or not.
121
+ # Check the given 'delivery_options', 'client' and 'delivery_settings' are
122
+ # hashes and if one of that does then check the 'delivery_system' is valid
123
+ # or not.
109
124
  # If the given 'delivery_system' is nil or doesn't match with extracted keys
110
125
  # then it will raise error.
111
126
  def delivery_system_value_check
112
- return unless @delivery_options.is_a?(Hash) || @client.is_a?(Hash)
127
+ return unless need_delivery_system?
113
128
 
114
129
  if @delivery_system.nil?
115
130
  raise Error::WrongDeliverySystem,
@@ -122,6 +137,17 @@ module MailPlugger
122
137
  "\"delivery_system\" '#{@delivery_system}' does not exist"
123
138
  end
124
139
 
140
+ # Check that 'delivery_settings' has 'delivery_system' key or not.
141
+ # If 'delivery_settings' contains 'DELIVERY_SETTINGS_KEYS' then it retruns
142
+ # false, else true.
143
+ #
144
+ # @return [Boolean] true/false
145
+ def exclude_delivey_settings_keys?
146
+ @delivery_settings.keys.none? do |key|
147
+ DELIVERY_SETTINGS_KEYS.include?(key.to_sym)
148
+ end
149
+ end
150
+
125
151
  # Extract attachments.
126
152
  #
127
153
  # @return [Array] with extracted attachment hashes
@@ -137,15 +163,28 @@ module MailPlugger
137
163
  end
138
164
  end
139
165
 
140
- # Extract keys from 'delivery_options' or 'client', depends on which is a
141
- # hash. If none of these are hashes then returns nil.
166
+ # Return 'delivery_systems' array if it's exist. If not then extract keys
167
+ # from 'delivery_options', 'client' or 'delivery_settings',
168
+ # depends on which is a hash. If none of these are hashes then returns nil.
142
169
  #
143
- # @return [Array/NilClass] with the keys of 'delivery_options' or 'client'
170
+ # @return [Array/NilClass] with the keys or nil
144
171
  def extract_keys
172
+ return @delivery_systems unless @delivery_systems.nil?
173
+
174
+ extract_keys_from_other_variables
175
+ end
176
+
177
+ # Extract keys from 'delivery_options', 'client' or 'delivery_settings',
178
+ # depends on which is a hash. If none of these are hashes then returns nil.
179
+ #
180
+ # @return [Array/NilClass] with the keys from one of the hash or nil
181
+ def extract_keys_from_other_variables
145
182
  if @delivery_options.is_a?(Hash)
146
183
  @delivery_options
147
184
  elsif @client.is_a?(Hash)
148
185
  @client
186
+ elsif @delivery_settings.is_a?(Hash) && exclude_delivey_settings_keys?
187
+ @delivery_settings
149
188
  end&.keys
150
189
  end
151
190
 
@@ -174,6 +213,17 @@ module MailPlugger
174
213
  message_field.public_send(*mail_field_value)
175
214
  end
176
215
 
216
+ # Check if either 'deliviery_options' or 'client' is a hash.
217
+ # Or delivery_settings is a hash but not contains 'DELIVERY_SETTINGS_KEYS'
218
+ # in first level.
219
+ #
220
+ # @return [Boolean] true/false
221
+ def need_delivery_system?
222
+ @delivery_options.is_a?(Hash) ||
223
+ @client.is_a?(Hash) ||
224
+ (@delivery_settings.is_a?(Hash) && exclude_delivey_settings_keys?)
225
+ end
226
+
177
227
  # Extract the value from the given options.
178
228
  #
179
229
  # @param [Hash/Array/Class] option
@@ -187,11 +237,22 @@ module MailPlugger
187
237
  end
188
238
  end
189
239
 
240
+ # Check that settings contains any SMTP related settings.
241
+ #
242
+ # @return [Boolean] true/false
243
+ def send_via_smtp?
244
+ return true if settings[:smtp_settings].is_a?(Hash) &&
245
+ settings[:smtp_settings].any?
246
+
247
+ false
248
+ end
249
+
190
250
  # Extract 'settings'. If it's a hash then it'll return the right
191
- # settings belongs to the delivery system. If it's not a hash it'll return
192
- # the given value. But if the value doesn't a hash it'll raise an error.
251
+ # settings belongs to the delivery system. If 'delivery_settings' is nil
252
+ # it'll return an empty hash. But if the value doesn't a hash it'll raise
253
+ # an error.
193
254
  #
194
- # @return [Hash] settings for Mail delivery_method
255
+ # @return [Hash] settings for Mail delivery_method and/or FakePlugger
195
256
  def settings
196
257
  @settings ||= option_value_from(@delivery_settings)
197
258
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MailPlugger
4
- VERSION = '1.3.0'
4
+ VERSION = '1.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail_plugger
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Norbert Szivós
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-14 00:00:00.000000000 Z
11
+ date: 2021-06-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -24,8 +24,8 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.5'
27
- description: Delivery Method to send emails via API(s). We can use this Delivery Method
28
- with Ruby on Rails ActionMailer or other solutions.
27
+ description: Delivery Method to send emails via SMTP(s) and API(s). We can use this
28
+ Delivery Method with Ruby on Rails ActionMailer or other solutions.
29
29
  email:
30
30
  - sysqa@yahoo.com
31
31
  executables: []
@@ -72,5 +72,5 @@ requirements: []
72
72
  rubygems_version: 3.1.4
73
73
  signing_key:
74
74
  specification_version: 4
75
- summary: Plug in required mailer API(s) with MailPlugger.
75
+ summary: Plug in required mailer SMTP(s) and API(s) with MailPlugger.
76
76
  test_files: []