Class: SparkPost::Transmission

Inherits:
Object
  • Object
show all
Includes:
Request
Defined in:
lib/sparkpost/transmission.rb

Instance Method Summary collapse

Methods included from Request

configure_http, configure_request, #endpoint, process_response, request

Constructor Details

#initialize(api_key, api_host) ⇒ Transmission

Returns a new instance of Transmission.



11
12
13
14
15
# 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

Instance Method Details

#prepare_recipients(recipients) ⇒ Object



49
50
51
52
# 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) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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 = {}) ⇒ Object



17
18
19
20
# 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