Class: Terminus::Controller
- Inherits:
-
Object
- Object
- Terminus::Controller
- Includes:
- Timeouts
- Defined in:
- lib/terminus/controller.rb
Constant Summary
Constants included from Timeouts
Instance Method Summary collapse
- #browser(id = nil) ⇒ Object
- #browser=(params) ⇒ Object
- #browsers ⇒ Object
- #cookies ⇒ Object
- #drop_browser(browser) ⇒ Object
- #ensure_browsers(n = 1) ⇒ Object
-
#initialize ⇒ Controller
constructor
A new instance of Controller.
- #messenger ⇒ Object
- #register_local_port(port) ⇒ Object
- #return_to_dock ⇒ Object
- #rewrite_local(url, dock_host) ⇒ Object
- #rewrite_remote(url, dock_host = nil) ⇒ Object
- #server_running?(server) ⇒ Boolean
- #visit_url(url) ⇒ Object
- #visited?(url) ⇒ Boolean
Methods included from Timeouts
Constructor Details
#initialize ⇒ Controller
Returns a new instance of Controller.
6 7 8 9 10 11 12 |
# File 'lib/terminus/controller.rb', line 6 def initialize @connected = false @browsers = {} @host_aliases = {} @local_ports = [] @visited = Set.new end |
Instance Method Details
#browser(id = nil) ⇒ Object
14 15 16 17 |
# File 'lib/terminus/controller.rb', line 14 def browser(id = nil) return @browser if id.nil? @browsers[id] ||= Browser.new(self, id) end |
#browser=(params) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/terminus/controller.rb', line 23 def browser=(params) ensure_connection return @browser = params if Browser === params @browser = wait_with_timeout(:selected_browser) do @browsers.values.find { |b| b === params } end end |
#browsers ⇒ Object
19 20 21 |
# File 'lib/terminus/controller.rb', line 19 def browsers @browsers.values end |
#cookies ⇒ Object
31 32 33 |
# File 'lib/terminus/controller.rb', line 31 def @cookies ||= CookieJar::Jar.new end |
#drop_browser(browser) ⇒ Object
35 36 37 38 |
# File 'lib/terminus/controller.rb', line 35 def drop_browser(browser) @browsers.delete(browser.id) @browser = nil if @browser == browser end |
#ensure_browsers(n = 1) ⇒ Object
40 41 42 43 |
# File 'lib/terminus/controller.rb', line 40 def ensure_browsers(n = 1) ensure_connection wait_with_timeout(:browsers) { @browsers.size >= n } end |
#messenger ⇒ Object
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/terminus/controller.rb', line 45 def messenger Terminus.ensure_reactor_running return @messenger if defined?(@messenger) @messenger = Faye::Client.new(Terminus.endpoint) @messenger.subscribe('/terminus/ping', &method(:accept_ping)) @messenger.subscribe('/terminus/results', &method(:accept_result)) @messenger end |
#register_local_port(port) ⇒ Object
56 57 58 59 |
# File 'lib/terminus/controller.rb', line 56 def register_local_port(port) @local_ports << port @host_aliases[Host.new(URI.parse("http://localhost:#{port}/"))] = Host.new(URI.parse("http://localhost:80/")) end |
#return_to_dock ⇒ Object
61 62 63 |
# File 'lib/terminus/controller.rb', line 61 def return_to_dock @browsers.each { |id, b| b.return_to_dock } end |
#rewrite_local(url, dock_host) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/terminus/controller.rb', line 65 def rewrite_local(url, dock_host) uri = URI.parse(url) uri.host = '127.0.0.1' if uri.host == dock_host uri.path = '' if uri.path == '/' # 1.8.7 does not have Hash#key, and 1.9.2 gives warnings for #index remote_host = @host_aliases.respond_to?(:key) ? @host_aliases.key(Host.new(uri)) : @host_aliases.index(Host.new(uri)) if remote_host uri.scheme = remote_host.scheme uri.host = remote_host.host uri.port = remote_host.port end uri.path = '/' if uri.path == '' uri end |
#rewrite_remote(url, dock_host = nil) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/terminus/controller.rb', line 84 def rewrite_remote(url, dock_host = nil) protocol_relative = (url =~ /^\/\//) url = "http:#{url}" if protocol_relative uri = URI.parse(url) return uri unless URI::HTTP === uri if (uri.host =~ LOCALHOST or uri.host == dock_host) if uri.port == 80 uri.port = @host_aliases.keys.find { |h| h.host =~ LOCALHOST }.port end if local_ports.include?(uri.port) uri.host = dock_host return uri end end server = boot(uri) uri.scheme = protocol_relative ? nil : 'http' uri.host, uri.port = (dock_host || server.host), server.port uri rescue URI::InvalidURIError url end |
#server_running?(server) ⇒ Boolean
110 111 112 113 114 115 116 117 |
# File 'lib/terminus/controller.rb', line 110 def server_running?(server) return false unless server.port uri = URI.parse("http://#{server.host}:#{server.port}#{PING_PATH}") Net::HTTP.start(uri.host, uri.port) { |h| h.head(uri.path) } true rescue false end |
#visit_url(url) ⇒ Object
119 120 121 |
# File 'lib/terminus/controller.rb', line 119 def visit_url(url) @visited.add(url) end |
#visited?(url) ⇒ Boolean
123 124 125 126 127 |
# File 'lib/terminus/controller.rb', line 123 def visited?(url) visited = @visited.member?(url) @visited.delete(url) visited end |