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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/capybara/server.rb', line 56

def boot
  if @app
    @port = Capybara::Server.ports[@app.object_id]

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

      Thread.new do
        Capybara.server.call(Identify.new(@app), @port)
      end

      Capybara.timeout(Capybara.server_boot_timeout) do
        if responsive? then true else sleep(0.5) and false end
      end
    end
  end
rescue TimeoutError
  puts "Rack application timed out during boot"
  exit
else
  self
end

#hostObject



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

def host
  "127.0.0.1"
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