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 +4 -4
- data/CHANGELOG.md +5 -0
- data/bin/hi +1 -4
- data/lib/hi.rb +1 -0
- data/lib/hi/app.rb +29 -0
- data/lib/hi/request.rb +1 -0
- data/lib/hi/server.rb +27 -13
- data/lib/hi/version.rb +1 -1
- data/spec/hi/app_spec.rb +42 -0
- data/spec/hi/server_spec.rb +16 -33
- data/spec/spec_helper.rb +3 -0
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d62905c572b1eec0a16b7e2eae36f284231c89bd
|
4
|
+
data.tar.gz: fad5a24a9be7b66e9be3046388454baa662afdfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aae46842f2606ab3374933b9e265dd2649bef5cb09315549001b943df3f87c4d6c39199c06b4c5f5798af39586d05069ffd2b43ddc013548c27b57cb3d34d6a0
|
7
|
+
data.tar.gz: fb7a173ed1ebb24171829acd469d32a2606cb263425c4a7912733bc295b33469e2d3940c4e3535c780a0e2ab82c2d0f367875543daa9b5defdc993f44f262e64
|
data/CHANGELOG.md
CHANGED
data/bin/hi
CHANGED
data/lib/hi.rb
CHANGED
data/lib/hi/app.rb
ADDED
@@ -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
|
data/lib/hi/request.rb
CHANGED
data/lib/hi/server.rb
CHANGED
@@ -1,27 +1,41 @@
|
|
1
|
-
require '
|
2
|
-
require 'hi/request'
|
1
|
+
require 'thin'
|
3
2
|
|
4
3
|
module Hi
|
5
4
|
class Server
|
6
|
-
attr_reader :
|
5
|
+
attr_reader :app
|
7
6
|
|
8
|
-
|
9
|
-
|
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
|
13
|
-
log
|
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
|
-
|
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
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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
|
data/lib/hi/version.rb
CHANGED
data/spec/hi/app_spec.rb
ADDED
@@ -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
|
data/spec/hi/server_spec.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
15
|
-
it
|
16
|
-
|
9
|
+
describe '#start' do
|
10
|
+
it 'starts up the app' do
|
11
|
+
server.should_receive(:start!).with app.port
|
17
12
|
|
18
|
-
|
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
|
-
|
27
|
-
|
28
|
-
|
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
|
-
|
31
|
-
|
32
|
-
|
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
|
data/spec/spec_helper.rb
CHANGED
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.
|
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
|