Class: Capybara::Server

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

Defined Under Namespace

Classes: Identify

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Server

Returns a new instance of Server.



30
31
32
# File 'lib/capybara/server.rb', line 30

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



28
29
30
# File 'lib/capybara/server.rb', line 28

def app
  @app
end

#portObject (readonly)

Returns the value of attribute port.



28
29
30
# File 'lib/capybara/server.rb', line 28

def port
  @port
end

Class Method Details

.portsObject



23
24
25
# File 'lib/capybara/server.rb', line 23

def ports
  @ports ||= {}
end

Instance Method Details

#bootObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/capybara/server.rb', line 71

def boot
  if @app
    @port = Capybara::Server.ports[@app.object_id]
      
    if not @port or not responsive?
      @port = find_available_port
      Capybara::Server.ports[@app.object_id] = @port

      Thread.new { handler.run(Identify.new(@app), :Port => @port, :AccessLog => []) }

      Capybara.timeout(10) { if responsive? then true else sleep(0.5) and false end }
    end
  end
rescue Timeout::Error
  puts "Rack application timed out during boot"
  exit
else
  self
end

#handlerObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/capybara/server.rb', line 56

def handler
  begin
    require 'rack/handler/thin'
    Rack::Handler::Thin
  rescue LoadError
    begin
      require 'rack/handler/mongrel'
      Rack::Handler::Mongrel
    rescue LoadError
      require 'rack/handler/webrick'
      Rack::Handler::WEBrick
    end
  end
end

#hostObject



34
35
36
# File 'lib/capybara/server.rb', line 34

def host
  "localhost" 
end

#responsive?Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
# File 'lib/capybara/server.rb', line 46

def responsive?
  res = Net::HTTP.start(host, @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 Errno::ECONNREFUSED, Errno::EBADF
  return false
end

#url(path) ⇒ Object



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

def url(path)
  if path =~ /^http/
    path
  else
    (Capybara.app_host || "http://#{host}:#{port}") + path.to_s
  end
end