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, scheduled_at: nil, validate_recipient: nil, idempotency_key: nil) ⇒ Object

Raises:

  • (ArgumentError)


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
32
33
34
35
36
37
38
39
40
41
# 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, scheduled_at: nil, validate_recipient: nil, idempotency_key: nil)
  raise ArgumentError, "to is required" if to.nil? || to.to_s.empty?

  meta = .nil? ? {} : .dup
  meta["from_email"] = from unless from.nil?

  body = {
    "channel" => "email",
    "idempotency_key" => idempotency_key || Client.generate_idempotency_key,
    "destination" => { "email" => to }
  }
  body["metadata"] = meta unless meta.empty?

  # Only the fields the caller supplied go on the wire, and "payload" is
  # dropped outright when none were: a template-only send would otherwise
  # ship nulls the server discards when it renders the template over them.
  # Shared by all five SDKs.
  payload = compact_params(
    "subject" => subject,
    "body_html" => html,
    "body_text" => text
  ).reject { |_key, value| value.to_s.empty? }
  body["payload"] = payload unless payload.empty?

  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?
  body["scheduled_at"] = scheduled_at unless scheduled_at.nil?
  body["validate_recipient"] = validate_recipient unless validate_recipient.nil?

  send_raw(body)
end

#send_batch(requests) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/sendara/resources/emails.rb', line 49

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



43
44
45
46
47
# File 'lib/sendara/resources/emails.rb', line 43

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