class WebMinion::Step

A Step represents the individual operation that the bot will perform. This often includes grabbing an element from the DOM tree, or performing some operation on an element that has already been found.

Constants

VALID_METHODS

Attributes

is_validator[RW]
method[RW]
name[RW]
retain_element[RW]
saved_values[R]
skippable[RW]
target[RW]
value[RW]
vars[R]

Public Class Methods

new(fields = {}) click to toggle source
# File lib/web_minion/step.rb, line 36
def initialize(fields = {})
  fields.each_pair do |k, v|
    if valid_method?(k.to_sym)
      send("method=", k)
      @target = v
    else
      send("#{k}=", v)
    end
  end

  replace_all_variables
end

Public Instance Methods

method=(method) click to toggle source
# File lib/web_minion/step.rb, line 57
def method=(method)
  raise(InvalidMethodError, "Method: #{method} is not valid") unless valid_method?(method.to_sym)
  split = method.to_s.split("/").map(&:to_sym)
  @method = split.count > 1 ? "#{split[0]}_#{split[1]}".to_sym : method.to_sym
end
perform(bot, element = nil, saved_values) click to toggle source
# File lib/web_minion/step.rb, line 53
def perform(bot, element = nil, saved_values)
  bot.execute_step(@method, @target, @value, element, saved_values)
end
retain?() click to toggle source
# File lib/web_minion/step.rb, line 63
def retain?
  retain_element
end
valid_method?(method) click to toggle source
# File lib/web_minion/step.rb, line 71
def valid_method?(method)
  split = method.to_s.split("/").map(&:to_sym)
  if split.count > 1
    return true if VALID_METHODS[split[0]].include?(split[1])
  end
  VALID_METHODS[:main_methods].include?(method)
end
validator?() click to toggle source
# File lib/web_minion/step.rb, line 67
def validator?
  is_validator
end
vars=(vars) click to toggle source
# File lib/web_minion/step.rb, line 49
def vars=(vars)
  @vars = Hash[vars.collect{ |k, v| [k.to_s, v] }]
end

Private Instance Methods

handle_hash_replacement(hash) click to toggle source
# File lib/web_minion/step.rb, line 103
def handle_hash_replacement(hash)
  hash.each_pair do |k, v|
    hash[k] = replace_variable(v)
  end
  hash
end
replace_all_variables() click to toggle source
# File lib/web_minion/step.rb, line 81
def replace_all_variables
  %w(value target).each do |field|
    next if send(field).nil?
    send("#{field}=", replace_variable(send(field)))
  end
end
replace_variable(var) click to toggle source
# File lib/web_minion/step.rb, line 88
def replace_variable(var)
  if var.is_a?(Hash)
    return handle_hash_replacement(var)
  else
    return var unless var.is_a?(String)
    # This will handle email addresses
    return var if var.match(/\w+@\D+\.\D+/)
    if replace_var = var.match(/@(\D+)/)
      raise(NoValueForVariableError, "no variable to use found for #{replace_var}") unless @vars[replace_var[1]]
      var = @vars[replace_var[1]]
    end
  end
  var
end