Class: SmartlistSparkpost::Transmissions

Inherits:
Object
  • Object
show all
Defined in:
lib/smartlist_sparkpost/transmissions.rb

Class Method Summary collapse

Class Method Details

.deliver(to, from_email, from_name, reply_to, subject, body, date = nil) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/smartlist_sparkpost/transmissions.rb', line 4

def self.deliver(to, from_email, from_name, reply_to, subject, body, date = nil)
  data = SmartlistSparkpost::Transmissions.make_body(to, from_email, from_name, reply_to, subject, body, date)

  url = "https://api.sparkpost.com/api/#{SmartlistSparkpost.configuration.version}/transmissions?num_rcpt_errors=3"
  headers = {
      'Authorization' => SmartlistSparkpost.configuration.api_key,
      'Content-Type' => 'application/json'
  }
  response = HTTParty.post(url, {body: data.to_json, headers: headers})

  if response.code == 200
    JSON.parse(response.body)
  else
    SmartlistSparkpost::Transmissions.handle_error(response)
  end
end

.handle_error(response) ⇒ Object



21
22
23
24
25
26
# File 'lib/smartlist_sparkpost/transmissions.rb', line 21

def self.handle_error(response)
  if response['errors']
    @response = response['errors']
    raise SmartlistSparkpost::DeliveryException, @response
  end
end

.make_body(to, from_email, from_name, reply_to, subject, body, date = nil) ⇒ Object



28
29
30
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
62
# File 'lib/smartlist_sparkpost/transmissions.rb', line 28

def self.make_body(to, from_email, from_name, reply_to, subject, body, date=nil)
  data = {
      recipients: [
          {
              address: to
          }
      ],
      options: {
          start_time: 'now',
          open_tracking: true,
          click_tracking: true
      },
      content: {
          from: {
              email: from_email,
              name: from_name
          },
          reply_to: reply_to,
          subject: subject,
          html: body
      }
  }
  if date.nil?
    data[:options][:start_time] = 'now'
  else
    data[:options][:start_time] = date.strftime("%Y-%m-%dT%H:%M:%S%:z")
  end

  data[:options][:open_tracking] = SmartlistSparkpost.configuration.track_opens
  data[:options][:click_tracking] = SmartlistSparkpost.configuration.track_clicks
  unless SmartlistSparkpost.configuration.ip_pool.nil?
    data[:options][:ip_pool] = SmartlistSparkpost.configuration.ip_pool
  end
  data
end