Class: Mailkube::Resources::ScheduledEmails

Inherits:
Object
  • Object
show all
Defined in:
lib/mailkube/resources/scheduled_emails.rb,
sig/mailkube/resources/scheduled_emails.rbs

Overview

The scheduled_emails namespace, reached as client.scheduled_emails.

Sends made with scheduled_at: are manageable here until they are due. A sent email has left the collection, so status: "sent" is a validation error rather than an empty result.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(transport) ⇒ ScheduledEmails

Returns a new instance of ScheduledEmails.

Parameters:

  • transport (#request_json)

    the transport performing this resource's requests.

  • (_JsonTransport)


51
52
53
54
55
# File 'lib/mailkube/resources/scheduled_emails.rb', line 51

def initialize(transport)
  @transport = transport
  @batches = ScheduledEmailBatches.new(transport)
  freeze
end

Instance Attribute Details

#batchesScheduledEmailBatches (readonly)

Returns the batch operations.

Returns:



48
49
50
# File 'lib/mailkube/resources/scheduled_emails.rb', line 48

def batches
  @batches
end

Instance Method Details

#cancel(email_id) ⇒ CanceledScheduledEmail

Cancel one scheduled email before it is sent.

Parameters:

  • email_id (String)

    the id the scheduled-send acknowledgement returned.

  • (String)

Returns:

Raises:



131
132
133
# File 'lib/mailkube/resources/scheduled_emails.rb', line 131

def cancel(email_id)
  CanceledScheduledEmail.from(@transport.request_json(ScheduledEmailRequests.cancel(email_id)))
end

#fetch_page(spec) ⇒ ScheduledEmailPage

Returns the parsed page.

Parameters:

Returns:



139
# File 'lib/mailkube/resources/scheduled_emails.rb', line 139

def fetch_page(spec) = ScheduledEmailPage.from(@transport.request_json(spec))

#get(email_id) ⇒ ScheduledEmail

Retrieve one scheduled email.

Parameters:

  • email_id (String)

    the id the scheduled-send acknowledgement returned.

  • (String)

Returns:

Raises:

  • (NotFoundError)

    when no such scheduled email exists, which is also what an id belonging to another organization reports.



108
109
110
# File 'lib/mailkube/resources/scheduled_emails.rb', line 108

def get(email_id)
  ScheduledEmail.from(@transport.request_json(ScheduledEmailRequests.get(email_id)))
end

#iter_all(status: nil, batch_id: nil, scheduled_at_gte: nil, scheduled_at_lte: nil, page: nil) ⇒ Object

A lazy Enumerator, so abandoning the walk early costs nothing.



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/mailkube/resources/scheduled_emails.rb', line 88

def iter_all(status: nil, batch_id: nil, scheduled_at_gte: nil, scheduled_at_lte: nil, page: nil)
  Enumerator.new do |yielder|
    current = list(status: status, batch_id: batch_id, scheduled_at_gte: scheduled_at_gte,
                   scheduled_at_lte: scheduled_at_lte, page: page)
    loop do
      current.data.each { |item| yielder << item }
      link = current.pagination.steps.next
      break if link.nil?

      current = fetch_page(ScheduledEmailRequests.page(link))
    end
  end
end

#list(status: nil, batch_id: nil, scheduled_at_gte: nil, scheduled_at_lte: nil, page: nil) ⇒ ScheduledEmailPage

List one page of scheduled emails.

Every filter is optional and an omitted one never reaches the wire. A list of statuses becomes one comma-joined parameter rather than a repeated one. The listing is scoped server-side to a rolling window around now, so a bound in the direction that can never match is rejected rather than silently returning nothing.

Parameters:

  • status (String, Array<String>, nil) (defaults to: nil)

    one status, or several. Only scheduled, canceled and failed can be listed.

  • batch_id (String, nil) (defaults to: nil)

    only emails grouped under this batch label.

  • scheduled_at_gte (Time, String, nil) (defaults to: nil)

    only emails due at or after this instant.

  • scheduled_at_lte (Time, String, nil) (defaults to: nil)

    only emails due at or before this instant.

  • page (Integer, nil) (defaults to: nil)

    the 1-based page number to fetch.

Returns:

Raises:

  • (APIError)

    on any non-2xx response.



72
73
74
75
76
# File 'lib/mailkube/resources/scheduled_emails.rb', line 72

def list(status: nil, batch_id: nil, scheduled_at_gte: nil, scheduled_at_lte: nil, page: nil)
  fetch_page(ScheduledEmailRequests.list(status: status, batch_id: batch_id,
                                         scheduled_at_gte: scheduled_at_gte,
                                         scheduled_at_lte: scheduled_at_lte, page: page))
end

#update(email_id, scheduled_at:, batch_id: nil) ⇒ ScheduledEmail

Reschedule one scheduled email, optionally moving it into or out of a batch.

The content of a scheduled email is immutable; only its due time and batch can change.

Parameters:

  • email_id (String)

    the id the scheduled-send acknowledgement returned.

  • scheduled_at (Time, String)

    the new due time.

  • batch_id (String, nil) (defaults to: nil)

    a batch to move the email into.

  • (String)
  • scheduled_at: (Time, String)
  • batch_id: (String, nil) (defaults to: nil)

Returns:

Raises:



121
122
123
124
# File 'lib/mailkube/resources/scheduled_emails.rb', line 121

def update(email_id, scheduled_at:, batch_id: nil)
  body = { "scheduled_at" => Serialization.to_iso(scheduled_at), "batch_id" => batch_id }.compact
  ScheduledEmail.from(@transport.request_json(ScheduledEmailRequests.update(email_id, body)))
end