class SparkPost::Transmission

Public Class Methods

new(api_key, api_host) click to toggle source
# File lib/sparkpost/transmission.rb, line 11
def initialize(api_key, api_host)
  @api_key = api_key
  @api_host = api_host
  @base_endpoint = "#{@api_host}/api/v1/transmissions"
end

Public Instance Methods

prepare_recipients(recipients) click to toggle source
# File lib/sparkpost/transmission.rb, line 49
def prepare_recipients(recipients)
  recipients = [recipients] unless recipients.is_a?(Array)
  recipients.map { |recipient| prepare_recipient(recipient) }
end
send_message(to, from, subject, html_message = nil, **options) click to toggle source
# File lib/sparkpost/transmission.rb, line 22
def send_message(to, from, subject, html_message = nil, **options)
  # TODO: add validations for to, from
  html_message = content_from(options, :html) || html_message
  text_message = content_from(options, :text) || options[:text_message]

  if html_message.blank? && text_message.blank?
    raise ArgumentError, 'Content missing. Either provide html_message or
     text_message in options parameter'
  end

  options_from_args = {
    recipients: prepare_recipients(to),
    content: {
      from: from,
      subject: subject,
      text: options.delete(:text_message),
      html: html_message
    },
    options: {}
  }

  options.merge!(options_from_args) { |_k, opts, _args| opts }
  add_attachments(options)

  send_payload(options)
end
send_payload(data = {}) click to toggle source
# File lib/sparkpost/transmission.rb, line 17
def send_payload(data = {})
  # TODO: consider refactoring this into send_message in v2
  request(endpoint, @api_key, data)
end

Private Instance Methods

add_attachments(options) click to toggle source
# File lib/sparkpost/transmission.rb, line 56
def add_attachments(options)
  if options[:attachments].present?
    options[:content][:attachments] = options.delete(:attachments)
  end
end
content_from(options, key) click to toggle source
# File lib/sparkpost/transmission.rb, line 72
def content_from(options, key)
  (options || {}).fetch(:content, {}).fetch(key, nil)
end
prepare_recipient(recipient) click to toggle source
# File lib/sparkpost/transmission.rb, line 62
def prepare_recipient(recipient)
  if recipient.is_a?(Hash)
    raise ArgumentError,
          "email missing - '#{recipient.inspect}'" unless recipient[:email]
    { address: recipient }
  else
    { address: { email: recipient } }
  end
end