Class: Crabfarm::Live::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/crabfarm/live/manager.rb

Constant Summary collapse

INJECTION_TM =

seconds

5

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



13
14
15
16
# File 'lib/crabfarm/live/manager.rb', line 13

def initialize
  @port = Utils::PortDiscovery.find_available_port
  @driver_name = Crabfarm.config.recorder_driver
end

Instance Method Details

#attach(_primary = true) ⇒ Object

Viewer implementation



119
120
121
# File 'lib/crabfarm/live/manager.rb', line 119

def attach(_primary=true)
  if _primary then primary_driver else build_driver end
end

#block_requestsObject



49
50
51
52
53
54
55
56
# File 'lib/crabfarm/live/manager.rb', line 49

def block_requests
  begin
    stop_crabtrap
    return yield
  ensure
    set_memento nil
  end
end

#detach(_driver) ⇒ Object



123
124
125
126
127
# File 'lib/crabfarm/live/manager.rb', line 123

def detach(_driver)
  if _driver != primary_driver
    _driver.quit rescue nil
  end
end

#inject_web_toolsObject



86
87
88
89
90
91
92
93
94
# File 'lib/crabfarm/live/manager.rb', line 86

def inject_web_tools
  Utils::Console.trap_errors 'injecting web tools' do
    Utils::Webdriver.inject_style primary_driver, 'https://www.crabtrap.io/selectorgadget_combined.css'
    Utils::Webdriver.inject_style primary_driver, 'https://www.crabtrap.io/tools.css'
    Utils::Webdriver.inject_script primary_driver, 'https://www.crabtrap.io/selectorgadget_combined.js'
    Utils::Webdriver.inject_script primary_driver, 'https://www.crabtrap.io/tools.js'
    Timeout::timeout(INJECTION_TM) { wait_for_injection }
  end
end

#primary_driverObject



33
34
35
# File 'lib/crabfarm/live/manager.rb', line 33

def primary_driver
  @driver
end

#proxy_portObject



18
19
20
# File 'lib/crabfarm/live/manager.rb', line 18

def proxy_port
  @port
end

#reset_driver_statusObject



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/crabfarm/live/manager.rb', line 37

def reset_driver_status
  if Crabfarm.config.live_full_reload
    # recreate driver if configured to do so
    release_primary_driver
    load_primary_driver
  else
    primary_driver.manage.delete_all_cookies
  end

  primary_driver.get('https://www.crabtrap.io/instructions.html')
end

#set_memento(_memento = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/crabfarm/live/manager.rb', line 58

def set_memento(_memento=nil)

  options = if _memento
    path = Utils::Resolve.memento_path _memento
    raise ConfigurationError.new "No memento found at #{path}" unless File.exists? path
    { mode: :replay, bucket_path: path }
  else
    { mode: :pass }
  end

  options.merge!({
    port: @port,
    virtual: File.expand_path('./assets/live-tools', Crabfarm.root)
  })

  stop_crabtrap
  @crabtrap = CrabtrapRunner.new config.crabtrap_config.merge(options)
  @crabtrap.start

end

#show_dialog(_status, _title, _subtitle, _content = nil, _content_type = :text) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/crabfarm/live/manager.rb', line 96

def show_dialog(_status, _title, _subtitle, _content=nil, _content_type=:text)
  Utils::Console.trap_errors 'loading web dialog' do
    primary_driver.execute_script(
      "window.crabfarm.showDialog.apply(null, arguments);",
      _status.to_s,
      _title,
      _subtitle,
      _content,
      _content_type.to_s
    );
  end
end

#show_selector_gadgetObject



109
110
111
112
113
114
115
# File 'lib/crabfarm/live/manager.rb', line 109

def show_selector_gadget()
  Utils::Console.trap_errors 'loading selector gadget' do
    primary_driver.execute_script(
      'window.crabfarm.showSelectorGadget();'
    )
  end
end

#startObject



22
23
24
25
26
# File 'lib/crabfarm/live/manager.rb', line 22

def start
  set_memento
  load_primary_driver
  primary_driver.get('https://www.crabtrap.io/welcome.html')
end

#stopObject



28
29
30
31
# File 'lib/crabfarm/live/manager.rb', line 28

def stop
  stop_crabtrap
  release_primary_driver
end

#stop_crabtrapObject



79
80
81
82
83
84
# File 'lib/crabfarm/live/manager.rb', line 79

def stop_crabtrap
  unless @crabtrap.nil?
    @crabtrap.kill
    @crabtrap = nil
  else nil end
end