Class: Mailkube::Resources::Emails
- Inherits:
-
Object
- Object
- Mailkube::Resources::Emails
- Defined in:
- lib/mailkube/resources/emails.rb,
sig/mailkube/resources/emails.rbs
Overview
The emails namespace, reached as client.emails.
This is the worked example every new resource copies. Note what it does not do: it holds
no configuration, performs no I/O, and never requires net/http. It depends only on an
object responding to the one verb it calls.
Instance Method Summary collapse
-
#idempotency(key) ⇒ Hash{String => String}
Build the body as one hash literal and drop the nils in a single pass, rather than a chain of
body["x"] = x if x. -
#initialize(transport) ⇒ Emails
constructor
A new instance of Emails.
-
#send(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil, headers: nil, attachments: nil, tags: nil, template_id: nil, template_version: nil, variables: nil, topic: nil, idempotency_key: nil, scheduled_at: nil, batch_id: nil) ⇒ Email
Send an email.
Constructor Details
#initialize(transport) ⇒ Emails
Returns a new instance of Emails.
12 13 14 15 |
# File 'lib/mailkube/resources/emails.rb', line 12 def initialize(transport) @transport = transport freeze end |
Instance Method Details
#idempotency(key) ⇒ Hash{String => String}
Build the body as one hash literal and drop the nils in a single pass, rather than a
chain of body["x"] = x if x. That keeps this method's cyclomatic complexity at 1 no
matter how many optional fields the API grows, and is why an unset field is absent from
the wire rather than sent as null.
78 |
# File 'lib/mailkube/resources/emails.rb', line 78 def idempotency(key) = key.nil? ? {} : { "Idempotency-Key" => key } |
#send(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil, headers: nil, attachments: nil, tags: nil, template_id: nil, template_version: nil, variables: nil, topic: nil, idempotency_key: nil, scheduled_at: nil, batch_id: nil) ⇒ Email
Send an email.
Supply html and/or text for a raw send, or template_id for a saved template.
idempotency_key travels as the Idempotency-Key header rather than in the body.
Passing scheduled_at schedules the send instead of delivering it now; the result then
reports Email#scheduled?.
This method shadows Object#send on this object only, which is deliberate: every
mailkube SDK spells the verb emails.send, and a Ruby-only name would break that. Use
__send__ if you need reflective dispatch on a resource.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/mailkube/resources/emails.rb', line 49 def send(from:, to:, subject:, html: nil, text: nil, cc: nil, bcc: nil, reply_to: nil, headers: nil, attachments: nil, tags: nil, template_id: nil, template_version: nil, variables: nil, topic: nil, idempotency_key: nil, scheduled_at: nil, batch_id: nil) # One hash literal, then a single `compact`, rather than a chain of `body["x"] = x if x`. # That keeps this method's cyclomatic complexity at 1 no matter how many optional fields # the API grows, and is why an unset field is absent from the wire rather than null. body = { "from" => from, "to" => to, "subject" => subject, "html" => html, "text" => text, "cc" => cc, "bcc" => bcc, "reply_to" => reply_to, "headers" => headers, "attachments" => Serialization.(), "tags" => Serialization.(), "template_id" => template_id, "template_version" => template_version, "variables" => variables, "topic" => topic, "scheduled_at" => Serialization.to_iso(scheduled_at), "batch_id" => batch_id }.compact @transport.send_email(RequestSpec.new(path: "emails", body: body, headers: idempotency(idempotency_key))) end |