Class: Calliper::Server
- Inherits:
-
Object
- Object
- Calliper::Server
- Defined in:
- lib/calliper/server.rb
Overview
NOTE: Borrows code from the fantastic Teaspoon gem:
http://github.com/modeset/teaspoon
Instance Attribute Summary collapse
-
#application ⇒ Object
readonly
Returns the value of attribute application.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
-
#initialize(application, options = {}) ⇒ Server
constructor
A new instance of Server.
- #rack_options ⇒ Object
- #responsive? ⇒ Boolean
- #start ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(application, options = {}) ⇒ Server
Returns a new instance of Server.
10 11 12 13 14 |
# File 'lib/calliper/server.rb', line 10 def initialize(application, = {}) @application = application @port = [:port] || Config.port || find_available_port @logger = [:logger] end |
Instance Attribute Details
#application ⇒ Object (readonly)
Returns the value of attribute application.
8 9 10 |
# File 'lib/calliper/server.rb', line 8 def application @application end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
8 9 10 |
# File 'lib/calliper/server.rb', line 8 def logger @logger end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
8 9 10 |
# File 'lib/calliper/server.rb', line 8 def port @port end |
Instance Method Details
#rack_options ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/calliper/server.rb', line 39 def { app: application, environment: 'test', Port: port, AccessLog: [], Logger: logger } end |
#responsive? ⇒ Boolean
31 32 33 34 35 36 37 |
# File 'lib/calliper/server.rb', line 31 def responsive? return false if @thread && @thread.join(0) TCPSocket.new('127.0.0.1', port).close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end |
#start ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/calliper/server.rb', line 16 def start @thread = Thread.new do server = Rack::Server.new() server.start end Timeout.timeout(60) do @thread.join(0.1) until responsive? end rescue Timeout::Error raise "Server failed to start within 60 seconds." rescue => e raise "Cannot start server: #{e.message}" end |
#url ⇒ Object
49 50 51 |
# File 'lib/calliper/server.rb', line 49 def url "http://127.0.0.1:#{port}" end |