hi 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 90b9c09d4718077fca4fc2f656da9becf7f54517
4
- data.tar.gz: 4053b75715a16a474269a6ed608dd679794e03e1
3
+ metadata.gz: d62905c572b1eec0a16b7e2eae36f284231c89bd
4
+ data.tar.gz: fad5a24a9be7b66e9be3046388454baa662afdfe
5
5
  SHA512:
6
- metadata.gz: ecb8367a1625838354d137a540def00a06e699f048ff98fc1f4f610525152b8cfabb37a697a4970a1b937cf363feb39beec14ee59263ec0dd7e1c7c699178961
7
- data.tar.gz: d1a587364e141c112de9daa65d98cb763b9c83252ec3b2cf785e0d680430bcdafcd3d1193adf9b039a782fd2df2a74ad8064a4298cfbae218aa03759c05d16f0
6
+ metadata.gz: aae46842f2606ab3374933b9e265dd2649bef5cb09315549001b943df3f87c4d6c39199c06b4c5f5798af39586d05069ffd2b43ddc013548c27b57cb3d34d6a0
7
+ data.tar.gz: fb7a173ed1ebb24171829acd469d32a2606cb263425c4a7912733bc295b33469e2d3940c4e3535c780a0e2ab82c2d0f367875543daa9b5defdc993f44f262e64
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ##v1.2.0
4
+ *2013-12-29*
5
+
6
+ - [#5](https://github.com/chrishunt/hi/pull/5) Use random port if port already in use
7
+
3
8
  ##v1.1.1
4
9
  *2013-12-28*
5
10
 
data/bin/hi CHANGED
@@ -1,7 +1,4 @@
1
1
  #!/usr/bin/env ruby
2
- require 'thin'
3
2
  require 'hi'
4
3
 
5
- app = Hi::Server.new(ARGV.pop)
6
-
7
- Thin::Server.start '0.0.0.0', app.port, app
4
+ Hi::Server.new(Hi::App.new(ARGV.pop)).start
data/lib/hi.rb CHANGED
@@ -1,3 +1,4 @@
1
1
  require 'hi/version'
2
2
  require 'hi/request'
3
+ require 'hi/app'
3
4
  require 'hi/server'
@@ -0,0 +1,29 @@
1
+ require 'awesome_print'
2
+ require 'hi/request'
3
+
4
+ module Hi
5
+ class App
6
+ attr_reader :port
7
+
8
+ DEFAULT_PORT = 3000
9
+
10
+ def initialize(port = nil)
11
+ @port = (port = port.to_i) > 0 ? port : DEFAULT_PORT
12
+ end
13
+
14
+ def call(env)
15
+ log Hi::Request.new(env).to_h
16
+
17
+ [ 200, { 'Content-Type' => 'text/plain' }, ['hi'] ]
18
+ end
19
+
20
+ private
21
+
22
+ def log(request)
23
+ unless ENV['RACK_ENV'] == 'test'
24
+ ap "#{request[:request_method]} #{request[:url]} (#{Time.now})"
25
+ ap request
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,4 @@
1
+ require 'forwardable'
1
2
  require 'rack/request'
2
3
 
3
4
  module Hi
@@ -1,27 +1,41 @@
1
- require 'awesome_print'
2
- require 'hi/request'
1
+ require 'thin'
3
2
 
4
3
  module Hi
5
4
  class Server
6
- attr_reader :port
5
+ attr_reader :app
7
6
 
8
- def initialize(port = nil)
9
- @port = (port = port.to_i) > 0 ? port : 3000
7
+ CantStartServerError = Class.new(RuntimeError)
8
+
9
+ MAX_ATTEMPTS = 5
10
+
11
+ def initialize(app)
12
+ @app = app
10
13
  end
11
14
 
12
- def call(env)
13
- log Hi::Request.new(env).to_h
15
+ def start(port = app.port, attempts = 1)
16
+ log "Starting server on port #{port}...\n\n"
17
+ start! port
18
+ rescue RuntimeError => e
19
+ if attempts < MAX_ATTEMPTS
20
+ log "\nUnable to start server, trying random port instead."
21
+ start random_port, attempts + 1
22
+ else
23
+ raise CantStartServerError.new(e)
24
+ end
25
+ end
14
26
 
15
- [ 200, { 'Content-Type' => 'text/plain' }, ['hi'] ]
27
+ def start!(port)
28
+ Thin::Server.start '0.0.0.0', port, app
16
29
  end
17
30
 
18
31
  private
19
32
 
20
- def log(request)
21
- unless ENV['RACK_ENV'] == 'test'
22
- ap "#{request[:request_method]} #{request[:url]} (#{Time.now})"
23
- ap request
24
- end
33
+ def random_port
34
+ 1000 + Random.rand(9000)
35
+ end
36
+
37
+ def log(message)
38
+ puts message unless ENV['RACK_ENV'] == 'test'
25
39
  end
26
40
  end
27
41
  end
@@ -1,3 +1,3 @@
1
1
  module Hi
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+ require 'hi/app'
3
+ require 'rack/test'
4
+
5
+ describe Hi::App do
6
+ include Rack::Test::Methods
7
+
8
+ def app
9
+ described_class.new
10
+ end
11
+
12
+ [:get, :put, :post].each do |method|
13
+ it "responds to #{method.to_s.upcase}" do
14
+ send(method, '/')
15
+
16
+ expect(last_response).to be_ok
17
+ expect(last_response.body).to eq('hi')
18
+ end
19
+ end
20
+
21
+ it 'responds to any route' do
22
+ get '/some/random/route'
23
+
24
+ expect(last_response).to be_ok
25
+ expect(last_response.body).to eq('hi')
26
+ end
27
+
28
+ it 'has a default port' do
29
+ expect(app.port).to eq described_class::DEFAULT_PORT
30
+ end
31
+
32
+ it 'allows a customized port' do
33
+ expect(described_class.new(1234).port).to eq(1234)
34
+ expect(described_class.new('1234').port).to eq(1234)
35
+ end
36
+
37
+ it 'uses default port when customized port is invalid' do
38
+ expect(described_class.new(nil).port).to eq described_class::DEFAULT_PORT
39
+ expect(described_class.new('nope').port).to eq described_class::DEFAULT_PORT
40
+ expect(described_class.new(-100).port).to eq described_class::DEFAULT_PORT
41
+ end
42
+ end
@@ -1,44 +1,27 @@
1
1
  require 'spec_helper'
2
+ require 'hi/app'
2
3
  require 'hi/server'
3
- require 'rack/test'
4
-
5
- ENV['RACK_ENV'] = 'test'
6
4
 
7
5
  describe Hi::Server do
8
- include Rack::Test::Methods
9
-
10
- def app
11
- described_class.new
12
- end
6
+ let(:app) { Hi::App.new }
7
+ let(:server) { described_class.new(app) }
13
8
 
14
- [:get, :put, :post].each do |method|
15
- it "responds to #{method.to_s.upcase}" do
16
- send(method, '/')
9
+ describe '#start' do
10
+ it 'starts up the app' do
11
+ server.should_receive(:start!).with app.port
17
12
 
18
- expect(last_response).to be_ok
19
- expect(last_response.body).to eq('hi')
13
+ server.start
20
14
  end
21
- end
22
-
23
- it 'responds to any route' do
24
- get '/some/random/route'
25
15
 
26
- expect(last_response).to be_ok
27
- expect(last_response.body).to eq('hi')
28
- end
16
+ it 'tries to start again on a different port requested port is in use' do
17
+ server
18
+ .should_receive(:start!)
19
+ .exactly(Hi::Server::MAX_ATTEMPTS).times
20
+ .and_raise RuntimeError
29
21
 
30
- it 'defaults to port 3000' do
31
- expect(app.port).to eq(3000)
32
- end
33
-
34
- it 'allows a customized port' do
35
- expect(described_class.new(1234).port).to eq(1234)
36
- expect(described_class.new('1234').port).to eq(1234)
37
- end
38
-
39
- it 'uses default port when customized port is invalid' do
40
- expect(described_class.new(nil).port).to eq(3000)
41
- expect(described_class.new('nope').port).to eq(3000)
42
- expect(described_class.new(-100).port).to eq(3000)
22
+ expect {
23
+ server.start
24
+ }.to raise_error Hi::Server::CantStartServerError
25
+ end
43
26
  end
44
27
  end
@@ -1,2 +1,5 @@
1
+ require 'pry'
1
2
  require 'coveralls'
2
3
  Coveralls.wear!
4
+
5
+ ENV['RACK_ENV'] = 'test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hunt
@@ -127,10 +127,12 @@ files:
127
127
  - bin/hi
128
128
  - hi.gemspec
129
129
  - lib/hi.rb
130
+ - lib/hi/app.rb
130
131
  - lib/hi/request.rb
131
132
  - lib/hi/server.rb
132
133
  - lib/hi/version.rb
133
134
  - screenshot.png
135
+ - spec/hi/app_spec.rb
134
136
  - spec/hi/request_spec.rb
135
137
  - spec/hi/server_spec.rb
136
138
  - spec/spec_helper.rb
@@ -159,6 +161,7 @@ signing_key:
159
161
  specification_version: 4
160
162
  summary: hi, I'm here to debug your HTTP
161
163
  test_files:
164
+ - spec/hi/app_spec.rb
162
165
  - spec/hi/request_spec.rb
163
166
  - spec/hi/server_spec.rb
164
167
  - spec/spec_helper.rb