Module: Roda::RodaPlugins::MailProcessor::ClassMethods

Defined in:
lib/roda/plugins/mail_processor.rb

Instance Method Summary collapse

Instance Method Details

#freezeObject

Freeze the rcpt routes if they are present.



289
290
291
292
293
294
295
# File 'lib/roda/plugins/mail_processor.rb', line 289

def freeze
  if string_routes = opts[:mail_processor_string_routes].freeze
    string_routes.freeze
    opts[:mail_processor_regexp_routes].freeze
  end
  super
end

#process_mail(mail) ⇒ Object

Process the given Mail instance, calling the appropriate hooks depending on whether the mail was handled during processing.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/roda/plugins/mail_processor.rb', line 299

def process_mail(mail)
  scope = new("PATH_INFO"=>'', 'SCRIPT_NAME'=>'', "REQUEST_METHOD"=>"PROCESSMAIL", 'rack.input'=>StringIO.new, 'roda.mail'=>mail)

  begin
    begin
      scope.process_mail
    rescue UnhandledMail
      scope.unhandled_mail_hook
    else
      scope.handled_mail_hook
    end
  ensure
    scope.after_mail_hook
  end
end

#process_mailbox(opts = OPTS) ⇒ Object

Process all mail in the given mailbox. If the :retriever option is given, should be an object supporting the Mail retriever API, otherwise uses the default Mail retriever_method. This deletes retrieved mail from the mailbox after processing, so that when called multiple times it does not reprocess the same mail. If mail should be archived and not deleted, the after_mail method should be used to perform the archiving of the mail.



321
322
323
324
# File 'lib/roda/plugins/mail_processor.rb', line 321

def process_mailbox(opts=OPTS)
  (opts[:retriever] || Mail).find_and_delete(opts.dup){|m| process_mail(m)}
  nil
end

#rcpt(*addresses, &block) ⇒ Object

Setup a routing tree for the given recipient addresses, which can be strings or regexps. Any messages matching the given recipient address will use these routing trees instead of the normal routing tree.



329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# File 'lib/roda/plugins/mail_processor.rb', line 329

def rcpt(*addresses, &block)
  opts[:mail_processor_string_routes] ||= {}
  opts[:mail_processor_regexp_routes] ||= {}
  string_meth = nil
  regexp_meth = nil
  addresses.each do |address|
    case address
    when String
      unless string_meth
        string_meth = define_roda_method("mail_processor_string_route_#{address}", 1, &convert_route_block(block))
      end
      opts[:mail_processor_string_routes][address] = string_meth 
    when Regexp
      unless regexp_meth
        regexp_meth = define_roda_method("mail_processor_regexp_route_#{address}", :any, &convert_route_block(block))
      end
      opts[:mail_processor_regexp_routes][address] = regexp_meth
    else
      raise RodaError, "invalid address format passed to rcpt, should be Array or String"
    end
  end
  nil
end