class WebMinion::Action

Represents a group of steps that the bot can perform and valdiate have performed as expected

Attributes

key[R]
name[R]
on_failure[RW]
on_success[RW]
starting_action[R]
steps[R]

Public Class Methods

build_from_hash(fields = {}, vars = {}) click to toggle source
# File lib/web_minion/action.rb, line 19
def self.build_from_hash(fields = {}, vars = {})
  steps = fields["steps"].map do |step|
    begin
      Step.new(step.merge("vars" => vars))
    rescue NoValueForVariableError => e
      (step["skippable"] && (step["is_validator"].nil? || !step["is_validator"])) ? nil : raise(e, "Current step is missing variable. (step: #{step['name']})")
    end
  end
  steps = steps.reject(&:nil?)

  starting = (fields["starting"] || "false") == "false" ? false : true
  new(name: fields["name"], steps: steps, key: fields["key"],
      starting: starting, on_success: fields["on_success"],
      on_failure: fields["on_failure"])
end
new(fields = {}) click to toggle source
# File lib/web_minion/action.rb, line 10
def initialize(fields = {})
  @name = fields[:name]
  @key = fields[:key] || @name
  @starting_action = fields[:starting]
  @on_success = fields[:on_success]
  @on_failure = fields[:on_failure]
  send("steps=", fields[:steps])
end

Public Instance Methods

ending_action?() click to toggle source
# File lib/web_minion/action.rb, line 39
def ending_action?
  @on_success.nil?
end
generate_edges(all_actions) click to toggle source
# File lib/web_minion/action.rb, line 47
def generate_edges(all_actions)
  @on_success = all_actions[on_success]
  @on_failure = all_actions[on_failure]
end
next_actions() click to toggle source
# File lib/web_minion/action.rb, line 43
def next_actions
  [on_success, on_failure].compact
end
perform(bot, saved_values) click to toggle source

Again, boilerplate for initial setup

# File lib/web_minion/action.rb, line 61
def perform(bot, saved_values)
  element = nil
  status = @steps.map do |step|
    if step.validator?
      step.perform(bot, element, saved_values)
    else
      if step.retain?
        step.perform(bot, element, saved_values)
      else
        element = step.perform(bot, element, saved_values)
      end
      nil
    end
  end
  !status.reject(&:nil?).include?(false)
rescue StandardError => e
  puts e
  return false
end
starting_action?() click to toggle source
# File lib/web_minion/action.rb, line 35
def starting_action?
  @starting_action
end
steps=(steps) click to toggle source
# File lib/web_minion/action.rb, line 52
def steps=(steps)
  unless steps.last.validator?
    warn "WARNING: Action: #{@name}'s final step is not a validation step!"
    warn "An action can not confirm its success without a validation step!"
  end
  @steps = steps
end