Class: ScalewayTem::ActionMailer::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/scaleway_tem/action_mailer/client.rb

Overview

The only part of this gem that talks to the network.

Keeping the endpoint in exactly one place matters more than usual here: Scaleway's Transactional Email API is still published as v1alpha1, so the path is the thing most likely to change out from under us.

Constant Summary collapse

API_PATH =
"/transactional-email/v1alpha1/regions/%<region>s"

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Client

Returns a new instance of Client.



16
17
18
# File 'lib/scaleway_tem/action_mailer/client.rb', line 16

def initialize(settings = {})
  @settings = ScalewayTem::ActionMailer.config.to_h.merge(compact(settings))
end

Instance Method Details

#list_domainsHash

Cheap credentials check: list the sending domains of the configured project. Useful from a console or a health check to prove the secret key and project id authenticate, without sending anyone an email.

Returns:

  • (Hash)

    { success:, error:, domains: }



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/scaleway_tem/action_mailer/client.rb', line 34

def list_domains
  response = connection.get("#{region_path}/domains") do |req|
    req.params["project_id"] = project_id
  end

  if response.success?
    body = safe_parse(response.body)
    { success: true, error: nil, domains: body["domains"] || [] }
  else
    { success: false, error: "HTTP #{response.status}: #{api_message(response.body)}", domains: [] }
  end
rescue Faraday::Error => e
  { success: false, error: "#{e.class}: #{e.message}", domains: [] }
end

#send_email(payload) ⇒ Hash

Send one already mapped payload.

Parameters:

  • payload (Hash)

    as built by MessageMapper

Returns:

  • (Hash)

    the parsed response body



24
25
26
27
# File 'lib/scaleway_tem/action_mailer/client.rb', line 24

def send_email(payload)
  response = post("#{region_path}/emails", payload)
  parse!(response)
end