class SiteDiff::Webserver

SiteDiff Web Server.

Constants

DEFAULT_PORT

Simple web server for testing purposes.

Attributes

ports[RW]

Public Class Methods

new(start_port, dirs, opts = {}) { |self| ... } click to toggle source

Serve a list of directories.

# File lib/sitediff/webserver.rb, line 15
def initialize(start_port, dirs, opts = {})
  start_port ||= DEFAULT_PORT
  @ports = (start_port...(start_port + dirs.size)).to_a
  @dirs = dirs
  @opts = opts

  setup
  start_servers

  if block_given?
    yield self
    kill
  end
end

Public Instance Methods

kill() click to toggle source

Kills the server.

# File lib/sitediff/webserver.rb, line 32
def kill
  @threads.each(&:kill)
end
uris() click to toggle source

Maps URIs to defined ports and returns a list of URIs.

# File lib/sitediff/webserver.rb, line 44
def uris
  ports.map { |p| "http://localhost:#{p}" }
end
wait() click to toggle source

Waits for the server.

# File lib/sitediff/webserver.rb, line 38
def wait
  @threads.each(&:join)
end

Protected Instance Methods

server(opts) click to toggle source
# File lib/sitediff/webserver.rb, line 58
def server(opts)
  WEBrick::HTTPServer.new(opts)
end
setup() click to toggle source
# File lib/sitediff/webserver.rb, line 50
def setup
  @server_opts = {}
  if @opts[:quiet]
    @server_opts[:Logger] = WEBrick::Log.new(IO::NULL)
    @server_opts[:AccessLog] = []
  end
end
start_servers() click to toggle source
# File lib/sitediff/webserver.rb, line 62
def start_servers
  @threads = []
  @dirs.each_with_index do |dir, idx|
    @server_opts[:Port] = @ports[idx]
    @server_opts[:DocumentRoot] = dir
    srv = server(@server_opts)
    @threads << Thread.new { srv.start }
  end
end