Class: Caffeinate::ActionProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/caffeinate/action_proxy.rb

Overview

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

Defined Under Namespace

Classes: DeliveryMethod

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActionProxy

Returns a new instance of ActionProxy.



50
51
52
53
# 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

Instance Attribute Details

#action_nameObject (readonly)

Returns the value of attribute action_name.



42
43
44
# File 'lib/caffeinate/action_proxy.rb', line 42

def action_name
  @action_name
end

#caffeinate_mailingObject

Returns the value of attribute caffeinate_mailing.



40
41
42
# File 'lib/caffeinate/action_proxy.rb', line 40

def caffeinate_mailing
  @caffeinate_mailing
end

#perform_deliveriesObject

Returns the value of attribute perform_deliveries.



41
42
43
# File 'lib/caffeinate/action_proxy.rb', line 41

def perform_deliveries
  @perform_deliveries
end

Class Method Details

.abstract?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'lib/caffeinate/action_proxy.rb', line 86

def abstract?
  true
end

.action_methodsObject



56
57
58
59
60
61
62
63
64
# 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_methodsObject



66
67
68
69
70
71
# 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) ⇒ Object



73
74
75
76
77
78
79
# 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

.respond_to_missing?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/caffeinate/action_proxy.rb', line 82

def respond_to_missing?(method, include_all = false)
  action_methods.include?(method.to_s) || super
end

Instance Method Details

#deliverObject

Follows Mail::Message



98
99
100
101
102
103
# File 'lib/caffeinate/action_proxy.rb', line 98

def deliver
  inform_interceptors
  do_delivery
  inform_observers
  self
end

#deliver!Object

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



111
112
113
114
115
116
117
118
119
# 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) ⇒ Object



91
92
93
94
95
# 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