Class: AutoBrewster::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/auto_brewster/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, port, rackup_path = 'config.ru', hostname = false) ⇒ Server

Returns a new instance of Server.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/auto_brewster/server.rb', line 13

def initialize(server, port, rackup_path = 'config.ru', hostname = false)
  ENV['RACK_ENV'] ||= 'test'

  @port = port
  @server = server
  @rackup_path = rackup_path
  @hostname = hostname
  @app = build_rack_app

  @middleware = AutoBrewster::Middleware.new(@app)
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



7
8
9
# File 'lib/auto_brewster/server.rb', line 7

def app
  @app
end

#hostnameObject

Returns the value of attribute hostname.



7
8
9
# File 'lib/auto_brewster/server.rb', line 7

def hostname
  @hostname
end

#portObject

Returns the value of attribute port.



7
8
9
# File 'lib/auto_brewster/server.rb', line 7

def port
  @port
end

#rackup_pathObject

Returns the value of attribute rackup_path.



7
8
9
# File 'lib/auto_brewster/server.rb', line 7

def rackup_path
  @rackup_path
end

#serverObject

Returns the value of attribute server.



7
8
9
# File 'lib/auto_brewster/server.rb', line 7

def server
  @server
end

Instance Method Details

#get_host_with_protocol_and_portObject



39
40
41
42
43
44
45
# File 'lib/auto_brewster/server.rb', line 39

def get_host_with_protocol_and_port
  if @hostname
    return @hostname
  else
    return "http://127.0.0.1:#{@port}"
  end
end

#startObject



25
26
27
28
29
30
31
# File 'lib/auto_brewster/server.rb', line 25

def start
  @server_thread = Thread.new do
    @server.call(@middleware, @port)
  end

  Timeout.timeout(10) { @server_thread.join(0.1) until responsive? }
end

#stopObject



33
34
35
36
37
# File 'lib/auto_brewster/server.rb', line 33

def stop
  return if @server_thread.nil?
  @server_thread.kill
  Timeout.timeout(10) { @server_thread.join(0.1) until !responsive? }
end