class AutoBrewster::Server

Attributes

app[RW]
hostname[RW]
port[RW]
rackup_path[RW]
server[RW]

Public Class Methods

new(server, port, timeout = 10, rackup_path = 'config.ru', hostname = false) click to toggle source
# File lib/auto_brewster/server.rb, line 13
def initialize(server, port, timeout = 10, rackup_path = 'config.ru', hostname = false)
  ENV['RACK_ENV'] ||= 'test'

  @port = port
  @server = server
  @timeout = timeout
  @rackup_path = rackup_path
  @hostname = hostname
  @app = build_rack_app
  @middleware = AutoBrewster::Middleware.new(@app)
end

Public Instance Methods

get_host_with_protocol_and_port() click to toggle source
# File lib/auto_brewster/server.rb, line 40
def get_host_with_protocol_and_port
  if @hostname
    return @hostname
  else
    return "http://127.0.0.1:#{@port}"
  end
end
start() click to toggle source
# File lib/auto_brewster/server.rb, line 25
def start
  @server_thread = Thread.new do
    AutoBrewster.include_support_post_launch
    @server.call(@middleware, @port)
  end

  Timeout.timeout(@timeout) { @server_thread.join(0.1) until responsive? }
end
stop() click to toggle source
# File lib/auto_brewster/server.rb, line 34
def stop
  return if @server_thread.nil?
  @server_thread.kill
  Timeout.timeout(@timeout) { @server_thread.join(0.1) until !responsive? }
end

Private Instance Methods

build_rack_app() click to toggle source
# File lib/auto_brewster/server.rb, line 61
def build_rack_app
  rackup_path = @rackup_path

  Rack::Builder.new do
    map '/' do
      run Rack::Builder.parse_file(rackup_path)[0]
    end
  end.to_app
end
responsive?() click to toggle source
# File lib/auto_brewster/server.rb, line 49
def responsive?
  return false if @server_thread and @server_thread.join(0)

  res = Net::HTTP.start('127.0.0.1', @port) { |http| http.get('/__identify__') }

  if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
    return res.body == @app.object_id.to_s
  end
rescue SystemCallError
  return false
end