Module: T::Mailer::Helper

Instance Method Summary collapse

Instance Method Details

#check_api_defined(klass) ⇒ Object

Check gem is installed or not. If not it will raise error.

Parameters:

  • klass (String)

    the API’s class name



7
8
9
10
11
12
# File 'lib/t/mailer/helper.rb', line 7

def check_api_defined(klass)
  unless T::Mailer.const_defined?(klass)
    fail Error::DeliverySystemNotDefined,
      "Please install #{using_gem(klass)} gem."
  end
end

#check_settings(*required_values) ⇒ Object

Check API credentials were given. If one is missing or empty it will raise error.

Parameters:

  • required_values (List)

    a comma separated values/symbols



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/t/mailer/helper.rb', line 18

def check_settings(*required_values)
  has_all_settings =
    settings.values_at(*required_values).all? do |setting|
      setting && !setting.empty?
    end

  unless settings.is_a?(Hash) && has_all_settings
    fail Error::MissingCredentials,
      "Please provide all credential values. Required: #{required_values}"
  end
end

#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



36
37
38
39
40
41
# File 'lib/t/mailer/helper.rb', line 36

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

#field_valueString

How to gets the uparsed value of the mail message fields.

Returns:

  • (String)

    version dependent method call



46
47
48
49
50
51
52
53
54
# File 'lib/t/mailer/helper.rb', line 46

def 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

#get_value_from(message_field) ⇒ String/Hash

Gets uparsed value of the mail message fields.

Parameters:

  • message_field (Mail::Field)

Returns:

  • (String/Hash)

    with the field unparsed value



61
62
63
64
65
# File 'lib/t/mailer/helper.rb', line 61

def get_value_from(message_field)
  return if message_field.nil?

  message_field.public_send(*field_value)
end

#using_gem(klass) ⇒ String

Which gem using an API class.

Parameters:

  • klass (String)

    the class name

Returns:

  • (String)

    the gem name which should use



72
73
74
75
76
77
78
79
80
81
# File 'lib/t/mailer/helper.rb', line 72

def using_gem(klass)
  case klass
  when "Api::AwsSes"
    "aws-sdk-ses"
  when "Api::SparkPost::Transmissions"
    "simple_spark"
  else
    "unknown"
  end
end