class Caffeinate::ActionProxy

Allows you to use a PORO for a drip; acts just like ActionMailer::Base

Usage:

class TextAction < Caffeinate::ActionProxy
  def welcome(mailing)
    user = mailing.subscriber
    HTTParty.post("...") # ...
  end
end

In the future (when?), “mailing” objects will become “messages”.

Optionally, you can use the method for setup and return an object that implements ‘#deliver!` and that will be invoked.

usage:

class TextAction < Caffeinate::ActionProxy
  class Envelope(user)
    @sms = SMS.new(to: user.phone_number)
  end

  def deliver!(action)
    # action will be the instantiated TextAction object
    # and you can access action.action_name, etc.

    erb = ERB.new(File.read(Rails.root + "app/views/cool_one_off_action/#{action_object.action_name}.text.erb"))
    # ...
    @sms.send!
  end

  def welcome(mailing)
    Envelope.new(mailing.subscriber)
  end
end

Attributes

action_name[R]
caffeinate_mailing[RW]
perform_deliveries[RW]

Public Class Methods

abstract?() click to toggle source
# File lib/caffeinate/action_proxy.rb, line 86
def abstract?
  true
end
action_methods() click to toggle source
# File lib/caffeinate/action_proxy.rb, line 56
def action_methods
  @action_methods ||= begin
                        methods = (public_instance_methods(true) -
                          internal_methods +
                          public_instance_methods(false))
                        methods.map!(&:to_s)
                        methods.to_set
                      end
end
internal_methods() click to toggle source
# File lib/caffeinate/action_proxy.rb, line 66
def internal_methods
  controller = self

  controller = controller.superclass until controller.abstract?
  controller.public_instance_methods(true)
end
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/caffeinate/action_proxy.rb, line 73
def method_missing(method_name, *args)
  if action_methods.include?(method_name.to_s)
    ::Caffeinate::MessageHandler.new(self, method_name, *args)
  else
    super
  end
end
new() click to toggle source
# File lib/caffeinate/action_proxy.rb, line 50
def initialize
  @delivery_method = DeliveryMethod.new
  @perform_deliveries = true # will only be false if interceptors set it so
end
respond_to_missing?(method, include_all = false) click to toggle source
Calls superclass method
# File lib/caffeinate/action_proxy.rb, line 82
def respond_to_missing?(method, include_all = false)
  action_methods.include?(method.to_s) || super
end

Public Instance Methods

deliver() click to toggle source

Follows Mail::Message

# File lib/caffeinate/action_proxy.rb, line 98
def deliver
  inform_interceptors
  do_delivery
  inform_observers
  self
end
deliver!() click to toggle source

This method bypasses checking perform_deliveries and raise_delivery_errors, so use with caution.

It still however fires off the interceptors and calls the observers callbacks if they are defined.

Returns self

# File lib/caffeinate/action_proxy.rb, line 111
def deliver!
  inform_interceptors
  handled = send(@action_name, @action_args)
  if handled.respond_to?(:deliver!) && !handled.is_a?(Caffeinate::Mailing)
    handled.deliver!(self)
  end
  inform_observers
  self
end
process(action_name, action_args) click to toggle source
# File lib/caffeinate/action_proxy.rb, line 91
def process(action_name, action_args)
  @action_name = action_name # pass-through for #send
  @action_args = action_args # pass-through for #send
  self.caffeinate_mailing = action_args if action_args.is_a?(Caffeinate::Mailing)
end

Private Instance Methods

do_delivery() click to toggle source

In your action’s method (@action_name), if you return an object that responds to ‘deliver!` we’ll invoke it. This is useful for doing setup in the method and then firing it later.

# File lib/caffeinate/action_proxy.rb, line 133
def do_delivery
  begin
    if perform_deliveries
      handled = send(@action_name, @action_args)
      if handled.respond_to?(:deliver!) && !handled.is_a?(Caffeinate::Mailing)
        handled.deliver!(self)
      end
    end
  rescue => e
    raise e
  end
end
inform_interceptors() click to toggle source
# File lib/caffeinate/action_proxy.rb, line 123
def inform_interceptors
  ::Caffeinate::ActionMailer::Interceptor.delivering_email(self)
end
inform_observers() click to toggle source
# File lib/caffeinate/action_proxy.rb, line 127
def inform_observers
  ::Caffeinate::ActionMailer::Observer.delivered_email(self)
end