Class: Crabfarm::Modes::Console::ConsoleDsl

Inherits:
Object
  • Object
show all
Defined in:
lib/crabfarm/modes/console.rb

Instance Method Summary collapse

Constructor Details

#initialize(_context) ⇒ ConsoleDsl

Returns a new instance of ConsoleDsl.



13
14
15
# File 'lib/crabfarm/modes/console.rb', line 13

def initialize(_context)
  @context = _context
end

Instance Method Details

#helpObject



51
52
53
# File 'lib/crabfarm/modes/console.rb', line 51

def help
  puts "Ejem..."
end

#reload!Object



17
18
19
20
21
# File 'lib/crabfarm/modes/console.rb', line 17

def reload!
  puts "Reloading crawler source".color(:green)
  ActiveSupport::Dependencies.clear
  @context.reset
end

#resetObject Also known as: r



23
24
25
26
# File 'lib/crabfarm/modes/console.rb', line 23

def reset
  puts "Resetting crawling context".color(:green)
  @context.reset
end

#transition(_name = nil, _params = {}) ⇒ Object Also known as: t



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/crabfarm/modes/console.rb', line 28

def transition(_name=nil, _params={})
  if _name.nil?
    puts "Must provide a state name".color(:red)
    return
  end

  begin
    elapsed = Benchmark.measure do
      puts "Transitioning to #{_name.to_s.camelize} state"
      doc = @context.run_state(_name, _params).output_as_json

      puts "State changed, generated document:"
      puts JSON.pretty_generate(doc).color(:green).gsub(/(^|\\n)/, '  ')
    end
    puts "Completed in #{elapsed.real} s"
  rescue EntityNotFoundError => e
    puts "#{e.to_s}".color(:red)
  rescue => e
    puts "#{e.to_s}".color(:red)
    puts e.backtrace
  end
end