Sha256: b0ffffbeb9a5769ba3039dc062a0297cd8168a6c8e29f46266861db32404b7e9

Contents?: true

Size: 1.27 KB

Versions: 6

Compression:

Stored size: 1.27 KB

Contents

module NoNotifierNeeded
  module Send
    def send_now(which_email, *args)
      email_hash = translate_to_hash(which_email, args)
      Resque.enqueue(EmailProcessor, email_hash)
    end

    def send_in(time_from_now, which_email, *args)
      email_hash = translate_to_hash(which_email, args)
      Resque.enqueue_at(time_from_now, EmailProcessor, email_hash)
    end

    def send_at(what_time, which_email, *args)
      email_hash = translate_to_hash(which_email, args)
      time_from_now = what_time.is_a?(Time) ? what_time : Chronic.parse(what_time)
      Resque.enqueue_at(time_from_now, EmailProcessor, email_hash)
    end

    private
    def known_models
      @known_models || @known_models = ActiveRecord::Base.send( :subclasses )
    end

    def translate_to_hash(which_email, args)
      th = {}
      th[:which_email] = which_email

      args = args.flatten if args.respond_to?(:flatten)
      args.each do |a|
        next if a.blank?
        if known_models.include?(a.class)
          th[a.class.name.downcase.to_sym] = a.id
        elsif a.is_a?(Hash)
          a.each{|k,v| th[k.to_sym] = CGI.escapeHTML(v) }
        else
          raise ArgumentError.new("Unknown values passed to email procesor '#{a}' with #{args.inspect}")
        end
      end
      th
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
no_notifier_needed-0.1.8 lib/no_notifier_needed/send.rb
no_notifier_needed-0.1.7 lib/no_notifier_needed/send.rb
no_notifier_needed-0.1.6 lib/no_notifier_needed/send.rb
no_notifier_needed-0.1.5 lib/no_notifier_needed/send.rb
no_notifier_needed-0.1.4 lib/no_notifier_needed/send.rb
no_notifier_needed-0.1.3 lib/no_notifier_needed/send.rb