Class: Mailkite::DeliveryMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/mailkite/delivery_method.rb

Defined Under Namespace

Classes: UnsupportedFeatureError

Constant Summary collapse

HANDLED_HEADERS =

Top-level headers the conversion consumes, plus the structural headers that only describe the MIME document (the API composes its own MIME). Anything else raises UnsupportedFeatureError — POST /v1/send takes no custom headers.

%w[
  from to cc bcc subject reply-to in-reply-to
  date message-id mime-version content-type content-transfer-encoding
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ DeliveryMethod

settings: api_key (falls back to ENV), optional base_url, or bring your own client — anything with send(message) (e.g. a Mailkite::Client, or a stub in tests). Takes precedence over api_key.



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mailkite/delivery_method.rb', line 38

def initialize(settings = {})
  require "mail" # lazy — only delivery-method users need the mail gem
  @settings = settings
  @client = settings[:client]
  return if @client

  api_key = settings[:api_key] || ENV["MAILKITE_API_KEY"]
  if api_key.nil? || api_key.empty?
    raise ArgumentError, "MailKite API key missing — pass api_key: in mailkite_settings or set MAILKITE_API_KEY"
  end
  @client = Client.new(api_key, settings[:base_url] || DEFAULT_BASE_URL)
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



32
33
34
# File 'lib/mailkite/delivery_method.rb', line 32

def settings
  @settings
end

Instance Method Details

#deliver!(mail) ⇒ Object

Deliver a Mail::Message via POST /v1/send. Returns the API response hash ({ "id" => …, "status" => … }); API failures raise Mailkite::Error.



53
54
55
# File 'lib/mailkite/delivery_method.rb', line 53

def deliver!(mail)
  @client.send(payload_for(mail))
end