Class: T::Mailer::DeliverySystem::SparkPost

Inherits:
Object
  • Object
show all
Includes:
Helper
Defined in:
lib/t/mailer/delivery_system/spark_post.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#check_api_defined, #check_settings, #check_version_of, #field_value, #get_value_from, #using_gem

Constructor Details

#initialize(options = {}) ⇒ SparkPost

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



13
14
15
# File 'lib/t/mailer/delivery_system/spark_post.rb', line 13

def initialize(options = {})
  @settings = options
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



7
8
9
# File 'lib/t/mailer/delivery_system/spark_post.rb', line 7

def settings
  @settings
end

Instance Method Details

#deliver(message) ⇒ Mail::Message

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.



24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Hash

Generate the required hash what it will send via API.



40
41
42
43
44
45
46
47
48
49
50
# 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) ⇒ Array

Generate recipients.



57
58
59
60
61
62
63
64
65
66
67
68
# 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