Class: Pact::Server

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

Defined Under Namespace

Classes: Middleware

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, port) ⇒ Server

Returns a new instance of Server.



37
38
39
40
41
42
# File 'lib/pact/consumer/server.rb', line 37

def initialize(app, port)
  @app = app
  @middleware = Middleware.new(@app)
  @server_thread = nil # supress warnings
  @port = port
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



35
36
37
# File 'lib/pact/consumer/server.rb', line 35

def app
  @app
end

#portObject (readonly)

Returns the value of attribute port.



35
36
37
# File 'lib/pact/consumer/server.rb', line 35

def port
  @port
end

Class Method Details

.portsObject



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

def ports
  @ports ||= {}
end

Instance Method Details

#bootObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/pact/consumer/server.rb', line 73

def boot
  unless responsive?
    Pact::Server.ports[@app.object_id] = @port

    @server_thread = Thread.new do
      run_default_server(@middleware, @port)
    end

    Timeout.timeout(60) { @server_thread.join(0.1) until responsive? }
  end
rescue Timeout::Error
  raise "Rack application timed out during boot"
else
  self
end

#errorObject



48
49
50
# File 'lib/pact/consumer/server.rb', line 48

def error
  @middleware.error
end

#hostObject



52
53
54
# File 'lib/pact/consumer/server.rb', line 52

def host
  "localhost"
end

#reset_error!Object



44
45
46
# File 'lib/pact/consumer/server.rb', line 44

def reset_error!
  @middleware.error = nil
end

#responsive?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/pact/consumer/server.rb', line 56

def responsive?
  return false if @server_thread && @server_thread.join(0)

  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 SystemCallError
  return false
end

#run_default_server(app, port) ⇒ Object



68
69
70
71
# File 'lib/pact/consumer/server.rb', line 68

def run_default_server(app, port)
  require 'rack/handler/webrick'
  Rack::Handler::WEBrick.run(app, :Port => port, :AccessLog => [], :Logger => WEBrick::Log::new(nil, 0))
end