request_context 0.0.1 → 0.0.2
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.
- data/README.md +2 -1
- data/lib/rack/request_context.rb +7 -18
- data/lib/request_context.rb +6 -31
- data/lib/request_context/version.rb +1 -1
- data/spec/rack/request_context_spec.rb +3 -7
- data/spec/request_context_spec.rb +1 -17
- metadata +1 -1
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# RequestContext
|
2
2
|
|
3
|
-
This is a gem that adds
|
3
|
+
This is a gem that adds Rack::Request to `Thread.current[:request_context]`.
|
4
|
+
This may be useful, for example, if you want access to request information in your models.
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
data/lib/rack/request_context.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Rack
|
2
2
|
|
3
|
-
# Public: Rack middleware that stores
|
4
|
-
# thread local variable.
|
3
|
+
# Public: Rack middleware that stores Rack Request in a thread local variable.
|
5
4
|
#
|
6
5
|
# app - The Rack app.
|
7
6
|
#
|
@@ -9,8 +8,8 @@ module Rack
|
|
9
8
|
#
|
10
9
|
# use Rack::RequestContext
|
11
10
|
#
|
12
|
-
# puts "Thread.current[:request_context]"
|
13
|
-
# # =>
|
11
|
+
# puts "Thread.current[:request_context].url"
|
12
|
+
# # => 'https://www.remind101.com'
|
14
13
|
class RequestContext
|
15
14
|
|
16
15
|
def initialize(app)
|
@@ -18,20 +17,10 @@ module Rack
|
|
18
17
|
end
|
19
18
|
|
20
19
|
def call(env)
|
21
|
-
::RequestContext.
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def request_context(env)
|
29
|
-
request = Rack::Request.new(env)
|
30
|
-
{ origo: request.cookies['origo'],
|
31
|
-
url: request.url,
|
32
|
-
user_agent: request.user_agent,
|
33
|
-
referrer: request.referer,
|
34
|
-
ip: request.ip }
|
20
|
+
::RequestContext.request_context = Rack::Request.new(env)
|
21
|
+
@app.call(env)
|
22
|
+
ensure
|
23
|
+
::RequestContext.request_context = nil
|
35
24
|
end
|
36
25
|
|
37
26
|
end
|
data/lib/request_context.rb
CHANGED
@@ -12,15 +12,11 @@ module RequestContext
|
|
12
12
|
#
|
13
13
|
# Examples
|
14
14
|
#
|
15
|
-
# RequestContext.request_context
|
16
|
-
# # =>
|
17
|
-
# # :url=>"http://localhost:5000/v2/groups/31419?group%5Bclass_name%5D=Spanish+6&auth_token=btVuzxrUYcaYJvnkVTbA",
|
18
|
-
# # :user_agent=>"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36",
|
19
|
-
# # :referrer=>"http://localhost:5000/",
|
20
|
-
# # :ip=>"127.0.0.1"}
|
15
|
+
# RequestContext.request_context.url
|
16
|
+
# # => 'https://www.remind101.com'
|
21
17
|
|
22
18
|
#
|
23
|
-
# Returns
|
19
|
+
# Returns Rack::Request.
|
24
20
|
def request_context
|
25
21
|
Thread.current[:request_context]
|
26
22
|
end
|
@@ -29,34 +25,13 @@ module RequestContext
|
|
29
25
|
#
|
30
26
|
# Examples
|
31
27
|
#
|
32
|
-
# RequestContext.request_context =
|
33
|
-
# # =>
|
28
|
+
# RequestContext.request_context = Rack::Request(env)
|
29
|
+
# # => #<Rack::Request:0x007fe675a39950 @env={...}>
|
34
30
|
#
|
35
|
-
# Returns the new
|
31
|
+
# Returns the new Rack::Request request context.
|
36
32
|
def request_context=(request_context)
|
37
33
|
Thread.current[:request_context] = request_context
|
38
34
|
end
|
39
35
|
|
40
|
-
# Public: Runs the block with the given request context set.
|
41
|
-
#
|
42
|
-
# Examples
|
43
|
-
#
|
44
|
-
# RequestContext.request_context
|
45
|
-
# # => {url: 'https://www.remind101.com'}
|
46
|
-
#
|
47
|
-
# RequestContext.with_request_context({url: 'https://www.remind101.com/users/sign_in'}) do
|
48
|
-
# RequestContext.request_context
|
49
|
-
# # => {url: 'https://www.remind101.com/users/sign_in'}
|
50
|
-
# end
|
51
|
-
#
|
52
|
-
# RequestContext.request_context
|
53
|
-
# # => {url: 'https://www.remind101.com'}
|
54
|
-
def with_request_context(request_context)
|
55
|
-
last_request_context = RequestContext.request_context
|
56
|
-
RequestContext.request_context = request_context
|
57
|
-
yield
|
58
|
-
ensure
|
59
|
-
RequestContext.request_context = last_request_context
|
60
|
-
end
|
61
36
|
end
|
62
37
|
end
|
@@ -6,17 +6,13 @@ describe Rack::RequestContext do
|
|
6
6
|
let(:middleware) { described_class.new app }
|
7
7
|
|
8
8
|
describe '.call' do
|
9
|
-
let(:request_context)
|
10
|
-
{origo: nil, url: '://::0', user_agent: 'hello', referrer: nil, ip: nil}
|
11
|
-
end
|
12
|
-
|
13
|
-
let(:env) { {'HTTP_USER_AGENT' => 'hello'} }
|
9
|
+
let(:request_context) { Rack::Request.new({}) }
|
14
10
|
|
15
11
|
it 'stores the request_context in a thread local' do
|
16
|
-
Thread.current.should_receive(:[]=).with(:request_context,
|
12
|
+
Thread.current.should_receive(:[]=).with(:request_context, kind_of(Rack::Request) )
|
17
13
|
app.should_receive(:call)
|
18
14
|
Thread.current.should_receive(:[]=).with(:request_context, nil)
|
19
|
-
middleware.call(
|
15
|
+
middleware.call({})
|
20
16
|
end
|
21
17
|
|
22
18
|
end
|
@@ -1,13 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe RequestContext do
|
4
|
-
let(:request_context)
|
5
|
-
{ origo: nil,
|
6
|
-
url: 'https://www.remind101.com',
|
7
|
-
user_agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36',
|
8
|
-
referrer: 'http://search.yahoo.com/search?p=remind101',
|
9
|
-
ip: '127.0.0.1' }
|
10
|
-
end
|
4
|
+
let(:request_context) { Rack::Request.new({}) }
|
11
5
|
|
12
6
|
describe '.request_context' do
|
13
7
|
subject { described_class.request_context }
|
@@ -25,14 +19,4 @@ describe RequestContext do
|
|
25
19
|
end
|
26
20
|
end
|
27
21
|
|
28
|
-
describe '.with_request_context' do
|
29
|
-
before { Thread.current[:request_context] = request_context }
|
30
|
-
|
31
|
-
it 'sets the request_context to the new request_context' do
|
32
|
-
described_class.with_request_context( {url: 'https://www.remind101.com', ip: '10.10.10.10'} ) do
|
33
|
-
expect(described_class.request_context).to eq({url: 'https://www.remind101.com', ip: '10.10.10.10'})
|
34
|
-
end
|
35
|
-
expect(described_class.request_context).to eq request_context
|
36
|
-
end
|
37
|
-
end
|
38
22
|
end
|