Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

A bit of refactoring

  • Loading branch information...
commit c42a8471026d5207d93e6fe870d887326b2b8d24 1 parent 1b03bb3
John Wilger authored

Showing 1 changed file with 20 additions and 10 deletions. Show diff stats Hide diff stats

  1. +20 10 lib/capybara/server.rb
30 lib/capybara/server.rb
@@ -24,7 +24,7 @@ def ports
24 24 end
25 25 end
26 26
27   - attr_reader :app, :port
  27 + attr_reader :app
28 28
29 29 def initialize(app, port = nil)
30 30 @app = app
@@ -32,6 +32,10 @@ def initialize(app, port = nil)
32 32 @server_thread = nil # supress warnings
33 33 end
34 34
  35 + def port
  36 + Capybara::Server.ports[@app.object_id]
  37 + end
  38 +
35 39 def host
36 40 Capybara.server_host || "127.0.0.1"
37 41 end
@@ -47,7 +51,7 @@ def url(path)
47 51 def responsive?
48 52 return false if @server_thread && @server_thread.join(0)
49 53
50   - res = Net::HTTP.start(host, @port) { |http| http.get('/__identify__') }
  54 + res = Net::HTTP.start(host, port) { |http| http.get('/__identify__') }
51 55
52 56 if res.is_a?(Net::HTTPSuccess) or res.is_a?(Net::HTTPRedirection)
53 57 return res.body == @app.object_id.to_s
@@ -58,16 +62,11 @@ def responsive?
58 62
59 63 def boot
60 64 if @app
61   - @port = Capybara::Server.ports[@app.object_id]
62   -
63   - if not @port or not responsive?
64   - @port = @init_port || Capybara.server_port || find_available_port
65   - Capybara::Server.ports[@app.object_id] = @port
66   -
  65 + if not port or not responsive?
  66 + bind_port
67 67 @server_thread = Thread.new do
68   - Capybara.server.call(Identify.new(@app), @port)
  68 + Capybara.server.call(Identify.new(@app), port)
69 69 end
70   -
71 70 Timeout.timeout(60) { @server_thread.join(0.1) until responsive? }
72 71 end
73 72 end
@@ -78,6 +77,17 @@ def boot
78 77 end
79 78
80 79 private
  80 + def bind_port
  81 + self.port = port_to_bind
  82 + end
  83 +
  84 + def port=(port)
  85 + Capybara::Server.ports[@app.object_id] = port
  86 + end
  87 +
  88 + def port_to_bind
  89 + @init_port || Capybara.server_port || find_available_port
  90 + end
81 91
82 92 def find_available_port
83 93 server = TCPServer.new('127.0.0.1', 0)

0 comments on commit c42a847

Please sign in to comment.
Something went wrong with that request. Please try again.