Class: FakePlugger::DeliveryMethod

Inherits:
MailPlugger::DeliveryMethod show all
Defined in:
lib/fake_plugger/delivery_method.rb

Constant Summary

Constants included from MailPlugger::MailHelper

MailPlugger::MailHelper::DELIVERY_SETTINGS_KEYS, MailPlugger::MailHelper::SENDING_METHODS

Instance Method Summary collapse

Methods included from MailPlugger::MailHelper

#check_version_of, #client, #default_data, #default_delivery_system_get, #delivery_data, #delivery_options, #delivery_system, #delivery_system_value_check, #exclude_delivey_settings_keys?, #extract_attachments, #extract_keys, #extract_keys_from_other_variables, #mail_field_value, #message_field_value_from, #need_delivery_system?, #option_value_from, #send_via_smtp?, #sending_method_get, #settings

Constructor Details

#initialize(options = {}) ⇒ DeliveryMethod

Initialize FakePlugger delivery method attributes. If we are using MailPlugger.plug_in method, then these attributes can be nil, if not then we should set these attributes.

Options Hash (options):

  • client (Class/Hash)

    e.g. DefinedApiClientClass or { ‘key’ => DefinedApiClientClass }

  • delivery_options (Array/Hash)

    e.g. [:to, :from, :subject, :body] or { ‘key’ => [:to, :from, :subject, :body] }

  • delivery_settings (Hash)

    e.g. { return_response: true }

  • default_delivery_system (String/Symbol)

    e.g. ‘api_client’

  • debug (Boolean)

    if true, it will show debug information

  • raw_message (Boolean)

    if true, it will show raw message

  • response (String/Symbol/Array/Hash)

    the deliver! method will return with this value or if this value is nil then it will return with the client object

  • use_mail_grabber (Boolean)

    if true it will store the message in a database which MailGrabber can read



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fake_plugger/delivery_method.rb', line 38

def initialize(options = {})
  super

  @debug            = options[:debug]

  @raw_message      = options[:raw_message]

  @response         = options[:response]

  @use_mail_grabber = options[:use_mail_grabber]
end

Instance Method Details

#deliver!(message) ⇒ Mail::Message/Hash

Using SMTP: Mock send message via SMTP protocol if the ‘delivery_settings’ contains a ‘smtp_settings’ key and the value is a hash with the settings.

Using API: Mock send message with the given client if the message parameter is a Mail::Message object. If ‘response’ parameter is nil, then it will extract this information from the Mail::Message object which was provided in the ‘delivery_options’. After that it generates a hash with these data and returns with the provided client class which has a ‘deliver’ method, but it won’t call the ‘deliver’ method. If the ‘response’ parameter is a hash with ‘return_delivery_data: true’ then it will return with the extracted delivery data.

If the ‘response’ parameter is not nil, then returns with that given data without call any other methods. Except if ‘debug’ is true. In this case, it will call those methods which are calling in normal operation as well. If ‘debug’ is true, then it prints out some debug information. If ‘raw_message’ is true, then it prints out raw message. If ‘use_mail_grabber’ is true, then it stores the message in a database.

Examples:


# Using SMTP:

MailPlugger.plug_in('test_smtp_client') do |smtp|
  smtp.delivery_settings = {
    smtp_settings: {
      address: 'smtp.server.com',
      port: 587,
      domain: 'test.domain.com',
      enable_starttls_auto: true,
      user_name: 'test_user',
      password: '1234',
      authentication: :plain
    }
  }
end

message = Mail.new(from: '[email protected]', to: '[email protected]',
                   subject: 'Test email', body: 'Test email body')

FakePlugger::DeliveryMethod.new.deliver!(message)

# or

message = Mail.new(from: '[email protected]', to: '[email protected]',
                   subject: 'Test email', body: 'Test email body')

FakePlugger::DeliveryMethod.new(
  delivery_settings: {
    smtp_settings: {
      address: 'smtp.server.com',
      port: 587,
      domain: 'test.domain.com',
      enable_starttls_auto: true,
      user_name: 'test_user',
      password: '1234',
      authentication: :plain
    }
  }
).deliver!(message)

# Using API:

MailPlugger.plug_in('test_api_client') do |api|
  api.client = DefinedApiClientClass
  api.delivery_options = i[from to subject body]
  api.delivery_settings = {
    fake_plugger_debug: true,
    fake_plugger_raw_message: true,
    fake_plugger_use_mail_grabber: true,
    fake_plugger_response: { response: 'OK' }
  }
end

message = Mail.new(from: '[email protected]', to: '[email protected]',
                   subject: 'Test email', body: 'Test email body')

FakePlugger::DeliveryMethod.new.deliver!(message)

# or

message = Mail.new(from: '[email protected]', to: '[email protected]',
                   subject: 'Test email', body: 'Test email body')

FakePlugger::DeliveryMethod.new(
  client: DefinedApiClientClass,
  delivery_options: i[from to subject body],
  debug: true,
  raw_message: true,
  use_mail_grabber: true,
  response: { response: 'OK' }
).deliver!(message)


151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/fake_plugger/delivery_method.rb', line 151

def deliver!(message)
  unless message.is_a?(Mail::Message)
    raise MailPlugger::Error::WrongParameter,
          'The given parameter is not a Mail::Message'
  end

  @message = message

  update_settings

  call_extra_options

  return_with_response
end