module MailPlugger::MailHelper

Constants

DELIVERY_SETTINGS_KEYS

Public Instance Methods

check_version_of(gem_name, version) click to toggle source

Check the version of a gem.

@param [String] gem_name the name of the gem @param [String] version the satisfied version of the gem

@return [Boolean] true/false

# File lib/mail_plugger/mail_helper.rb, line 23
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
client() click to toggle source

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.

@return [Class] the defined API class

# File lib/mail_plugger/mail_helper.rb, line 35
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_delivery_system_get() click to toggle source

Tries to set up a default delivery system, if the 'delivery_system' wasn't defined in the Mail::Message object and we're using MailPlugger.plug_in then it returns with first key of the stored 'delivery_systems'. When we are't using MailPlugger.plug_in method and 'delivery_options', 'client' and/or 'delivery_settings' is a hash, then it tries to get the 'delivery_system' from the hashes. Otherwise it returns with nil.

@return [Stirng/NilClass] the first key from the extracted keys or nil

# File lib/mail_plugger/mail_helper.rb, line 85
def default_delivery_system_get
  extract_keys&.first
end
delivery_data() click to toggle source

Collects data from Mail::Message object.

@return [Hash] the data which was defined in 'delivery_options'

# File lib/mail_plugger/mail_helper.rb, line 51
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(data)
end
delivery_options() click to toggle source

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.

@return [Array] the options it'll collect from the Mail::Message object

# File lib/mail_plugger/mail_helper.rb, line 94
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_system() click to toggle source

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.

@return [String] with the name of the delivery system

# File lib/mail_plugger/mail_helper.rb, line 111
def delivery_system
  @delivery_system ||=
    (@message && message_field_value_from(@message[:delivery_system])) ||
    @default_delivery_system

  delivery_system_value_check

  @delivery_system
end
delivery_system_value_check() click to toggle source

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.

# File lib/mail_plugger/mail_helper.rb, line 126
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?() click to toggle source

Check that 'delivery_settings' has 'delivery_system' key or not. If 'delivery_settings' contains 'DELIVERY_SETTINGS_KEYS' then it retruns false, else true.

@return [Boolean] true/false

# File lib/mail_plugger/mail_helper.rb, line 145
def exclude_delivey_settings_keys?
  @delivery_settings.keys.none? do |key|
    DELIVERY_SETTINGS_KEYS.include?(key.to_sym)
  end
end
extract_attachments() click to toggle source

Extract attachments.

@return [Array] with extracted attachment hashes

# File lib/mail_plugger/mail_helper.rb, line 154
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_keys() click to toggle source

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.

@return [Array/NilClass] with the keys or nil

# File lib/mail_plugger/mail_helper.rb, line 171
def extract_keys
  return @delivery_systems unless @delivery_systems.nil?

  extract_keys_from_other_variables
end
extract_keys_from_other_variables() click to toggle source

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

@return [Array/NilClass] with the keys from one of the hash or nil

# File lib/mail_plugger/mail_helper.rb, line 181
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_value() click to toggle source

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

@return [String] version dependent method call

# File lib/mail_plugger/mail_helper.rb, line 194
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) click to toggle source

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

@param [Mail::Field] message_field

@return [String/Boolean/Hash] with the field (unparsed) value

# File lib/mail_plugger/mail_helper.rb, line 210
def message_field_value_from(message_field)
  return if message_field.nil?

  message_field.public_send(*mail_field_value)
end
need_delivery_system?() click to toggle source

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.

@return [Boolean] true/false

# File lib/mail_plugger/mail_helper.rb, line 221
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) click to toggle source

Extract the value from the given options.

@param [Hash/Array/Class] option

@return [Hash/Array/Class] with the option value

# File lib/mail_plugger/mail_helper.rb, line 232
def option_value_from(option)
  if option.is_a?(Hash) && option[delivery_system]
    option[delivery_system]
  else
    option
  end
end
send_via_smtp?() click to toggle source

Check that settings contains any SMTP related settings.

@return [Boolean] true/false

# File lib/mail_plugger/mail_helper.rb, line 243
def send_via_smtp?
  return true if settings[:smtp_settings].is_a?(Hash) &&
                 settings[:smtp_settings].any?

  false
end
settings() click to toggle source

Extract 'settings'. If it's 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.

@return [Hash] settings for Mail delivery_method and/or FakePlugger

# File lib/mail_plugger/mail_helper.rb, line 256
def settings
  @settings ||= option_value_from(@delivery_settings)

  return {} if @settings.nil?

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

  @settings
end