Sha256: 580d38a2fb43f389d48ca5f006844dc07ccd6ee27c6ff7198ffc07e6056740f9

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

require 'net/http'
require 'uri'
require 'http'
require_relative '../core_extensions/object'
require_relative 'request'
require_relative 'exceptions'

module SparkPost
  class Transmission
    include Request

    def initialize(api_key, api_host)
      @api_key = api_key
      @api_host = api_host
    end

    def endpoint
      @api_host.concat('/api/v1/transmissions')
    end

    def send_message(to, from, subject, html_message = nil, **options)
      #todo add validations for to, from
      unless to.is_a?(Array)
        to = [to]
      end

      if html_message.blank? && options[:text_message].blank?
        fail ArgumentError, 'Content missing. Either provide html_message or text_message in options parameter'
      end

      options.merge!(
          {
              recipients: prepare_recipients(to),
              content: {
                  from: from,
                  subject: subject,
                  text: options['text_message'],
                  html: html_message
              },
              options: {}
          }
      )

      request(endpoint, @api_key, options)
    end

    private
    def prepare_recipients(recipients)
      recipients.map do |recipient|
        {
            address: {
                email: recipient
            }
        }
      end
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
sparkpost-0.1.2 lib/sparkpost/transmission.rb
sparkpost-0.1.1 lib/sparkpost/transmission.rb
sparkpost-0.1.0 lib/sparkpost/transmission.rb