class Madness::Browser
Handles browser launching
Attributes
Public Class Methods
Source
# File lib/madness/browser.rb, line 9 def initialize(host, port) @host = host @port = port end
Public Instance Methods
Source
# File lib/madness/browser.rb, line 42 def open fork do if server_running? success = open! yield success ? nil : "Failed opening browser (#{open_command.join ' '})" else yield "Failed connecting to #{server_url}. Is the server running?" end end end
Open a web browser if the server is running. This is done in a non-blocking manner, so it can be executed before starting the server. It will yield an error message if it fails, or nil on success.
Source
# File lib/madness/browser.rb, line 54 def open! system(*open_command, err: File::NULL, in: File::NULL, out: File::NULL) end
Runs the appropriate command (based on OS) to open a browser.
Source
# File lib/madness/browser.rb, line 59 def open_command @open_command ||= [OS.open_file_command, server_url] end
Returns the appropriate command (based on OS) to open a browser.
Source
# File lib/madness/browser.rb, line 23 def server_running?(retries: 5, delay: 1) connected = false attempts = 0 begin connected = Socket.tcp(host, port) rescue sleep delay retry if (attempts += 1) < retries ensure connected.close if connected end !!connected end
Returns true if the server is running. Will attempt to connect multiple times. This is designed to assist in running some code after the server has launched.
Source
# File lib/madness/browser.rb, line 15 def server_url url_host = ['0.0.0.0', '127.0.0.1'].include?(host) ? 'localhost' : host "http://#{url_host}:#{port}" end
Returns a URL based on host, port and MADNESS_FORCE_SSL.