Class: Resque::Mailer::MessageDecoy

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

Instance Method Summary collapse

Constructor Details

#initialize(mailer_class, method_name, *args) ⇒ MessageDecoy

Returns a new instance of MessageDecoy.



88
89
90
91
92
93
# File 'lib/resque_mailer.rb', line 88

def initialize(mailer_class, method_name, *args)
  @mailer_class = mailer_class
  @method_name = method_name
  *@args = *args
  actual_message if environment_excluded?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



168
169
170
# File 'lib/resque_mailer.rb', line 168

def method_missing(method_name, *args)
  actual_message.send(method_name, *args)
end

Instance Method Details

#actual_messageObject



115
116
117
# File 'lib/resque_mailer.rb', line 115

def actual_message
  @actual_message ||= @mailer_class.send(:new, @method_name, *@args).message
end

#current_envObject



99
100
101
102
103
104
105
# File 'lib/resque_mailer.rb', line 99

def current_env
  if defined?(Rails)
    ::Resque::Mailer.current_env || ::Rails.env
  else
    ::Resque::Mailer.current_env
  end
end

#deliverObject



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/resque_mailer.rb', line 119

def deliver
  return deliver! if environment_excluded?

  if @mailer_class.deliver?
    begin
      resque.enqueue(@mailer_class, @method_name, *@args)
    rescue Errno::ECONNREFUSED
      logger.error "Unable to connect to Redis; falling back to synchronous mail delivery" if logger
      deliver!
    end
  end
end

#deliver!Object



164
165
166
# File 'lib/resque_mailer.rb', line 164

def deliver!
  actual_message.deliver
end

#deliver_at(time) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/resque_mailer.rb', line 132

def deliver_at(time)
  return deliver! if environment_excluded?

  unless resque.respond_to? :enqueue_at
    raise "You need to install resque-scheduler to use deliver_at"
  end

  if @mailer_class.deliver?
    resque.enqueue_at(time, @mailer_class, @method_name, *@args)
  end
end

#deliver_in(time) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
# File 'lib/resque_mailer.rb', line 144

def deliver_in(time)
  return deliver! if environment_excluded?

  unless resque.respond_to? :enqueue_in
    raise "You need to install resque-scheduler to use deliver_in"
  end

  if @mailer_class.deliver?
    resque.enqueue_in(time, @mailer_class, @method_name, *@args)
  end
end

#environment_excluded?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/resque_mailer.rb', line 107

def environment_excluded?
  !ActionMailer::Base.perform_deliveries || excluded_environment?(current_env)
end

#excluded_environment?(name) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/resque_mailer.rb', line 111

def excluded_environment?(name)
  ::Resque::Mailer.excluded_environments && ::Resque::Mailer.excluded_environments.include?(name.to_sym)
end

#loggerObject



172
173
174
# File 'lib/resque_mailer.rb', line 172

def logger
  @mailer_class.logger
end

#resqueObject



95
96
97
# File 'lib/resque_mailer.rb', line 95

def resque
  ::Resque::Mailer.default_queue_target
end

#unschedule_deliveryObject



156
157
158
159
160
161
162
# File 'lib/resque_mailer.rb', line 156

def unschedule_delivery
  unless resque.respond_to? :remove_delayed
    raise "You need to install resque-scheduler to use unschedule_delivery"
  end

  resque.remove_delayed(@mailer_class, @method_name, *@args)
end