defmodule Apostle.Queue do defstruct emails: [] @moduledoc """ Functions for working with `Apostle.Queue` structs, which represent a number of emails, all to be sent at once. """ def init do %Apostle.Queue{} end def init %Apostle.Mail{}=mail do init [mail] end def init(mails) when is_list(mails) do Enum.reduce mails, init, fn(mail, queue)-> push queue, mail end end @doc """ Push a mail onto the queue. """ def push %Apostle.Queue{emails: emails}=queue, %Apostle.Mail{}=mail do Map.put queue, :emails, [mail | emails] end @doc """ Deliver the contents of the queue via the Apostle API. """ def deliver %Apostle.Queue{}=queue do payload = Poison.encode! queue HTTPoison.post Apostle.delivery_host, payload, content_type: "application/json", apostle_client: "Elixir/#{Apostle.version}", authorization: "Bearer #{Apostle.domain_key}" end end