Class: Sendara::Resources::Emails

Inherits:
Sendara::Resource show all
Defined in:
lib/sendara/resources/emails.rb

Instance Method Summary collapse

Methods inherited from Sendara::Resource

#initialize

Constructor Details

This class inherits a constructor from Sendara::Resource

Instance Method Details

#send(to:, from: nil, subject: nil, html: nil, text: nil, message_type: nil, template_id: nil, template_vars: nil, metadata: nil, store_payload: nil, test_send: nil, idempotency_key: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sendara/resources/emails.rb', line 6

def send(to:, from: nil, subject: nil, html: nil, text: nil, message_type: nil,
         template_id: nil, template_vars: nil, metadata: nil, store_payload: nil,
         test_send: nil, idempotency_key: nil)
  meta = .nil? ? {} : .dup
  meta["from_email"] = from unless from.nil?

  body = {
    "channel" => "email",
    "idempotency_key" => idempotency_key || Client.generate_idempotency_key,
    "destination" => { "email" => to },
    "payload" => {
      "subject" => subject,
      "body_html" => html,
      "body_text" => text
    },
    "metadata" => meta
  }

  body["message_type"] = message_type unless message_type.nil?
  body["template_id"] = template_id unless template_id.nil?
  body["template_vars"] = template_vars unless template_vars.nil?
  body["store_payload"] = store_payload unless store_payload.nil?
  body["test_send"] = test_send unless test_send.nil?

  send_raw(body)
end

#send_batch(requests) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/sendara/resources/emails.rb', line 39

def send_batch(requests)
  body = requests.map do |req|
    req = req.dup
    req["idempotency_key"] ||= Client.generate_idempotency_key
    req
  end
  request(:post, "/v1/send/batch", body: body) || []
end

#send_raw(request) ⇒ Object



33
34
35
36
37
# File 'lib/sendara/resources/emails.rb', line 33

def send_raw(request)
  request = request.dup
  request["idempotency_key"] ||= Client.generate_idempotency_key
  request(:post, "/v1/send", body: request) || {}
end