Class: Pact::Server
- Inherits:
-
Object
- Object
- Pact::Server
- Defined in:
- lib/pact/consumer/server.rb
Defined Under Namespace
Classes: Middleware
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
Instance Method Summary collapse
- #boot ⇒ Object
- #error ⇒ Object
- #get_identity ⇒ Object
- #host ⇒ Object
-
#initialize(app, port, options = {}) ⇒ Server
constructor
A new instance of Server.
- #reset_error! ⇒ Object
- #responsive? ⇒ Boolean
- #run_default_server(app, port) ⇒ Object
- #ssl_opts ⇒ Object
- #webrick_opts ⇒ Object
Constructor Details
#initialize(app, port, options = {}) ⇒ Server
Returns a new instance of Server.
39 40 41 42 43 44 45 |
# File 'lib/pact/consumer/server.rb', line 39 def initialize(app, port, = {}) @app = app @middleware = Middleware.new(@app) @server_thread = nil @port = port @options = end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
37 38 39 |
# File 'lib/pact/consumer/server.rb', line 37 def app @app end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
37 38 39 |
# File 'lib/pact/consumer/server.rb', line 37 def @options end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
37 38 39 |
# File 'lib/pact/consumer/server.rb', line 37 def port @port end |
Class Method Details
.ports ⇒ Object
32 33 34 |
# File 'lib/pact/consumer/server.rb', line 32 def ports @ports ||= {} end |
Instance Method Details
#boot ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/pact/consumer/server.rb', line 98 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 |
#error ⇒ Object
51 52 53 |
# File 'lib/pact/consumer/server.rb', line 51 def error @middleware.error end |
#get_identity ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'lib/pact/consumer/server.rb', line 76 def get_identity http = Net::HTTP.new host, @port if [:ssl] http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE end http.get('/__identify__') end |
#host ⇒ Object
55 56 57 |
# File 'lib/pact/consumer/server.rb', line 55 def host "localhost" end |
#reset_error! ⇒ Object
47 48 49 |
# File 'lib/pact/consumer/server.rb', line 47 def reset_error! @middleware.error = nil end |
#responsive? ⇒ Boolean
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pact/consumer/server.rb', line 59 def responsive? return false if @server_thread && @server_thread.join(0) res = get_identity 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 rescue EOFError return false end |
#run_default_server(app, port) ⇒ Object
71 72 73 74 |
# File 'lib/pact/consumer/server.rb', line 71 def run_default_server(app, port) require 'rack/handler/webrick' Rack::Handler::WEBrick.run(app, webrick_opts) end |
#ssl_opts ⇒ Object
91 92 93 94 95 96 |
# File 'lib/pact/consumer/server.rb', line 91 def ssl_opts { :SSLEnable => true, :SSLCertName => [ %w[CN localhost] ] } end |
#webrick_opts ⇒ Object
85 86 87 88 89 |
# File 'lib/pact/consumer/server.rb', line 85 def webrick_opts opts = {:Port => port, :AccessLog => [], :Logger => WEBrick::Log::new(nil, 0)} opts.merge!(ssl_opts) if [:ssl] opts end |