Class: WebMinion::Flow

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

Overview

A flow represents the top level watcher of a series of actions that are to be performed. It tracks the sucess or failure, where to go next given an outcome, and a history of all actions performed.

Defined Under Namespace

Classes: CyclicalFlowError, NoStartingActionError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actions, bot, vars = {}, name = "") ⇒ Flow

Returns a new instance of Flow.



19
20
21
22
23
24
25
26
# File 'lib/web_minion/flow.rb', line 19

def initialize(actions, bot, vars = {}, name = "")
  @actions = actions
  @bot = bot
  @name = name
  @vars = vars
  @history = nil
  @saved_values = {}
end

Instance Attribute Details

#actionsObject

Returns the value of attribute actions.



16
17
18
# File 'lib/web_minion/flow.rb', line 16

def actions
  @actions
end

#botObject

Returns the value of attribute bot.



16
17
18
# File 'lib/web_minion/flow.rb', line 16

def bot
  @bot
end

#curr_actionObject (readonly)

Returns the value of attribute curr_action.



17
18
19
# File 'lib/web_minion/flow.rb', line 17

def curr_action
  @curr_action
end

#historyObject

Returns the value of attribute history.



16
17
18
# File 'lib/web_minion/flow.rb', line 16

def history
  @history
end

#nameObject

Returns the value of attribute name.



16
17
18
# File 'lib/web_minion/flow.rb', line 16

def name
  @name
end

#saved_valuesObject (readonly)

Returns the value of attribute saved_values.



17
18
19
# File 'lib/web_minion/flow.rb', line 17

def saved_values
  @saved_values
end

#starting_actionObject (readonly)

Returns the value of attribute starting_action.



17
18
19
# File 'lib/web_minion/flow.rb', line 17

def starting_action
  @starting_action
end

#varsObject

Returns the value of attribute vars.



16
17
18
# File 'lib/web_minion/flow.rb', line 16

def vars
  @vars
end

Class Method Details

.build_from_hash(fields = {}) ⇒ Object



39
40
41
42
43
44
45
46
# File 'lib/web_minion/flow.rb', line 39

def self.build_from_hash(fields = {})
  flow = new([], nil, nil)
  flow.vars = fields[:vars] if fields[:vars]
  fields.each_pair do |k, v|
    flow.send("#{k}=", v)
  end
  flow
end

.build_via_json(rule_json, vars = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/web_minion/flow.rb', line 28

def self.build_via_json(rule_json, vars = {})
  ruleset = JSON.parse(rule_json)
  driver = ruleset["config"]["driver"] || "mechanize"
  bot = if driver == "mechanize"
          MechanizeBot.new(ruleset["config"])
        else
          CapybaraBot.new(ruleset["config"])
        end
  build_from_hash(ruleset["flow"].merge(bot: bot, vars: vars))
end

Instance Method Details

#all_actionsObject



60
61
62
# File 'lib/web_minion/flow.rb', line 60

def all_actions
  @actions.values
end

#performObject



64
65
66
67
68
69
70
# File 'lib/web_minion/flow.rb', line 64

def perform
  @history = FlowHistory.new
  status = execute_action(@starting_action, @saved_values)
  @history.end_time = Time.now
  @history.status = status
  results
end