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

#check_version_of(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 check_version_of(gem_name, version)
  requirement     = Gem::Requirement.new(version)
  current_version = Gem.loaded_specs[gem_name].version

  requirement.satisfied_by?(current_version)
end

#clientClass

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

Returns:

  • (Class)

    the defined API class



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

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 belongs to the delivery system. If ‘default_delivery_options’ is nil, it’ll return an empty hash. But if the value doesn’t a hash, it’ll raise an error.

Returns:

  • (Hash)

    the data which was defined in ‘default_delivery_options’



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

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



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

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 Mail::Message object.

Returns:

  • (Hash)

    the data which was defined in ‘delivery_options’



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

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, belongs to the delivery system. If it’s not a hash, it’ll return the given value. But if the value doesn’t an array, it’ll raise an error.

Returns:

  • (Array)

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



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

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 error.

Returns:

  • (String)

    with the name of the delivery system



138
139
140
141
142
143
144
145
146
147
148
# File 'lib/mail_plugger/mail_helper.rb', line 138

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 the given ‘delivery_options’, ‘client’ and ‘delivery_settings’ are hashes and if one of that does, then check 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 error.



155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/mail_plugger/mail_helper.rb', line 155

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 ‘delivery_system’ key or not. If ‘delivery_settings’ contains ‘DELIVERY_SETTINGS_KEYS’ then it returns false, else true.

Returns:

  • (Boolean)

    true/false



174
175
176
177
178
# File 'lib/mail_plugger/mail_helper.rb', line 174

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



183
184
185
186
187
188
189
190
191
192
193
# File 'lib/mail_plugger/mail_helper.rb', line 183

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 ‘delivery_systems’ array if it’s exist. If not, then extract keys from ‘delivery_options’, ‘client’ or ‘delivery_settings’, depends on which is a hash. If none of these are hashes, then returns nil.

Returns:

  • (Array/NilClass)

    with the keys or nil



200
201
202
# File 'lib/mail_plugger/mail_helper.rb', line 200

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’, depends 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



208
209
210
211
212
213
214
215
216
# File 'lib/mail_plugger/mail_helper.rb', line 208

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

#mail_field_valueString

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

Returns:

  • (String)

    version dependent method call



221
222
223
224
225
226
227
228
229
230
# File 'lib/mail_plugger/mail_helper.rb', line 221

def mail_field_value
  @mail_field_value ||=
    if check_version_of('mail', '> 2.7.0')
      %w[unparsed_value]
    elsif check_version_of('mail', '= 2.7.0')
      %w[instance_variable_get @unparsed_value]
    elsif check_version_of('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



237
238
239
240
241
# File 'lib/mail_plugger/mail_helper.rb', line 237

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 ‘deliviery_options’ or ‘client’ is a hash, or ‘delivery_settings’ is a hash but not contains ‘DELIVERY_SETTINGS_KEYS’ in first level.

Returns:

  • (Boolean)

    true/false



248
249
250
251
252
# File 'lib/mail_plugger/mail_helper.rb', line 248

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



259
260
261
262
263
264
265
# File 'lib/mail_plugger/mail_helper.rb', line 259

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 settings contains any SMTP related settings.

Returns:

  • (Boolean)

    true/false



270
271
272
273
274
275
# File 'lib/mail_plugger/mail_helper.rb', line 270

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



280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/mail_plugger/mail_helper.rb', line 280

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, belongs to the delivery system. If ‘delivery_settings’ is nil, it’ll return an empty hash. But if the value doesn’t a hash, it’ll raise an error.

Returns:

  • (Hash)

    settings for Mail delivery_method and/or FakePlugger



299
300
301
302
303
304
305
306
307
308
309
310
# File 'lib/mail_plugger/mail_helper.rb', line 299

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