rack-request-id 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: be032e96fd59cbc3453055028201c428fe7d923d
4
+ data.tar.gz: fc8581bede6f4e0d096ccc3b8818e296167266d6
5
+ SHA512:
6
+ metadata.gz: 34e1986d41b481640ccb6a8eaafdce29c27387bd5014d22738cf0776e86430c6129c339b04948e01e501a3bb8f705e6613d8f6edf15eacba9e110e3ac7f2d065
7
+ data.tar.gz: f0d77d11f819298ae756377a6100fc46870739d27131bc23804e874383bc69c95bb2eeab174bd30ce0625d0886b92c0c1c0e43669117ce5b3cbb3695f945724d
data/Gemfile CHANGED
@@ -2,3 +2,10 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rack-request-id.gemspec
4
4
  gemspec
5
+
6
+ group :test do
7
+ gem 'rack'
8
+ gem 'rack-test'
9
+ gem 'rspec'
10
+ gem 'sinatra'
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Brian Racer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -30,7 +30,23 @@ end
30
30
 
31
31
  ## Configuration
32
32
 
33
- TODO: proc option to override id format
33
+ Optionally you can override the ID generation `proc`. By default it's `SecureRandom.hex(16)`.
34
+
35
+ ```ruby
36
+ class MyApp < Sinatra::Base
37
+ use Rack::RequestId, id_generator: proc { SecureRandom.random_number(100) }
38
+ end
39
+ ```
40
+
41
+ Optionally you can also change storage. Storage objects must have `#[]` and `#[]=` method.
42
+
43
+ ```ruby
44
+ require 'request_store'
45
+
46
+ class MyApp < Sinatra::Base
47
+ use Rack::RequestId, storage: RequestStore
48
+ end
49
+ ```
34
50
 
35
51
  ## Contributing
36
52
 
@@ -39,3 +55,4 @@ TODO: proc option to override id format
39
55
  3. Commit your changes (`git commit -am 'Add some feature'`)
40
56
  4. Push to the branch (`git push origin my-new-feature`)
41
57
  5. Create new Pull Request
58
+
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  class RequestId
3
- VERSION = "0.0.3"
3
+ VERSION = "0.0.4"
4
4
  end
5
5
  end
@@ -2,14 +2,17 @@ require 'securerandom'
2
2
 
3
3
  module Rack
4
4
  class RequestId
5
- def initialize(app)
5
+ def initialize(app, opts = {})
6
6
  @app = app
7
+ @storage = opts[:storage] || proc { Thread.current }
8
+ @id_generator = opts[:id_generator] || proc { SecureRandom.hex(16) }
7
9
  end
8
10
 
9
11
  def call(env)
10
- Thread.current[:request_id] = env['HTTP_X_REQUEST_ID'] || SecureRandom.hex(16)
12
+ storage = @storage.respond_to?(:call) ? @storage.call : @storage
13
+ storage[:request_id] = env['HTTP_X_REQUEST_ID'] || @id_generator.call
11
14
  status, headers, body = @app.call(env)
12
- headers['X-Request-Id'] ||= Thread.current[:request_id]
15
+ headers['X-Request-Id'] ||= storage[:request_id]
13
16
  [status, headers, body]
14
17
  end
15
18
  end
@@ -11,6 +11,7 @@ Gem::Specification.new do |gem|
11
11
  gem.description = %q{Request ID Header}
12
12
  gem.summary = %q{Adds a 16-digit hexidecimal X-Request-Id header if none exists.}
13
13
  gem.homepage = "https://github.com/anveo/rack-request-id"
14
+ gem.license = 'MIT'
14
15
 
15
16
  gem.files = `git ls-files`.split($/)
16
17
  gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ module SinatraSpec
4
+
5
+ class TestApp < Sinatra::Base
6
+ use Rack::RequestId
7
+ get('/') { status(200) }
8
+ end
9
+
10
+ class TestAppWithCustomId < Sinatra::Base
11
+ use Rack::RequestId, id_generator: proc { "foobar" }
12
+ get('/') { status(200) }
13
+ end
14
+
15
+ class TestAppWithMyStorage < Sinatra::Base
16
+ use Rack::RequestId, storage: MyStorage
17
+ get('/') { status(200) }
18
+ end
19
+
20
+ describe Rack::RequestId do
21
+ include Rack::Test::Methods
22
+
23
+ context 'default storage' do
24
+ let(:app) { TestApp.new }
25
+
26
+ it 'adds an X-Request-Id header to the response' do
27
+ get '/'
28
+ expect(last_response["X-Request-Id"]).to_not be_nil
29
+ end
30
+ end
31
+
32
+ context 'with other storage' do
33
+ let(:app) { TestAppWithMyStorage.new }
34
+
35
+ it 'adds an X-Request-Id header to the response' do
36
+ get '/'
37
+ expect(last_response["X-Request-Id"]).to_not be_nil
38
+ end
39
+ end
40
+
41
+ context 'with custom id generator' do
42
+ let(:app) { TestAppWithCustomId.new }
43
+
44
+ it 'matches the custom id' do
45
+ get '/'
46
+ expect(last_response["X-Request-Id"]).to eq("foobar")
47
+ end
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'rspec'
3
+ require 'rack'
4
+ require 'rack/test'
5
+ require 'sinatra/base'
6
+
7
+ $:.push(File.expand_path(File.dirname(__FILE__)))
8
+ $:.push(File.expand_path(File.dirname(__FILE__)) + '/../lib')
9
+
10
+ require 'rack-request-id'
11
+
12
+ class MyStorage
13
+ class << self
14
+ def [](k)
15
+ storage[k]
16
+ end
17
+
18
+ def []=(k, v)
19
+ storage[k] =v
20
+ end
21
+
22
+ private
23
+
24
+ def storage
25
+ Thread.current[:my_storage] ||= {}
26
+ end
27
+ end
28
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-request-id
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.3
4
+ version: 0.0.4
6
5
  platform: ruby
7
6
  authors:
8
7
  - Brian Racer
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-07-19 00:00:00.000000000 Z
11
+ date: 2016-03-24 00:00:00.000000000 Z
13
12
  dependencies: []
14
13
  description: Request ID Header
15
14
  email:
@@ -18,37 +17,41 @@ executables: []
18
17
  extensions: []
19
18
  extra_rdoc_files: []
20
19
  files:
21
- - .gitignore
20
+ - ".gitignore"
22
21
  - Gemfile
23
- - LICENSE.txt
22
+ - LICENSE
24
23
  - README.md
25
24
  - Rakefile
26
25
  - lib/rack-request-id.rb
27
26
  - lib/rack-request-id/version.rb
28
27
  - lib/rack/request_id.rb
29
28
  - rack-request-id.gemspec
29
+ - spec/integration/sinatra_spec.rb
30
+ - spec/spec_helper.rb
30
31
  homepage: https://github.com/anveo/rack-request-id
31
- licenses: []
32
+ licenses:
33
+ - MIT
34
+ metadata: {}
32
35
  post_install_message:
33
36
  rdoc_options: []
34
37
  require_paths:
35
38
  - lib
36
39
  required_ruby_version: !ruby/object:Gem::Requirement
37
- none: false
38
40
  requirements:
39
- - - ! '>='
41
+ - - ">="
40
42
  - !ruby/object:Gem::Version
41
43
  version: '0'
42
44
  required_rubygems_version: !ruby/object:Gem::Requirement
43
- none: false
44
45
  requirements:
45
- - - ! '>='
46
+ - - ">="
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  requirements: []
49
50
  rubyforge_project:
50
- rubygems_version: 1.8.23
51
+ rubygems_version: 2.5.1
51
52
  signing_key:
52
- specification_version: 3
53
+ specification_version: 4
53
54
  summary: Adds a 16-digit hexidecimal X-Request-Id header if none exists.
54
- test_files: []
55
+ test_files:
56
+ - spec/integration/sinatra_spec.rb
57
+ - spec/spec_helper.rb
@@ -1,22 +0,0 @@
1
- Copyright (c) 2013 Brian Racer
2
-
3
- MIT License
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining
6
- a copy of this software and associated documentation files (the
7
- "Software"), to deal in the Software without restriction, including
8
- without limitation the rights to use, copy, modify, merge, publish,
9
- distribute, sublicense, and/or sell copies of the Software, and to
10
- permit persons to whom the Software is furnished to do so, subject to
11
- the following conditions:
12
-
13
- The above copyright notice and this permission notice shall be
14
- included in all copies or substantial portions of the Software.
15
-
16
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.