require 'twitter' MAX_PAGES = 5 unless defined?(MAX_PAGES) TWITTER_RPP = 40 unless defined?(TWITTER_RPP) module RulesEngine module Rule class <%=rule_class%> < RulesEngine::Rule::Definition attr_reader :message attr_reader :plan ################################################################## # class options self.options = { :group => 'Twitter', :display_name => 'Twitter Reader', :help_partial => '/re_rule_definitions/<%=rule_name%>/help', :new_partial => '/re_rule_definitions/<%=rule_name%>/new', :edit_partial => '/re_rule_definitions/<%=rule_name%>/edit' } ################################################################## # set the rule data def data= data if data.nil? @title = nil @message = nil @plan = nil else @title, @message, @plan = ActiveSupport::JSON.decode(data) end end ################################################################## # get the rule attributes def title @title end def summary "Look for \"#{message}\" and send matches to the plan : #{plan}" end def data [title, message, plan].to_json end def expected_outcomes [{:outcome => RulesEngine::Rule::Outcome::NEXT}, {:outcome => RulesEngine::Rule::Outcome::STOP_FAILURE}] end ################################################################## # set the rule attributes def attributes=(params) param_hash = params.symbolize_keys @title = param_hash[:<%=rule_name%>_title] @message = param_hash[:<%=rule_name%>_message] @plan = param_hash[:<%=rule_name%>_plan] end ################################################################## # validation and errors def valid? @errors = {} @errors[:<%=rule_name%>_title] = "Title required" if title.blank? @errors[:<%=rule_name%>_message] = "Message required" if message.blank? @errors[:<%=rule_name%>_plan] = "Plan required" if plan.blank? return @errors.empty? end ################################################################## # callbacks when the rule is added and removed from a workflow def before_create() end def before_update() end def before_destroy() end ################################################################## # execute the rule def process(process_id, data) sub_process_id = RulesEngine::Process.runner.create RulesEngine::Process.auditor.audit(process_id, "Starting Sub Plan #{sub_process_id}", RulesEngine::Process::AUDIT_INFO) success = RulesEngine::Process.runner.run(sub_process_id, plan, data.merge(:message => message)) RulesEngine::Process.auditor.audit(process_id, "Finished Sub Plan #{sub_process_id}", success ? RulesEngine::Process::AUDIT_SUCCESS : RulesEngine::Process::AUDIT_FAILURE) return success ? RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::NEXT) : RulesEngine::Rule::Outcome.new(RulesEngine::Rule::Outcome::STOP_FAILURE) end end end end