Class: Cloudflare::Email::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudflare/email/client.rb

Constant Summary collapse

DEFAULT_BASE_URL =
"https://api.cloudflare.com/client/v4".freeze
DEFAULT_RETRIES =
3
DEFAULT_TIMEOUT =
30
DEFAULT_BACKOFF =
0.5
MAX_RETRY_AFTER =

seconds; never sleep longer than this even if server says so

60
RETRYABLE_NETWORK =
[
  Net::OpenTimeout, Net::ReadTimeout, Errno::ECONNRESET,
  Errno::ECONNREFUSED, Errno::EHOSTUNREACH, EOFError, SocketError,
  IOError
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_id:, api_token:, base_url: DEFAULT_BASE_URL, retries: DEFAULT_RETRIES, timeout: DEFAULT_TIMEOUT, initial_backoff: DEFAULT_BACKOFF, max_retry_after: MAX_RETRY_AFTER, logger: nil) ⇒ Client

Returns a new instance of Client.

Raises:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/cloudflare/email/client.rb', line 22

def initialize(account_id:, api_token:, base_url: DEFAULT_BASE_URL,
               retries: DEFAULT_RETRIES, timeout: DEFAULT_TIMEOUT,
               initial_backoff: DEFAULT_BACKOFF, max_retry_after: MAX_RETRY_AFTER,
               logger: nil)
  raise ConfigurationError, "account_id is required" if .nil? || .to_s.empty?
  raise ConfigurationError, "api_token is required"  if api_token.nil?  || api_token.to_s.empty?

  @account_id      = 
  @api_token       = api_token
  @base_url        = base_url
  @retries         = retries
  @timeout         = timeout
  @initial_backoff = initial_backoff
  @max_retry_after = max_retry_after
  @logger          = logger
end

Instance Attribute Details

#account_idObject (readonly)

Returns the value of attribute account_id.



20
21
22
# File 'lib/cloudflare/email/client.rb', line 20

def 
  @account_id
end

#base_urlObject (readonly)

Returns the value of attribute base_url.



20
21
22
# File 'lib/cloudflare/email/client.rb', line 20

def base_url
  @base_url
end

#retriesObject (readonly)

Returns the value of attribute retries.



20
21
22
# File 'lib/cloudflare/email/client.rb', line 20

def retries
  @retries
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



20
21
22
# File 'lib/cloudflare/email/client.rb', line 20

def timeout
  @timeout
end

Instance Method Details

#send(from:, to:, subject:, text: nil, html: nil, cc: nil, bcc: nil, reply_to: nil, headers: nil, attachments: nil) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloudflare/email/client.rb', line 39

def send(from:, to:, subject:, text: nil, html: nil, cc: nil, bcc: nil,
         reply_to: nil, headers: nil, attachments: nil)
  raise ValidationError, "must provide :text or :html" if text.nil? && html.nil?

  body = {
    from:    normalize_address(from),
    to:      wrap(to).map  { |addr| normalize_address(addr) },
    subject: subject,
  }
  body[:text]        = text if text
  body[:html]        = html if html
  body[:cc]          = wrap(cc).map  { |a| normalize_address(a) } if cc
  body[:bcc]         = wrap(bcc).map { |a| normalize_address(a) } if bcc
  body[:reply_to]    = normalize_address(reply_to) if reply_to
  body[:headers]     = headers if headers
  body[:attachments] = attachments if attachments

  perform(:send, "/accounts/#{@account_id}/email/sending/send", body)
end

#send_raw(from:, recipients:, mime_message:) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/cloudflare/email/client.rb', line 59

def send_raw(from:, recipients:, mime_message:)
  body = {
    from:         extract_address(from),
    recipients:   wrap(recipients).map { |r| extract_address(r) },
    mime_message: mime_message,
  }
  perform(:send_raw, "/accounts/#{@account_id}/email/sending/send_raw", body)
end