Class: Capybara::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Server

Returns a new instance of Server.



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

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



5
6
7
# File 'lib/capybara/server.rb', line 5

def app
  @app
end

Instance Method Details

#bootObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/capybara/server.rb', line 23

def boot
  Capybara.log "application has already booted" and return if responsive?
  Capybara.log "booting Rack applicartion on port #{port}"
  start_time = Time.now
  Thread.new do
    Rack::Handler::Mongrel.run @app, :Port => port
  end
  Capybara.log "checking if application has booted"
  loop do
    Capybara.log("application has booted") and break if responsive?
    if Time.now - start_time > 10 
      Capybara.log "Rack application timed out during boot"
      exit
    end
    
    Capybara.log '.'
    sleep 1
  end
end

#hostObject



15
16
17
# File 'lib/capybara/server.rb', line 15

def host
  'localhost'
end

#portObject



11
12
13
# File 'lib/capybara/server.rb', line 11

def port
  8080
end

#responsive?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
# File 'lib/capybara/server.rb', line 43

def responsive?
  res = Net::HTTP.start(host, port) { |http| http.get('/') }

  if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
    return true
  end
rescue Errno::ECONNREFUSED 
  return false
end

#url(path) ⇒ Object



19
20
21
# File 'lib/capybara/server.rb', line 19

def url(path)
  "http://#{host}:#{port}#{path}"
end