request_store 1.3.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 057f28c37dcfc7b49f7443c8440af572892672ee
4
+ data.tar.gz: b5d14983640d8e012ca2c153e2b8dddaa6ace337
5
+ SHA512:
6
+ metadata.gz: cea24b9f63fa74fc0b0235d155133932fc018934b64dae3256266a320f060e2f10b004b48735926caaeacf64d28f5fcad336f3df4c5c436994a9c02ac5651994
7
+ data.tar.gz: 502ed5cb5807e7066fc3d2a0ac7d7f0d768af7f53c88f96e43c384ca57cfb9a371230f09c98692a8441c74e87c2402d68f09e48dc6aa8ba461892efc6af415c2
@@ -9,7 +9,6 @@ rvm:
9
9
  - 2.2.3
10
10
  - jruby-18mode
11
11
  - jruby-19mode
12
- - rbx-2
13
12
  - ruby-head
14
13
  - jruby-head
15
14
  - ree
data/README.md CHANGED
@@ -27,7 +27,7 @@ our controller:
27
27
  def index
28
28
  Thread.current[:counter] ||= 0
29
29
  Thread.current[:counter] += 1
30
-
30
+
31
31
  render :text => Thread.current[:counter]
32
32
  end
33
33
  ```
@@ -97,6 +97,17 @@ def app
97
97
  end
98
98
  ```
99
99
 
100
+ ## Using with Sidekiq
101
+
102
+ This gem uses a Rack middleware to clear the store object after every request,
103
+ but that doesn't translate well to background processing with
104
+ [Sidekiq](https://github.com/mperham/sidekiq).
105
+
106
+ A companion library,
107
+ [request_store-sidekiq](https://rubygems.org/gems/request_store-sidekiq)
108
+ creates a Sidekiq middleware that will ensure the store is cleared after each
109
+ job is processed, for security and consistency with how this is done in Rack.
110
+
100
111
  ## Semantic Versioning
101
112
 
102
113
  This project conforms to [semver](http://semver.org/). As a result of this
@@ -1,3 +1,12 @@
1
+ require 'rack/body_proxy'
2
+
3
+ # A middleware that ensures the RequestStore stays around until
4
+ # the last part of the body is rendered. This is useful when
5
+ # using streaming.
6
+ #
7
+ # Uses Rack::BodyProxy, adapted from Rack::Lock's usage of the
8
+ # same pattern.
9
+
1
10
  module RequestStore
2
11
  class Middleware
3
12
  def initialize(app)
@@ -6,10 +15,18 @@ module RequestStore
6
15
 
7
16
  def call(env)
8
17
  RequestStore.begin!
9
- @app.call(env)
18
+
19
+ response = @app.call(env)
20
+
21
+ returned = response << Rack::BodyProxy.new(response.pop) do
22
+ RequestStore.end!
23
+ RequestStore.clear!
24
+ end
10
25
  ensure
11
- RequestStore.end!
12
- RequestStore.clear!
26
+ unless returned
27
+ RequestStore.end!
28
+ RequestStore.clear!
29
+ end
13
30
  end
14
31
  end
15
32
  end
@@ -1,3 +1,3 @@
1
1
  module RequestStore
2
- VERSION = "1.3.2"
2
+ VERSION = "1.4.0"
3
3
  end
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ["lib"]
20
20
 
21
+ gem.add_dependency "rack", ">= 1.4"
22
+
21
23
  gem.add_development_dependency "rake", "~> 10.5"
22
24
  gem.add_development_dependency "minitest", "~> 5.0"
23
25
  end
@@ -9,8 +9,15 @@ class MiddlewareTest < Minitest::Test
9
9
  @middleware = RequestStore::Middleware.new(@app)
10
10
  end
11
11
 
12
+ def call_middleware(opts = {})
13
+ _, _, proxy = @middleware.call(opts)
14
+ proxy.close
15
+ end
16
+
12
17
  def test_middleware_resets_store
13
- 2.times { @middleware.call({}) }
18
+ 2.times do
19
+ call_middleware
20
+ end
14
21
 
15
22
  assert_equal 1, @app.last_value
16
23
  assert_equal({}, RequestStore.store)
@@ -18,7 +25,7 @@ class MiddlewareTest < Minitest::Test
18
25
 
19
26
  def test_middleware_resets_store_on_error
20
27
  e = assert_raises RuntimeError do
21
- @middleware.call({:error => true})
28
+ call_middleware({:error => true})
22
29
  end
23
30
 
24
31
  assert_equal 'FAIL', e.message
@@ -26,20 +33,33 @@ class MiddlewareTest < Minitest::Test
26
33
  end
27
34
 
28
35
  def test_middleware_begins_store
29
- @middleware.call({})
36
+ call_middleware
30
37
  assert_equal true, @app.store_active
31
38
  end
32
39
 
33
40
  def test_middleware_ends_store
34
- @middleware.call({})
41
+ call_middleware
42
+
35
43
  assert_equal false, RequestStore.active?
36
44
  end
37
45
 
38
46
  def test_middleware_ends_store_on_error
39
47
  assert_raises RuntimeError do
40
- @middleware.call({:error => true})
48
+ call_middleware({:error => true})
41
49
  end
42
50
 
43
51
  assert_equal false, RequestStore.active?
44
52
  end
53
+
54
+ def test_middleware_stores_until_proxy_closes
55
+ _, _, proxy = @middleware.call({})
56
+
57
+ assert_equal 1, @app.last_value
58
+ assert RequestStore.active?
59
+
60
+ proxy.close
61
+
62
+ refute RequestStore.active?
63
+ refute RequestStore.store[:foo]
64
+ end
45
65
  end
@@ -7,6 +7,10 @@ class RequestStoreTest < Minitest::Test
7
7
  RequestStore.clear!
8
8
  end
9
9
 
10
+ def teardown
11
+ RequestStore.clear!
12
+ end
13
+
10
14
  def test_initial_state
11
15
  Thread.current[:request_store] = nil
12
16
  assert_equal RequestStore.store, Hash.new
@@ -7,5 +7,7 @@ class RackApp
7
7
  @last_value = RequestStore.store[:foo]
8
8
  @store_active = RequestStore.active?
9
9
  raise 'FAIL' if env[:error]
10
+
11
+ [200, {}, ["response"]]
10
12
  end
11
13
  end
metadata CHANGED
@@ -1,46 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: request_store
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.2
5
- prerelease:
4
+ version: 1.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Steve Klabnik
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2017-01-05 00:00:00.000000000 Z
11
+ date: 2018-01-01 00:00:00.000000000 Z
13
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
14
27
  - !ruby/object:Gem::Dependency
15
28
  name: rake
16
29
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
30
  requirements:
19
- - - ~>
31
+ - - "~>"
20
32
  - !ruby/object:Gem::Version
21
33
  version: '10.5'
22
34
  type: :development
23
35
  prerelease: false
24
36
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
37
  requirements:
27
- - - ~>
38
+ - - "~>"
28
39
  - !ruby/object:Gem::Version
29
40
  version: '10.5'
30
41
  - !ruby/object:Gem::Dependency
31
42
  name: minitest
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ~>
45
+ - - "~>"
36
46
  - !ruby/object:Gem::Version
37
47
  version: '5.0'
38
48
  type: :development
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
- - - ~>
52
+ - - "~>"
44
53
  - !ruby/object:Gem::Version
45
54
  version: '5.0'
46
55
  description: RequestStore gives you per-request global storage.
@@ -50,8 +59,8 @@ executables: []
50
59
  extensions: []
51
60
  extra_rdoc_files: []
52
61
  files:
53
- - .gitignore
54
- - .travis.yml
62
+ - ".gitignore"
63
+ - ".travis.yml"
55
64
  - Gemfile
56
65
  - LICENSE.txt
57
66
  - README.md
@@ -67,33 +76,26 @@ files:
67
76
  homepage: http://github.com/steveklabnik/request_store
68
77
  licenses:
69
78
  - MIT
79
+ metadata: {}
70
80
  post_install_message:
71
81
  rdoc_options: []
72
82
  require_paths:
73
83
  - lib
74
84
  required_ruby_version: !ruby/object:Gem::Requirement
75
- none: false
76
85
  requirements:
77
- - - ! '>='
86
+ - - ">="
78
87
  - !ruby/object:Gem::Version
79
88
  version: '0'
80
- segments:
81
- - 0
82
- hash: 431099191289644974
83
89
  required_rubygems_version: !ruby/object:Gem::Requirement
84
- none: false
85
90
  requirements:
86
- - - ! '>='
91
+ - - ">="
87
92
  - !ruby/object:Gem::Version
88
93
  version: '0'
89
- segments:
90
- - 0
91
- hash: 431099191289644974
92
94
  requirements: []
93
95
  rubyforge_project:
94
- rubygems_version: 1.8.23
96
+ rubygems_version: 2.5.2
95
97
  signing_key:
96
- specification_version: 3
98
+ specification_version: 4
97
99
  summary: RequestStore gives you per-request global storage.
98
100
  test_files:
99
101
  - test/middleware_test.rb