Module: MailPlugger::MailHelper

Included in:
DeliveryMethod
Defined in:
lib/mail_plugger/mail_helper.rb

Constant Summary collapse

DELIVERY_SETTINGS_KEYS =
%i[
  fake_plugger_debug
  fake_plugger_raw_message
  fake_plugger_response
  fake_plugger_use_mail_grabber
  return_response
  smtp_settings
].freeze
SENDING_METHODS =
%i[
  default_delivery_system
  plugged_in_first
  random
  round_robin
].freeze

Instance Method Summary collapse

Instance Method Details

#clientClass

Extract ‘client’. If it’s a hash, then it’ll return the right client that belongs to the delivery system. If it’s not a hash, it’ll return the given value. But if the value isn’t a class, it’ll raise an error.

Returns:

  • (Class)

    the defined API class



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/mail_plugger/mail_helper.rb', line 42

def client
  api_client = option_value_from(@client)

  unless api_client.is_a?(Class)
    raise Error::WrongApiClient, '"client" does not a Class'
  end
  unless api_client.method_defined?(:deliver)
    raise Error::WrongApiClient, '"client" does not have "deliver" method'
  end

  api_client
end

#default_dataHash

Extract ‘default_delivery_options’. If it’s a hash, then it’ll return the right sending options that belong to the delivery system. If ‘default_delivery_options’ is nil, it’ll return an empty hash. But if the value isn’t a hash, it’ll raise an error.

Returns:

  • (Hash)

    the data that was defined in ‘default_delivery_options’



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/mail_plugger/mail_helper.rb', line 88

def default_data
  options = option_value_from(@default_delivery_options)

  return {} if options.nil?

  unless options.is_a?(Hash)
    raise Error::WrongDefaultDeliveryOptions,
          '"default_delivery_options" does not a Hash'
  end

  options.transform_keys(&:to_sym)
end

#default_delivery_system_getStirng/NilClass

Tries to set up the ‘default_delivery_system’.

Returns:

  • (Stirng/NilClass)

    the name of a delivery system or nil



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mail_plugger/mail_helper.rb', line 104

def default_delivery_system_get
  case sending_method_get
  when :default_delivery_system
    @passed_default_delivery_system
  when :plugged_in_first
    extract_keys&.first
  when :random
    extract_keys&.sample
  when :round_robin
    @rotatable_delivery_systems&.next
  end
end

#delivery_dataHash

Collects data from the Mail::Message object.

Returns:

  • (Hash)

    the data that was defined in ‘delivery_options’



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/mail_plugger/mail_helper.rb', line 58

def delivery_data
  data = {}

  delivery_options.each do |option|
    option = option.to_sym unless option.is_a?(Symbol)

    data[option] =
      case option
      when :from, :to, :cc, :bcc, :subject
        @message.public_send(option)
      when :attachments
        extract_attachments
      when :body, :html_part, :text_part
        @message.public_send(option)&.decoded
      when :message_obj
        @message
      else
        message_field_value_from(@message[option])
      end
  end

  Mail::IndifferentHash.new(default_data.merge(data))
end

#delivery_optionsArray

Extract ‘delivery_options’. If it’s a hash, then it’ll return the right options that belong to the delivery system. If it’s not a hash, it’ll return the given value. But if the value isn’t an array, it’ll raise an error.

Returns:

  • (Array)

    the options it’ll collect from the Mail::Message object



123
124
125
126
127
128
129
130
131
132
# File 'lib/mail_plugger/mail_helper.rb', line 123

def delivery_options
  options = option_value_from(@delivery_options)

  unless options.is_a?(Array)
    raise Error::WrongDeliveryOptions,
          '"delivery_options" does not an Array'
  end

  options
end

#delivery_systemString

Extract ‘delivery_system’ from the Mail::Message object, or if it’s not defined, then use the default one. If it’s still nil and one of the ‘delivery_options’, ‘client’, and/or ‘delivery_settings’ is a hash and ‘delivery_settings’ doesn’t contain ‘delivery_system’, then raise an error.

Returns:

  • (String)

    with the name of the delivery system



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mail_plugger/mail_helper.rb', line 141

def delivery_system
  return @delivery_system unless @delivery_system.nil?

  @delivery_system =
    (@message && message_field_value_from(@message[:delivery_system])) ||
    @default_delivery_system

  delivery_system_value_check

  @delivery_system
end

#delivery_system_value_checkObject

Check that the given ‘delivery_options’, ‘client’, and ‘delivery_settings’ are hashes, and if one of those is, then check whether the ‘delivery_system’ is valid or not. If the given ‘delivery_system’ is nil or doesn’t match with extracted keys, then it will raise an error.



158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/mail_plugger/mail_helper.rb', line 158

def delivery_system_value_check
  return unless need_delivery_system?

  if @delivery_system.nil?
    raise Error::WrongDeliverySystem,
          '"delivery_system" was not defined as a Mail::Message parameter'
  end

  return if extract_keys&.include?(@delivery_system)

  raise Error::WrongDeliverySystem,
        "\"delivery_system\" '#{@delivery_system}' does not exist"
end

#exclude_delivey_settings_keys?Boolean

Check that ‘delivery_settings’ has the ‘delivery_system’ key or not. If ‘delivery_settings’ contains ‘DELIVERY_SETTINGS_KEYS’, then it returns false, else true.

Returns:

  • (Boolean)

    true/false



177
178
179
180
181
# File 'lib/mail_plugger/mail_helper.rb', line 177

def exclude_delivey_settings_keys?
  @delivery_settings.keys.none? do |key|
    DELIVERY_SETTINGS_KEYS.include?(key.to_sym)
  end
end

#extract_attachmentsArray

Extract attachments.

Returns:

  • (Array)

    with extracted attachment hashes



186
187
188
189
190
191
192
193
194
195
196
# File 'lib/mail_plugger/mail_helper.rb', line 186

def extract_attachments
  @message.attachments&.map do |attachment|
    hash = attachment.inline? ? { cid: attachment.cid } : {}

    hash.merge(
      filename: attachment.filename,
      type: attachment.mime_type,
      content: Base64.encode64(attachment.decoded)
    )
  end
end

#extract_keysArray/NilClass

Return the ‘delivery_systems’ array if it exists. If not, then extract keys from ‘delivery_options’, ‘client’, or ‘delivery_settings’, depending on which is a hash. If none of these are hashes, then returns nil.

Returns:

  • (Array/NilClass)

    with the keys or nil



203
204
205
# File 'lib/mail_plugger/mail_helper.rb', line 203

def extract_keys
  @delivery_systems || extract_keys_from_other_variables
end

#extract_keys_from_other_variablesArray/NilClass

Extract keys from ‘delivery_options’, ‘client’, or ‘delivery_settings’, depending on which is a hash. If none of these are hashes, then returns nil.

Returns:

  • (Array/NilClass)

    with the keys from one of the hash or nil



212
213
214
215
216
217
218
219
220
# File 'lib/mail_plugger/mail_helper.rb', line 212

def extract_keys_from_other_variables
  if @delivery_options.is_a?(Hash)
    @delivery_options
  elsif @client.is_a?(Hash)
    @client
  elsif @delivery_settings.is_a?(Hash) && exclude_delivey_settings_keys?
    @delivery_settings
  end&.keys
end

#gem_version_satisfied?(gem_name, version) ⇒ Boolean

Check the version of a gem.

Parameters:

  • gem_name (String)

    the name of the gem

  • version (String)

    the satisfied version of the gem

Returns:

  • (Boolean)

    true/false



29
30
31
32
33
34
# File 'lib/mail_plugger/mail_helper.rb', line 29

def gem_version_satisfied?(gem_name, version)
  requirement     = Gem::Requirement.new(version)
  current_version = Gem.loaded_specs[gem_name].version

  requirement.satisfied_by?(current_version)
end

#mail_field_valueString

How to extract the (unparsed) value of the mail message fields.

Returns:

  • (String)

    version-dependent method call



225
226
227
228
229
230
231
232
233
234
# File 'lib/mail_plugger/mail_helper.rb', line 225

def mail_field_value
  @mail_field_value ||=
    if gem_version_satisfied?('mail', '> 2.7.0')
      %w[unparsed_value]
    elsif gem_version_satisfied?('mail', '= 2.7.0')
      %w[instance_variable_get @unparsed_value]
    elsif gem_version_satisfied?('mail', '< 2.7.0')
      %w[instance_variable_get @value]
    end
end

#message_field_value_from(message_field) ⇒ String/Boolean/Hash

Extract the (unparsed) value of the mail message fields.

Parameters:

  • message_field (Mail::Field)

Returns:

  • (String/Boolean/Hash)

    with the field (unparsed) value



241
242
243
244
245
# File 'lib/mail_plugger/mail_helper.rb', line 241

def message_field_value_from(message_field)
  return if message_field.nil?

  message_field.public_send(*mail_field_value)
end

#need_delivery_system?Boolean

Check if either ‘delivery_options’ or ‘client’ is a hash, or ‘delivery_settings’ is a hash but does not contain ‘DELIVERY_SETTINGS_KEYS’ at the first level.

Returns:

  • (Boolean)

    true/false



252
253
254
255
256
# File 'lib/mail_plugger/mail_helper.rb', line 252

def need_delivery_system?
  @delivery_options.is_a?(Hash) ||
    @client.is_a?(Hash) ||
    (@delivery_settings.is_a?(Hash) && exclude_delivey_settings_keys?)
end

#option_value_from(option) ⇒ Hash/Array/Class

Extract the value from the given options.

Parameters:

  • option (Hash/Array/Class)

Returns:

  • (Hash/Array/Class)

    with the option value



263
264
265
266
267
268
269
# File 'lib/mail_plugger/mail_helper.rb', line 263

def option_value_from(option)
  if option.is_a?(Hash) && option[delivery_system]
    option[delivery_system]
  else
    option
  end
end

#send_via_smtp?Boolean

Check that the settings contain any SMTP-related settings.

Returns:

  • (Boolean)

    true/false



274
275
276
277
278
279
# File 'lib/mail_plugger/mail_helper.rb', line 274

def send_via_smtp?
  return true if settings[:smtp_settings].is_a?(Hash) &&
                 settings[:smtp_settings].any?

  false
end

#sending_method_getSymbol

Choose a ‘sending_method’ for the given conditions.

Returns:

  • (Symbol)

    the appropriate sending method



284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/mail_plugger/mail_helper.rb', line 284

def sending_method_get
  if @sending_method.nil? && !@passed_default_delivery_system.nil?
    :default_delivery_system
  elsif @sending_method.nil? ||
        !SENDING_METHODS.include?(@sending_method.to_sym) ||
        (@sending_method.to_sym == :default_delivery_system &&
          @passed_default_delivery_system.nil?)
    :plugged_in_first
  else
    @sending_method.to_sym
  end
end

#settingsHash

Extract ‘settings’. If ‘delivery_settings’ is a hash, then it’ll return the right settings that belong to the delivery system. If ‘delivery_settings’ is nil, it’ll return an empty hash. But if the value isn’t a hash, it’ll raise an error.

Returns:

  • (Hash)

    settings for Mail delivery_method and/or FakePlugger



303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/mail_plugger/mail_helper.rb', line 303

def settings
  return @settings unless @settings.nil?

  extracted_settings = option_value_from(@delivery_settings) || {}

  unless extracted_settings.is_a?(Hash)
    raise Error::WrongDeliverySettings,
          '"delivery_settings" does not a Hash'
  end

  @settings = extracted_settings.transform_keys(&:to_sym)
end