class T::Mailer::DeliverySystem::SparkPost

Attributes

settings[R]

Public Class Methods

new(options = {}) click to toggle source

Set settings with the required credentials for the API, but allow to call this delivery system without it.

@param [Hash] options with the credentials

# File lib/t/mailer/delivery_system/spark_post.rb, line 13
def initialize(options = {})
  @settings = options
end

Public Instance Methods

deliver(message) click to toggle source

Check that the API is loaded. If API is missing it will raise error. If API exists then it will call the API with the generated options from the given mail message.

@param [Mail::Message] message what we would like to send

@return [Mail::Message] message with the changed message_id

# File lib/t/mailer/delivery_system/spark_post.rb, line 24
def deliver(message)
  check_api_defined("Api::SparkPost::Transmissions")

  options = generate_options(message)

  response = Api::SparkPost::Transmissions.new(settings).create(options)
  message.message_id = response && response.dig("id")

  message
end
generate_options(message) click to toggle source

Generate the required hash what it will send via API.

@param [Mail::Message] message what we would like to send

@return [Hash] options for the API

# File lib/t/mailer/delivery_system/spark_post.rb, line 40
def generate_options(message)
  {
    options:     get_value_from(message["options"]),
    campaign_id: get_value_from(message["tag"]),
    content:     {
      email_rfc822: message.to_s,
    },
    metadata:    get_value_from(message["metadata"]),
    recipients:  generate_recipients(message),
  }
end
generate_recipients(message) click to toggle source

Generate recipients.

@param [Mail::Message] message what we would like to send

@return [Array] with the recipients and tags

# File lib/t/mailer/delivery_system/spark_post.rb, line 57
def generate_recipients(message)
  message.to.map do |to|
    {
      address: {
        email: to,
      },
      tags:    [
        get_value_from(message["tag"]),
      ],
    }
  end
end