hi 1.2.0 → 1.2.1
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/lib/hi/app.rb +10 -11
- data/lib/hi/logger.rb +23 -0
- data/lib/hi/server.rb +2 -6
- data/lib/hi/version.rb +1 -1
- data/lib/hi.rb +1 -0
- data/spec/hi/logger_spec.rb +48 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa67d1893f36e4de0d7869cd034c1a4f4c52fdf1
|
4
|
+
data.tar.gz: 52116bf9b6840792dcc49f35a3228ba19ebf9bd9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ec28a50bb9c90f4d03c4dbee1e6963f534294d6b36fa34e6a45709746e6b14b1f03b7cec5b74ce50db5d75d9ad5c881d682e091dfd96bb1b1792678f853b33f6
|
7
|
+
data.tar.gz: 64587eb574ae81a82e277c1e716dece6b6f2bf8c660c87e93d3aa4c2e9e555c13c4709380a32fe7a75f5a9d73f4687b18094c0834c48bbd4ea6f664da2ea1527
|
data/CHANGELOG.md
CHANGED
data/lib/hi/app.rb
CHANGED
@@ -1,29 +1,28 @@
|
|
1
|
-
require 'awesome_print'
|
2
1
|
require 'hi/request'
|
2
|
+
require 'hi/logger'
|
3
3
|
|
4
4
|
module Hi
|
5
5
|
class App
|
6
|
-
attr_reader :port
|
6
|
+
attr_reader :port, :logger
|
7
7
|
|
8
8
|
DEFAULT_PORT = 3000
|
9
9
|
|
10
|
-
def initialize(port = nil)
|
10
|
+
def initialize(port = nil, logger = Hi::Logger.new)
|
11
11
|
@port = (port = port.to_i) > 0 ? port : DEFAULT_PORT
|
12
|
+
@logger = logger
|
12
13
|
end
|
13
14
|
|
14
15
|
def call(env)
|
15
|
-
|
16
|
+
request = Hi::Request.new(env).to_h
|
17
|
+
|
18
|
+
log "#{request[:request_method]} #{request[:url]} (#{Time.now})"
|
19
|
+
log request
|
16
20
|
|
17
21
|
[ 200, { 'Content-Type' => 'text/plain' }, ['hi'] ]
|
18
22
|
end
|
19
23
|
|
20
|
-
|
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
|
24
|
+
def log(message)
|
25
|
+
logger.log message
|
27
26
|
end
|
28
27
|
end
|
29
28
|
end
|
data/lib/hi/logger.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'awesome_print'
|
2
|
+
|
3
|
+
module Hi
|
4
|
+
class Logger
|
5
|
+
def log(message)
|
6
|
+
print message unless testing
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def print(message)
|
12
|
+
if message.is_a? String
|
13
|
+
puts message
|
14
|
+
else
|
15
|
+
ap message
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def testing
|
20
|
+
ENV['RACK_ENV'] == 'test'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/hi/server.rb
CHANGED
@@ -13,11 +13,11 @@ module Hi
|
|
13
13
|
end
|
14
14
|
|
15
15
|
def start(port = app.port, attempts = 1)
|
16
|
-
log "Starting server on port #{port}...\n\n"
|
16
|
+
app.log "Starting server on port #{port}...\n\n"
|
17
17
|
start! port
|
18
18
|
rescue RuntimeError => e
|
19
19
|
if attempts < MAX_ATTEMPTS
|
20
|
-
log "\nUnable to start server, trying random port instead."
|
20
|
+
app.log "\nUnable to start server, trying random port instead."
|
21
21
|
start random_port, attempts + 1
|
22
22
|
else
|
23
23
|
raise CantStartServerError.new(e)
|
@@ -33,9 +33,5 @@ module Hi
|
|
33
33
|
def random_port
|
34
34
|
1000 + Random.rand(9000)
|
35
35
|
end
|
36
|
-
|
37
|
-
def log(message)
|
38
|
-
puts message unless ENV['RACK_ENV'] == 'test'
|
39
|
-
end
|
40
36
|
end
|
41
37
|
end
|
data/lib/hi/version.rb
CHANGED
data/lib/hi.rb
CHANGED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'hi/logger'
|
3
|
+
|
4
|
+
describe Hi::Logger do
|
5
|
+
let(:logger) { described_class.new }
|
6
|
+
let(:message) { 'Hello logger' }
|
7
|
+
|
8
|
+
context "when RACK_ENV is not 'test'" do
|
9
|
+
before do
|
10
|
+
ENV['RACK_ENV'] = 'the real deal'
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'prints message to standard out' do
|
14
|
+
stdout = capture_stdout do
|
15
|
+
logger.log message
|
16
|
+
end
|
17
|
+
|
18
|
+
expect(stdout).to include message
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "when RACK_ENV is 'test'" do
|
23
|
+
before do
|
24
|
+
ENV['RACK_ENV'] = 'test'
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'does not print message to standard out' do
|
28
|
+
stdout = capture_stdout do
|
29
|
+
logger.log message
|
30
|
+
end
|
31
|
+
|
32
|
+
expect(stdout).to be_empty
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def capture_stdout
|
39
|
+
captured = StringIO.new
|
40
|
+
stdout = $stdout
|
41
|
+
$stdout = captured
|
42
|
+
|
43
|
+
yield
|
44
|
+
|
45
|
+
$stdout = stdout
|
46
|
+
captured.string
|
47
|
+
end
|
48
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Hunt
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-01-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: awesome_print
|
@@ -128,11 +128,13 @@ files:
|
|
128
128
|
- hi.gemspec
|
129
129
|
- lib/hi.rb
|
130
130
|
- lib/hi/app.rb
|
131
|
+
- lib/hi/logger.rb
|
131
132
|
- lib/hi/request.rb
|
132
133
|
- lib/hi/server.rb
|
133
134
|
- lib/hi/version.rb
|
134
135
|
- screenshot.png
|
135
136
|
- spec/hi/app_spec.rb
|
137
|
+
- spec/hi/logger_spec.rb
|
136
138
|
- spec/hi/request_spec.rb
|
137
139
|
- spec/hi/server_spec.rb
|
138
140
|
- spec/spec_helper.rb
|
@@ -162,6 +164,7 @@ specification_version: 4
|
|
162
164
|
summary: hi, I'm here to debug your HTTP
|
163
165
|
test_files:
|
164
166
|
- spec/hi/app_spec.rb
|
167
|
+
- spec/hi/logger_spec.rb
|
165
168
|
- spec/hi/request_spec.rb
|
166
169
|
- spec/hi/server_spec.rb
|
167
170
|
- spec/spec_helper.rb
|