module AlertsHelper

  class Alert
    def run_alert(alert)
      begin
        out = ""
        alert.query.each_line do |line|
          out += line
        end

        objs = eval(out)
        return [nil, 0] if objs.nil?

        # Count the number of objects returned, can't use count/size/length methods
        # because the object returned varies
        count = 0
        objs.each do |objs|
          count += 1
        end
        eval(alert.action) if count > 0

        return [objs, count]
      rescue Exception => e
        return [nil, 0]
      end
    end
  end

  # TODO: move the following hard-coded strings to application.rb configuration
  class Email
    def send_email(subject, text, html)
      Resque.logger = Logger.new("log/resque.log")
      ENV['MANDRILL_APIKEY'] = '---'

      require 'mandrill'
      m = Mandrill::API.new
      message = {
          :subject => subject,
          :from_name => "John V",
          :text => text,
          :to => [
              {
                  :email => "---",
                  :name => "---"
              }
          ],
          :html => html,
          :from_email => "---"
      }
      sending = m.messages.send message
      Resque.logger.debug sending
    end
  end
end
