module Crabfarm::Modes::Recorder::Memento

Public Instance Methods

start(_target, _replay=false) click to toggle source
# File lib/crabfarm/modes/recorder/memento.rb, line 12
def start(_target, _replay=false)
  return puts "Must provide a recording target" unless _target.is_a? String

  target_path = Utils::Resolve.memento_path _target
  return puts "Memento file does not exist: #{target_path}" if _replay and not File.exist? target_path

  start_crabtrap _replay, target_path

  begin
    driver = build_driver Crabfarm.config.recorder_driver
    return puts "Invalid recorder_driver name '#{Crabfarm.config.recorder_driver}'" if driver.nil?

    begin
      puts "Press Ctrl-C or close browser to stop #{_replay ? 'playback' : 'capturing'}."
      loop do
        driver.window_handle
        sleep 1.0
      end
    rescue SystemExit, Interrupt
      # Nothing for now
    rescue Exception => e
      # Nothing for now
      # puts e.class.to_s
      # puts e.backtrace
    end

    puts "Releasing crawling context".color(:green)
    driver.quit rescue nil
  ensure
    crabtrap.stop
  end
end

Private Instance Methods

build_driver(_name) click to toggle source
# File lib/crabfarm/modes/recorder/memento.rb, line 61
def build_driver(_name)
  case _name.to_sym
  when :firefox
    Crabfarm::Support::WebdriverFactory.build_firefox_driver driver_config
  when :chrome
    Crabfarm::Support::WebdriverFactory.build_chrome_driver driver_config
  else return nil end
end
crabtrap() click to toggle source
# File lib/crabfarm/modes/recorder/memento.rb, line 57
def crabtrap
  @crabtrap
end
driver_config() click to toggle source
# File lib/crabfarm/modes/recorder/memento.rb, line 70
def driver_config
  {
    proxy: "127.0.0.1:#{crabtrap.port}",
    window_width: Crabfarm.config.webdriver_window_width,
    window_height: Crabfarm.config.webdriver_window_height
  }
end
start_crabtrap(_replay, _target_path) click to toggle source
# File lib/crabfarm/modes/recorder/memento.rb, line 47
def start_crabtrap(_replay, _target_path)
  crabtrap_config = Crabfarm.config.crabtrap_config
  crabtrap_config[:mode] = _replay ? :replay : :capture
  crabtrap_config[:port] = Utils::PortDiscovery.find_available_port
  crabtrap_config[:bucket_path] = _target_path

  @crabtrap = CrabtrapRunner.new crabtrap_config
  @crabtrap.start
end