low 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -11,6 +11,7 @@ spec/reports
11
11
  test/tmp
12
12
  test/version_tmp
13
13
  tmp
14
+ log/*
14
15
 
15
16
  # YARD artifacts
16
17
  .yardoc
@@ -0,0 +1,39 @@
1
+ module Low
2
+ module Middleware
3
+ # `RequestId` adds a value for env["request_id"]. `RequestLogger`, for one,
4
+ # uses this value to scope the logger it instantiates.
5
+ class RequestId
6
+ @@request_id = 0
7
+
8
+ VALID_REQUEST_ID_REGEX = /^[a-zA-Z0-9\s\-_]*$/
9
+
10
+ def self.is_valid?(id)
11
+ id =~ VALID_REQUEST_ID_REGEX
12
+ end
13
+
14
+ def initialize(app)
15
+ @app = app
16
+
17
+ #generate a request ID (not too worried about uniqueness here).
18
+ @current_request_id = (@@request_id += 1)
19
+ end
20
+
21
+ def call(env)
22
+ # If there is a request_id parameter,
23
+ req = Rack::Request.new(env)
24
+
25
+ # and it's valid, use it;
26
+ if req['request_id'] and RequestId.is_valid?(req['request_id'])
27
+ env['useless.request_id'] = req['request_id']
28
+
29
+ # otherwise, use the generated one
30
+ else
31
+ env['useless.request_id'] = @current_request_id.to_s
32
+ end
33
+
34
+ # and call the app.
35
+ @app.call(env)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -7,7 +7,7 @@ module Low
7
7
  # the log directory, otherwise use STDOUT (as Heroku likes).
8
8
  # * _log_level_: If set, use the LOG_LEVEL environment variable value;
9
9
  # otherwise, use INFO.
10
- # * _group_key_: Use request_id env value.
10
+ # * _group_key_: Use request_id env value (see the RequestId middleware).
11
11
  class RequestLogger
12
12
  def self.level
13
13
  # If `LOG_LEVEL` is a valid level other than INFO,
data/lib/low/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Low
2
- VERSION = '0.0.7'
2
+ VERSION = '0.0.8'
3
3
  end
data/lib/low.rb CHANGED
@@ -5,6 +5,7 @@ module Low
5
5
  autoload :Mongo, 'low/mongo'
6
6
 
7
7
  module Middleware
8
+ autoload :RequestId, 'low/middleware/request_id'
8
9
  autoload :RequestLogger, 'low/middleware/request_logger'
9
10
  autoload :SubdomainMap, 'low/middleware/subdomain_map'
10
11
  end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ describe Low::Middleware::RequestId do
4
+ def app
5
+ app = lambda do |env|
6
+ [200, {'Content-Type' => 'text/plain'}, ['Request Id: ' + env['useless.request_id']]]
7
+ end
8
+
9
+ Low::Middleware::RequestId.new(app)
10
+ end
11
+
12
+ it 'should add a request ID to the env while proxying transparently' do
13
+ res = Rack::MockRequest.new(app).get('http://useless.info')
14
+ res.should be_ok
15
+ res.body.should =~ /Request Id: [a-z0-9]{1,}/
16
+ end
17
+
18
+ it 'should use the ID specified in the query parameter' do
19
+ res = Rack::MockRequest.new(app).get('http://useless.info?request_id=jah')
20
+ res.should be_ok
21
+ res.body.should == 'Request Id: jah'
22
+ end
23
+
24
+ it 'should not use the ID specified it does not have "simple" characters' do
25
+ res = Rack::MockRequest.new(app).get('http://useless.info?request_id=(jah)')
26
+ res.should be_ok
27
+ res.body.should_not == 'Request Id: (jah)'
28
+ end
29
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: low
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -105,6 +105,7 @@ files:
105
105
  - README.md
106
106
  - Rakefile
107
107
  - lib/low.rb
108
+ - lib/low/middleware/request_id.rb
108
109
  - lib/low/middleware/request_logger.rb
109
110
  - lib/low/middleware/subdomain_map.rb
110
111
  - lib/low/mongo.rb
@@ -113,6 +114,7 @@ files:
113
114
  - lib/low/scoped_logger.rb
114
115
  - lib/low/version.rb
115
116
  - low.gemspec
117
+ - spec/low/middleware/request_id_spec.rb
116
118
  - spec/low/middleware/request_logger_spec.rb
117
119
  - spec/low/middleware/subdomain_map_spec.rb
118
120
  - spec/low/mongo/util_spec.rb
@@ -144,6 +146,7 @@ signing_key:
144
146
  specification_version: 3
145
147
  summary: A low-level utility library.
146
148
  test_files:
149
+ - spec/low/middleware/request_id_spec.rb
147
150
  - spec/low/middleware/request_logger_spec.rb
148
151
  - spec/low/middleware/subdomain_map_spec.rb
149
152
  - spec/low/mongo/util_spec.rb