request_global 0.1.1
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.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +14 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/ext/request_global/extconf.rb +3 -0
- data/ext/request_global/request_global.c +177 -0
- data/ext/request_global/request_global.h +6 -0
- data/lib/request_global.rb +6 -0
- data/lib/request_global/middleware.rb +32 -0
- data/lib/request_global/railtie.rb +10 -0
- data/lib/request_global/version.rb +3 -0
- data/request_global.gemspec +30 -0
- data/spec/request_global_middleware_spec.rb +66 -0
- data/spec/request_global_spec.rb +107 -0
- data/spec/spec_helper.rb +25 -0
- metadata +127 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: de7d04321ffed44ba4c175ddb2e64c975dcb3e92
|
|
4
|
+
data.tar.gz: 3877e07cb3f6d84dedd02dc69ce1f0fb68b8e467
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b8d8780fce7006828a5a40b41d16cdd11ebf444e0c810656dd882c17607af4894b3bcb5e93c06ef89531d328403026431085d757c64d4d2a8e8d7a614b162c28
|
|
7
|
+
data.tar.gz: 3fb96483eea62bbe31780f8a5527cd2a9e839ce27683d95a6e2ca4a528882bbcae6599e43dedffe3f8ca498b8677f98ae5468ca1292a336899010914bc5c9b6d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
2.1.5
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Chaerim Yeo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all 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,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# RequestGlobal
|
|
2
|
+
RequestGlobal provides global storage per request for Rails. The core part of RequestGlobal is written in Ruby's C API.
|
|
3
|
+
|
|
4
|
+
## Installation
|
|
5
|
+
Add this line to your Gemfile:
|
|
6
|
+
|
|
7
|
+
```ruby
|
|
8
|
+
gem 'request_global'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install request_global
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
Each storage of request is a Hash.
|
|
21
|
+
```ruby
|
|
22
|
+
# Setter methods
|
|
23
|
+
RequestGlobal.store(:foo, 1) #=> 1
|
|
24
|
+
RequestGlobal[:bar] = 2 #=> 2
|
|
25
|
+
|
|
26
|
+
# Getter methods
|
|
27
|
+
RequestGlobal.fetch(:bar) #=> 2
|
|
28
|
+
RequestGlobal[:foo] #=> 1
|
|
29
|
+
|
|
30
|
+
# Delete
|
|
31
|
+
RequestGlobal.delete(:bar)
|
|
32
|
+
|
|
33
|
+
# Clear storage of current request
|
|
34
|
+
RequestGlobal.clear!
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
### No Rails?
|
|
38
|
+
RequestGlobal provides Railtie which configures Rack middleware. However, if you're not using Rails, use middleware yourself like following:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
use RequestGlobal::Middleware
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Contributing
|
|
45
|
+
|
|
46
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/cryeo/request_global.
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
## License
|
|
50
|
+
|
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
|
2
|
+
require "rspec/core/rake_task"
|
|
3
|
+
|
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
5
|
+
|
|
6
|
+
require "rake/extensiontask"
|
|
7
|
+
|
|
8
|
+
task :build => :compile
|
|
9
|
+
|
|
10
|
+
Rake::ExtensionTask.new("request_global") do |ext|
|
|
11
|
+
ext.lib_dir = "lib/request_global"
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
task :default => [:clobber, :compile, :spec]
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "request_global"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
#include "request_global.h"
|
|
2
|
+
|
|
3
|
+
VALUE mRequestGlobal;
|
|
4
|
+
|
|
5
|
+
VALUE
|
|
6
|
+
class_variable_storages(VALUE self) {
|
|
7
|
+
return rb_cv_get(self, "@@storages");
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
VALUE
|
|
11
|
+
class_variable_current_request(VALUE self) {
|
|
12
|
+
return rb_cv_get(self, "@@current_request");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
void
|
|
16
|
+
check_condition(VALUE self) {
|
|
17
|
+
Check_Type(class_variable_storages(self), T_HASH);
|
|
18
|
+
if (class_variable_current_request(self) == Qnil) rb_raise(rb_eException, "Could not idendify current request");
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
VALUE
|
|
22
|
+
request_global_current_storage(VALUE self) {
|
|
23
|
+
VALUE current_storage = rb_hash_aref(class_variable_storages(self), class_variable_current_request(self));
|
|
24
|
+
Check_Type(current_storage, T_HASH);
|
|
25
|
+
return current_storage;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/*
|
|
29
|
+
* call-seq:
|
|
30
|
+
* RequestGlobal.set_current_request(request) -> request
|
|
31
|
+
*
|
|
32
|
+
* Set current request id.
|
|
33
|
+
*/
|
|
34
|
+
static VALUE
|
|
35
|
+
request_global_set_current_request(VALUE self, VALUE request) {
|
|
36
|
+
rb_cv_set(self, "@@current_request", request);
|
|
37
|
+
return class_variable_current_request(self);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/*
|
|
41
|
+
* call-seq:
|
|
42
|
+
* RequestGlobal.get_current_request -> request
|
|
43
|
+
*
|
|
44
|
+
* Get current request id.
|
|
45
|
+
*/
|
|
46
|
+
static VALUE
|
|
47
|
+
request_global_get_current_request(VALUE self) {
|
|
48
|
+
return class_variable_current_request(self);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/*
|
|
52
|
+
* call-seq:
|
|
53
|
+
* RequestGlobal.begin! -> nil
|
|
54
|
+
*
|
|
55
|
+
* Set up storage for current request.
|
|
56
|
+
*/
|
|
57
|
+
static VALUE
|
|
58
|
+
request_global_begin(VALUE self) {
|
|
59
|
+
check_condition(self);
|
|
60
|
+
Check_Type(rb_hash_aref(class_variable_storages(self), class_variable_current_request(self)), T_NIL);
|
|
61
|
+
rb_hash_aset(class_variable_storages(self), class_variable_current_request(self), rb_hash_new());
|
|
62
|
+
return Qnil;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/*
|
|
66
|
+
* call-seq:
|
|
67
|
+
* RequestGlobal.clear! -> nil
|
|
68
|
+
*
|
|
69
|
+
* Removes all key-value pairs from storage for current request.
|
|
70
|
+
*/
|
|
71
|
+
static VALUE
|
|
72
|
+
request_global_clear(VALUE self) {
|
|
73
|
+
check_condition(self);
|
|
74
|
+
rb_hash_clear(request_global_current_storage(self));
|
|
75
|
+
return Qnil;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/*
|
|
79
|
+
* call-seq:
|
|
80
|
+
* RequestGlobal.clear_all! -> nil
|
|
81
|
+
*
|
|
82
|
+
* Removes all storages from container.
|
|
83
|
+
*/
|
|
84
|
+
static VALUE
|
|
85
|
+
request_global_clear_all(VALUE self) {
|
|
86
|
+
rb_hash_clear(class_variable_storages(self));
|
|
87
|
+
request_global_set_current_request(self, Qnil);
|
|
88
|
+
return Qnil;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/*
|
|
92
|
+
* call-seq:
|
|
93
|
+
* RequestGlobal.end! -> nil
|
|
94
|
+
*
|
|
95
|
+
* Destroy information of current request and storage for current request.
|
|
96
|
+
*/
|
|
97
|
+
static VALUE
|
|
98
|
+
request_global_end(VALUE self) {
|
|
99
|
+
check_condition(self);
|
|
100
|
+
rb_hash_delete(class_variable_storages(self), class_variable_current_request(self));
|
|
101
|
+
request_global_set_current_request(self, Qnil);
|
|
102
|
+
return Qnil;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/*
|
|
106
|
+
* call-seq:
|
|
107
|
+
* RequestGlobal.storage -> hash
|
|
108
|
+
*
|
|
109
|
+
* Get storage for current request.
|
|
110
|
+
*/
|
|
111
|
+
static VALUE
|
|
112
|
+
request_global_storage(VALUE self) {
|
|
113
|
+
check_condition(self);
|
|
114
|
+
return request_global_current_storage(self);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* call-seq:
|
|
119
|
+
* RequestGlobal.delete(key) -> value
|
|
120
|
+
*
|
|
121
|
+
* Delete the key-value pair for the given key and return the value from storage for current request.
|
|
122
|
+
*/
|
|
123
|
+
static VALUE
|
|
124
|
+
request_global_delete(VALUE self, VALUE key) {
|
|
125
|
+
check_condition(self);
|
|
126
|
+
return rb_hash_delete(request_global_current_storage(self), key);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/*
|
|
130
|
+
* call-seq:
|
|
131
|
+
* RequestGlobal.fetch(key) -> value
|
|
132
|
+
* RequestGlobal[key] -> value
|
|
133
|
+
*
|
|
134
|
+
* Return the value for the given key from storage for current request.
|
|
135
|
+
*/
|
|
136
|
+
static VALUE
|
|
137
|
+
request_global_fetch(VALUE self, VALUE key) {
|
|
138
|
+
check_condition(self);
|
|
139
|
+
return rb_hash_aref(request_global_current_storage(self), key);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/*
|
|
143
|
+
* call-seq:
|
|
144
|
+
* RequestGlobal.store(key, value) -> value
|
|
145
|
+
* RequestGlobal[key] = value -> value
|
|
146
|
+
*
|
|
147
|
+
* Assign the value with the key to storage for current request.
|
|
148
|
+
*/
|
|
149
|
+
static VALUE
|
|
150
|
+
request_global_store(VALUE self, VALUE key, VALUE value) {
|
|
151
|
+
check_condition(self);
|
|
152
|
+
return rb_hash_aset(request_global_current_storage(self), key, value);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
void
|
|
156
|
+
Init_request_global(void) {
|
|
157
|
+
mRequestGlobal = rb_define_module("RequestGlobal");
|
|
158
|
+
|
|
159
|
+
rb_cv_set(mRequestGlobal, "@@storages", rb_hash_new());
|
|
160
|
+
rb_cv_set(mRequestGlobal, "@@current_request", Qnil);
|
|
161
|
+
|
|
162
|
+
rb_define_module_function(mRequestGlobal, "begin!", request_global_begin, 0);
|
|
163
|
+
rb_define_module_function(mRequestGlobal, "end!", request_global_end, 0);
|
|
164
|
+
rb_define_module_function(mRequestGlobal, "clear!", request_global_clear, 0);
|
|
165
|
+
rb_define_module_function(mRequestGlobal, "clear_all!", request_global_clear_all, 0);
|
|
166
|
+
|
|
167
|
+
rb_define_module_function(mRequestGlobal, "get_current_request", request_global_get_current_request, 0);
|
|
168
|
+
rb_define_module_function(mRequestGlobal, "set_current_request", request_global_set_current_request, 1);
|
|
169
|
+
|
|
170
|
+
rb_define_module_function(mRequestGlobal, "storage", request_global_storage, 0);
|
|
171
|
+
|
|
172
|
+
rb_define_module_function(mRequestGlobal, "delete", request_global_delete, 1);
|
|
173
|
+
rb_define_module_function(mRequestGlobal, "fetch", request_global_fetch, 1);
|
|
174
|
+
rb_define_module_function(mRequestGlobal, "[]", request_global_fetch, 1);
|
|
175
|
+
rb_define_module_function(mRequestGlobal, "store", request_global_store, 2);
|
|
176
|
+
rb_define_module_function(mRequestGlobal, "[]=", request_global_store, 2);
|
|
177
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'securerandom'
|
|
2
|
+
|
|
3
|
+
module RequestGlobal
|
|
4
|
+
class Middleware
|
|
5
|
+
def initialize(app)
|
|
6
|
+
@app = app
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(env)
|
|
10
|
+
result = nil
|
|
11
|
+
begin
|
|
12
|
+
RequestGlobal.set_current_request current_request_id(env)
|
|
13
|
+
RequestGlobal.begin!
|
|
14
|
+
result = @app.call(env)
|
|
15
|
+
ensure
|
|
16
|
+
RequestGlobal.end!
|
|
17
|
+
end
|
|
18
|
+
result
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
protected
|
|
22
|
+
def current_request_id(env)
|
|
23
|
+
if env["action_dispatch.request_id"]
|
|
24
|
+
env["action_dispatch.request_id"]
|
|
25
|
+
elsif request_id = env["HTTP_X_REQUEST_ID"]
|
|
26
|
+
request_id.gsub(/[^\w\-]/, "")[0, 255]
|
|
27
|
+
else
|
|
28
|
+
SecureRandom.uuid
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module RequestGlobal
|
|
2
|
+
class Railtie < ::Rails::Railtie
|
|
3
|
+
initializer "request_global.insert_middleware" do |app|
|
|
4
|
+
app.config.middleware.insert_after ActionDispatch::RequestId, RequestGlobal::Middleware
|
|
5
|
+
ActionDispatch::Reloader.to_cleanup do
|
|
6
|
+
RequestGlobal.clear_all!
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'request_global/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "request_global"
|
|
8
|
+
spec.version = RequestGlobal::VERSION
|
|
9
|
+
spec.authors = ["Chaerim Yeo"]
|
|
10
|
+
spec.email = ["yeochaerim@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = "Global storage per request for Rails"
|
|
13
|
+
spec.description = "RequestGlobal provides global storage per request for Rails"
|
|
14
|
+
spec.homepage = "http://github.com/cryeo/request_global"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0")
|
|
18
|
+
spec.bindir = "bin"
|
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
20
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
21
|
+
spec.require_paths = ["lib"]
|
|
22
|
+
spec.extensions = ["ext/request_global/extconf.rb"]
|
|
23
|
+
|
|
24
|
+
spec.required_ruby_version = '>= 2.1.5'
|
|
25
|
+
|
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
28
|
+
spec.add_development_dependency "rake-compiler", "~> 0"
|
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
30
|
+
end
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe RequestGlobal::Middleware do
|
|
4
|
+
let(:app) { FakeApp.new }
|
|
5
|
+
let(:middleware) { RequestGlobal::Middleware.new(app) }
|
|
6
|
+
let(:env) { Hash.new }
|
|
7
|
+
|
|
8
|
+
def current_request_id(env)
|
|
9
|
+
middleware.send(:current_request_id, env)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "resets all storages after each request" do
|
|
13
|
+
allow(middleware).to receive(:current_request_id).and_return(test_request_id)
|
|
14
|
+
2.times { middleware.call(env) }
|
|
15
|
+
|
|
16
|
+
expect(app.last_count).to eq 1
|
|
17
|
+
expect(storages).to eq Hash.new
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "resets all storages when error raised" do
|
|
21
|
+
allow(middleware).to receive(:current_request_id).and_return(test_request_id)
|
|
22
|
+
error = nil
|
|
23
|
+
begin
|
|
24
|
+
env[:error] = true
|
|
25
|
+
middleware.call(env)
|
|
26
|
+
rescue Exception => e
|
|
27
|
+
error = e
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
expect(error.message).to eq "fail"
|
|
31
|
+
expect(storages).to eq Hash.new
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
describe "#current_request_id" do
|
|
35
|
+
let!(:action_dispatch_request_id) { "action_dispatch.request_id" }
|
|
36
|
+
let!(:http_x_request_id) { "HTTP_X_REQUEST_ID" }
|
|
37
|
+
|
|
38
|
+
context "when env['action_dispatch.request_id'] and env['HTTP_X_REQUEST_ID'] both appear" do
|
|
39
|
+
it "returns env['action_dispatch.request_id']" do
|
|
40
|
+
env["action_dispatch.request_id"] = action_dispatch_request_id
|
|
41
|
+
env["HTTP_X_REQUEST_ID"] = http_x_request_id
|
|
42
|
+
expect(current_request_id(env)).to eq action_dispatch_request_id
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
context "when only env['action_dispatch.request_id'] appears" do
|
|
47
|
+
it "returns env['action_dispatch.request_id']" do
|
|
48
|
+
env["action_dispatch.request_id"] = action_dispatch_request_id
|
|
49
|
+
expect(current_request_id(env)).to eq action_dispatch_request_id
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
context "when only env['HTTP_X_REQUEST_ID'] appears" do
|
|
54
|
+
it "returns env['HTTP_X_REQUEST_ID']" do
|
|
55
|
+
env["HTTP_X_REQUEST_ID"] = http_x_request_id
|
|
56
|
+
expect(current_request_id(env)).to eq http_x_request_id.gsub(/[^\w\-]/, "")[0, 255]
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
context "when env['action_dispatch.request_id'] and env['HTTP_X_REQUEST_ID'] both don't appear" do
|
|
61
|
+
it "returns randomly generated uuid" do
|
|
62
|
+
expect(current_request_id(env)).to match /[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe RequestGlobal do
|
|
4
|
+
before do
|
|
5
|
+
RequestGlobal.clear_all!
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it 'has a version number' do
|
|
9
|
+
expect(RequestGlobal::VERSION).not_to be_nil
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe ".set_current_request" do
|
|
13
|
+
it "set request id to class variable" do
|
|
14
|
+
RequestGlobal.set_current_request test_request_id
|
|
15
|
+
expect(current_request).to eq test_request_id
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe ".get_current_request" do
|
|
20
|
+
it "returns request id from class variable" do
|
|
21
|
+
RequestGlobal.set_current_request test_request_id
|
|
22
|
+
expect(RequestGlobal.get_current_request).to eq current_request
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe ".begin!" do
|
|
27
|
+
it "creates storage for current request" do
|
|
28
|
+
RequestGlobal.set_current_request test_request_id
|
|
29
|
+
RequestGlobal.begin!
|
|
30
|
+
expect(storages).to include test_request_id
|
|
31
|
+
RequestGlobal.end!
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
describe ".end!" do
|
|
36
|
+
it "destroy storage for current request" do
|
|
37
|
+
RequestGlobal.set_current_request test_request_id
|
|
38
|
+
RequestGlobal.begin!
|
|
39
|
+
RequestGlobal.end!
|
|
40
|
+
expect(storages).not_to include test_request_id
|
|
41
|
+
expect(current_request).to be_nil
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
describe ".clear!" do
|
|
46
|
+
it "clears only storage for current request" do
|
|
47
|
+
RequestGlobal.set_current_request test_request_id
|
|
48
|
+
RequestGlobal.begin!
|
|
49
|
+
RequestGlobal.storage[:foo] = 1
|
|
50
|
+
RequestGlobal.storage[:bar] = 2
|
|
51
|
+
expect(RequestGlobal.storage).to include foo: 1, bar: 2
|
|
52
|
+
RequestGlobal.clear!
|
|
53
|
+
expect(RequestGlobal.storage).not_to include test_request_id
|
|
54
|
+
RequestGlobal.end!
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
describe ".clear_all!" do
|
|
59
|
+
it "clears all storages and current request" do
|
|
60
|
+
RequestGlobal.set_current_request test_request_id
|
|
61
|
+
RequestGlobal.begin!
|
|
62
|
+
RequestGlobal.storage[:foo] = 1
|
|
63
|
+
RequestGlobal.storage[:bar] = 2
|
|
64
|
+
expect(RequestGlobal.storage).to include foo: 1, bar: 2
|
|
65
|
+
RequestGlobal.clear_all!
|
|
66
|
+
expect(storages).not_to include test_request_id
|
|
67
|
+
expect(current_request).to be_nil
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
describe ".delete" do
|
|
72
|
+
it "remove given key-value pairs from current storage" do
|
|
73
|
+
RequestGlobal.set_current_request test_request_id
|
|
74
|
+
RequestGlobal.begin!
|
|
75
|
+
RequestGlobal.storage[:foo] = 1
|
|
76
|
+
RequestGlobal.storage[:bar] = 2
|
|
77
|
+
RequestGlobal.delete(:foo)
|
|
78
|
+
expect(RequestGlobal.storage).not_to include foo: 1
|
|
79
|
+
RequestGlobal.end!
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
describe ".fetch" do
|
|
84
|
+
it "returns value for given key from current storage" do
|
|
85
|
+
RequestGlobal.set_current_request test_request_id
|
|
86
|
+
RequestGlobal.begin!
|
|
87
|
+
RequestGlobal.storage[:foo] = 1
|
|
88
|
+
RequestGlobal.storage[:bar] = 2
|
|
89
|
+
expect(RequestGlobal.fetch(:foo)).to eq 1
|
|
90
|
+
expect(RequestGlobal[:bar]).to eq 2
|
|
91
|
+
expect(RequestGlobal[:hoge]).to be_nil
|
|
92
|
+
RequestGlobal.end!
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
describe ".storage" do
|
|
97
|
+
it "stores key-value pairs" do
|
|
98
|
+
RequestGlobal.set_current_request test_request_id
|
|
99
|
+
RequestGlobal.begin!
|
|
100
|
+
RequestGlobal.storage[:foo] = 1
|
|
101
|
+
RequestGlobal[:bar] = 2
|
|
102
|
+
expect(RequestGlobal.fetch(:foo)).to eq 1
|
|
103
|
+
expect(RequestGlobal[:bar]).to eq 2
|
|
104
|
+
RequestGlobal.end!
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
|
2
|
+
require 'request_global'
|
|
3
|
+
|
|
4
|
+
def storages
|
|
5
|
+
RequestGlobal.class_variable_get('@@storages')
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def current_request
|
|
9
|
+
RequestGlobal.class_variable_get('@@current_request')
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_request_id
|
|
13
|
+
"test_request_id"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class FakeApp
|
|
17
|
+
attr_reader :last_count
|
|
18
|
+
|
|
19
|
+
def call(env)
|
|
20
|
+
RequestGlobal.storage[:count] ||= 0
|
|
21
|
+
RequestGlobal.storage[:count] += 1
|
|
22
|
+
@last_count = RequestGlobal.storage[:count]
|
|
23
|
+
raise "fail" if env[:error]
|
|
24
|
+
end
|
|
25
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: request_global
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Chaerim Yeo
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-04-06 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.11'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.11'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rake-compiler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '3.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '3.0'
|
|
69
|
+
description: RequestGlobal provides global storage per request for Rails
|
|
70
|
+
email:
|
|
71
|
+
- yeochaerim@gmail.com
|
|
72
|
+
executables:
|
|
73
|
+
- console
|
|
74
|
+
- setup
|
|
75
|
+
extensions:
|
|
76
|
+
- ext/request_global/extconf.rb
|
|
77
|
+
extra_rdoc_files: []
|
|
78
|
+
files:
|
|
79
|
+
- ".gitignore"
|
|
80
|
+
- ".rspec"
|
|
81
|
+
- ".ruby-version"
|
|
82
|
+
- ".travis.yml"
|
|
83
|
+
- Gemfile
|
|
84
|
+
- LICENSE.txt
|
|
85
|
+
- README.md
|
|
86
|
+
- Rakefile
|
|
87
|
+
- bin/console
|
|
88
|
+
- bin/setup
|
|
89
|
+
- ext/request_global/extconf.rb
|
|
90
|
+
- ext/request_global/request_global.c
|
|
91
|
+
- ext/request_global/request_global.h
|
|
92
|
+
- lib/request_global.rb
|
|
93
|
+
- lib/request_global/middleware.rb
|
|
94
|
+
- lib/request_global/railtie.rb
|
|
95
|
+
- lib/request_global/version.rb
|
|
96
|
+
- request_global.gemspec
|
|
97
|
+
- spec/request_global_middleware_spec.rb
|
|
98
|
+
- spec/request_global_spec.rb
|
|
99
|
+
- spec/spec_helper.rb
|
|
100
|
+
homepage: http://github.com/cryeo/request_global
|
|
101
|
+
licenses:
|
|
102
|
+
- MIT
|
|
103
|
+
metadata: {}
|
|
104
|
+
post_install_message:
|
|
105
|
+
rdoc_options: []
|
|
106
|
+
require_paths:
|
|
107
|
+
- lib
|
|
108
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
109
|
+
requirements:
|
|
110
|
+
- - ">="
|
|
111
|
+
- !ruby/object:Gem::Version
|
|
112
|
+
version: 2.1.5
|
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
114
|
+
requirements:
|
|
115
|
+
- - ">="
|
|
116
|
+
- !ruby/object:Gem::Version
|
|
117
|
+
version: '0'
|
|
118
|
+
requirements: []
|
|
119
|
+
rubyforge_project:
|
|
120
|
+
rubygems_version: 2.2.2
|
|
121
|
+
signing_key:
|
|
122
|
+
specification_version: 4
|
|
123
|
+
summary: Global storage per request for Rails
|
|
124
|
+
test_files:
|
|
125
|
+
- spec/request_global_middleware_spec.rb
|
|
126
|
+
- spec/request_global_spec.rb
|
|
127
|
+
- spec/spec_helper.rb
|