Class: DMCourier::Services::Sparkpost

Inherits:
Object
  • Object
show all
Includes:
MessageHelper
Defined in:
lib/dm_courier/services/sparkpost.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MessageHelper

#attachments, #attachments?, #extract_params, #fallback_options, #from, #from_address, #from_email, #from_name, #html_part, #nil_true_false?, #return_string_value, #subject, #text_part

Constructor Details

#initialize(mail, options = {}) ⇒ Sparkpost

Returns a new instance of Sparkpost.



14
15
16
17
18
# File 'lib/dm_courier/services/sparkpost.rb', line 14

def initialize(mail, options = {})
  @mail = mail
  @api_key = options.fetch(:api_key)
  @options = options
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



12
13
14
# File 'lib/dm_courier/services/sparkpost.rb', line 12

def api_key
  @api_key
end

#mailObject (readonly)

Returns the value of attribute mail.



12
13
14
# File 'lib/dm_courier/services/sparkpost.rb', line 12

def mail
  @mail
end

#optionsObject (readonly)

Returns the value of attribute options.



12
13
14
# File 'lib/dm_courier/services/sparkpost.rb', line 12

def options
  @options
end

Instance Method Details

#deliver!Object



24
25
26
27
28
29
# File 'lib/dm_courier/services/sparkpost.rb', line 24

def deliver!
  sparkpost = ::SparkPost::Client.new(api_key)
  transmission = sparkpost.transmission

  transmission.send(:request, transmission.endpoint, api_key, sparkpost_message)
end

#nameObject



20
21
22
# File 'lib/dm_courier/services/sparkpost.rb', line 20

def name
  :sparkpost
end

#sparkpost_messageObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/dm_courier/services/sparkpost.rb', line 31

def sparkpost_message
  parameters = extract_params(nil_true_false: { inline_css: :inline_css,
                                                click_tracking: :track_clicks,
                                                open_tracking: :track_opens },
                              string: { return_path: :return_path_domain })

  message = { options: {} }
  message[:options][:inline_css] = parameters[:inline_css] unless parameters[:inline_css].nil?
  message[:options][:click_tracking] = parameters[:click_tracking] unless parameters[:click_tracking].nil?
  message[:options][:open_tracking] = parameters[:open_tracking] unless parameters[:open_tracking].nil?

  message[:return_path] = parameters[:return_path] unless parameters[:return_path].nil?

  from = { email: from_email }
  from[:name] = from_name if from_name

  message[:content] = {
    from: from,
    subject: subject
  }

  message[:content][:reply_to] = reply_to if reply_to
  message[:content][:html] = html_part if html_part
  message[:content][:text] = text_part if text_part

  message[:recipients] = recipients

  message[:content][:attachments] = regular_attachments if attachments?(inline: false)
  message[:content][:inline_images] = inline_attachments if attachments?(inline: true)
  message
end