Class: Hanami::Mailer::Delivery::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/mailer/delivery/result.rb

Overview

Represents the outcome of a message delivery attempt.

This is the base class for delivery results. Delivery methods return an instance of this class (or a subclass) from their #call method. Third-party delivery methods are encouraged to subclass this and add any service-specific attributes they need.

Examples:

Checking a result

result = mailer.deliver(user: user)
if result.success?
  log.info "Delivered to #{result.message.to.join(', ')}"
else
  log.error "Delivery failed: #{result.error.message}"
end

A third-party delivery method returning a richer result

class Delivery::Postmark::Result < Hanami::Mailer::Delivery::Result
  attr_reader :message_id, :submitted_at

  def initialize(message_id:, submitted_at: nil, **)
    super(**)
    @message_id   = message_id
    @submitted_at = 
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message:, response: nil, success: true, error: nil) ⇒ Result

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Result.

Parameters:

  • message (Hanami::Mailer::Message)

    the prepared message

  • response (Object, nil) (defaults to: nil)

    the raw response from the delivery method

  • success (Boolean) (defaults to: true)

    whether delivery succeeded (default: true)

  • error (Exception, nil) (defaults to: nil)

    the exception raised, if delivery failed



64
65
66
67
68
69
# File 'lib/hanami/mailer/delivery/result.rb', line 64

def initialize(message:, response: nil, success: true, error: nil)
  @message  = message
  @response = response
  @success  = success
  @error    = error
end

Instance Attribute Details

#errorException? (readonly)

The exception raised during delivery, if delivery failed.

Returns:

  • (Exception, nil)


56
57
58
# File 'lib/hanami/mailer/delivery/result.rb', line 56

def error
  @error
end

#messageHanami::Mailer::Message (readonly)

The prepared message that was (or was attempted to be) delivered.



38
39
40
# File 'lib/hanami/mailer/delivery/result.rb', line 38

def message
  @message
end

#responseObject? (readonly)

The raw return value from the delivery method, if any.

For SMTP delivery this will be the Mail::Message object. For test delivery this will be nil. The exact type is delivery-method-specific; consult the documentation for the delivery method you are using.

Returns:

  • (Object, nil)


49
50
51
# File 'lib/hanami/mailer/delivery/result.rb', line 49

def response
  @response
end

Instance Method Details

#success?Boolean

Returns true if delivery succeeded.

Returns:

  • (Boolean)


76
77
78
# File 'lib/hanami/mailer/delivery/result.rb', line 76

def success?
  @success
end