Class: Quolle::Resources::Emails

Inherits:
Object
  • Object
show all
Defined in:
lib/quolle/resources/emails.rb

Overview

Send and manage emails.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Emails

Returns a new instance of Emails.



7
8
9
# File 'lib/quolle/resources/emails.rb', line 7

def initialize(client)
  @client = client
end

Instance Method Details

#cancel(id) ⇒ Hash

Cancel a scheduled email that has not yet been sent.

Returns:

  • (Hash)


43
44
45
# File 'lib/quolle/resources/emails.rb', line 43

def cancel(id)
  @client.request("DELETE", "/v1/emails/#{escape(id)}/cancel")
end

#get(id) ⇒ Hash

Retrieve a single email by ID.

Returns:

  • (Hash)


36
37
38
39
# File 'lib/quolle/resources/emails.rb', line 36

def get(id)
  resp = @client.request("GET", "/v1/emails/#{escape(id)}")
  resp["email"] || resp
end

#send(idempotency_key: nil, **params) ⇒ Hash

Queue an email for immediate or scheduled delivery.

Provide either :html (with :subject) or a :template slug (with optional :variables). Pass idempotency_key: to make retries safe.

Parameters:

  • params (Hash)

    from:, to:, subject:, html:, text:, template:, variables:, reply_to:/replyTo:, scheduled_at:/scheduledAt:, metadata:

Returns:

  • (Hash)

    at least "id" and "message"



19
20
21
22
# File 'lib/quolle/resources/emails.rb', line 19

def send(idempotency_key: nil, **params)
  headers = idempotency_key ? { "Idempotency-Key" => idempotency_key } : {}
  @client.request("POST", "/v1/emails/send", body: normalize(params), headers: headers)
end

#send_batch(emails, idempotency_key: nil) ⇒ Hash

Send up to 100 emails in one all-or-nothing request.

Parameters:

  • emails (Array<Hash>)

Returns:

  • (Hash)

    { "queued" => Integer, "ids" => Array }



28
29
30
31
32
# File 'lib/quolle/resources/emails.rb', line 28

def send_batch(emails, idempotency_key: nil)
  headers = idempotency_key ? { "Idempotency-Key" => idempotency_key } : {}
  body = { emails: emails.map { |e| normalize(e) } }
  @client.request("POST", "/v1/emails/batch", body: body, headers: headers)
end