Class: Broadcast::Resources::Transactionals

Inherits:
Base
  • Object
show all
Defined in:
lib/broadcast/resources/transactionals.rb

Constant Summary collapse

MAX_IDEMPOTENCY_KEY_LENGTH =
255

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Broadcast::Resources::Base

Instance Method Details

#create(to:, subject: nil, body: nil, reply_to: nil, preheader: nil, template_id: nil, include_unsubscribe_link: nil, double_opt_in: nil, confirmation_template_id: nil, subscriber: nil, idempotency_key: nil, **extra) ⇒ Object

Send a transactional email.

Required:

to:      recipient email address

One of subject/body or template_id is required (template_id resolves subject and body server-side; subject/body override the template).

Optional:

subject:, body:, preheader:
reply_to:
template_id:                 resolve subject/body/preheader from a Template
include_unsubscribe_link:    boolean
double_opt_in:               true | { reply_to:, confirmation_template_id:, include_unsubscribe_link: }
                           Holds the email until the recipient confirms.
confirmation_template_id:    custom confirmation template (used with double_opt_in: true)
subscriber:                  { first_name:, last_name: } — populates Subscriber on first send
idempotency_key:             see below

Idempotency

Pass idempotency_key: to make a retry safe. The server stores the response for 24 hours keyed on (token, key) and replays it rather than sending a second email. Check result.idempotent_replay? to tell a replay from a fresh send.

client.transactionals.create(to: 'a@b.com', subject: 'Receipt',
                           body: '<p>Thanks</p>',
                           idempotency_key: "receipt-#{order.id}")

The key is part of a fingerprint over method + full path + body:

- same key, same payload, still running   -> Broadcast::ConflictError (409)
- same key, *different* payload           -> Broadcast::ValidationError (422)

That 422 means "this key was already used for something else", not that the email was invalid — don't retry it with the same key. rubocop:disable Metrics/ParameterLists -- mirrors the API's flat param surface



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/broadcast/resources/transactionals.rb', line 44

def create(to:, subject: nil, body: nil, reply_to: nil, preheader: nil,
           template_id: nil, include_unsubscribe_link: nil,
           double_opt_in: nil, confirmation_template_id: nil,
           subscriber: nil, idempotency_key: nil, **extra)
  # rubocop:enable Metrics/ParameterLists
  payload = { to: to }
  payload[:subject] = subject unless subject.nil?
  payload[:body] = body unless body.nil?
  payload[:preheader] = preheader unless preheader.nil?
  payload[:reply_to] = reply_to unless reply_to.nil?
  payload[:template_id] = template_id unless template_id.nil?
  payload[:include_unsubscribe_link] = include_unsubscribe_link unless include_unsubscribe_link.nil?
  payload[:double_opt_in] = double_opt_in unless double_opt_in.nil?
  payload[:confirmation_template_id] = confirmation_template_id unless confirmation_template_id.nil?
  payload[:subscriber] = subscriber unless subscriber.nil?
  payload.merge!(extra) unless extra.empty?

  @client.request(:post, '/api/v1/transactionals.json', payload,
                  headers: idempotency_headers(idempotency_key))
end

#get_transactional(id) ⇒ Object



65
66
67
# File 'lib/broadcast/resources/transactionals.rb', line 65

def get_transactional(id)
  get("/api/v1/transactionals/#{id}.json")
end