rack-current 1.0.0
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/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/lib/rack-current.rb +8 -0
- data/lib/rack-current/middleware.rb +25 -0
- data/lib/rack-current/version.rb +3 -0
- data/rack-current.gemspec +22 -0
- data/spec/current_request_spec.rb +27 -0
- data/spec/spec_helper.rb +1 -0
- metadata +112 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Charles Lowell
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# CurrentRequest
|
2
|
+
|
3
|
+
Sometimes, in the coures of human events, you need to access the currently executing http request, and you
|
4
|
+
don't always have the luxury of passing it around, especially when you're working with frameworks that manage
|
5
|
+
the lifecycle of objects for you, or you want to include functionality from a module. That module might not have
|
6
|
+
a clean way of getting a reference to the request object.
|
7
|
+
|
8
|
+
### CurrentRequest can help
|
9
|
+
|
10
|
+
CurrentRequest stores the current request in a thread local so that you can access it from anywhere inside the
|
11
|
+
request stack.
|
12
|
+
|
13
|
+
### Oooh. Thread locals, those are dangerous I hear.
|
14
|
+
|
15
|
+
Not really. There are certain objects whose natural scope is the lifetime of an http request. The http request object
|
16
|
+
itself is one such object. As long as the thread is the fundamental unit of concurrency in your server, you'll be ok.
|
17
|
+
|
18
|
+
That said, you should ensure that you are in fact including this into methods that will be invoked inside the request
|
19
|
+
stack, and not, say, like a background job (like sending email) that is fired off during the course of a request.
|
20
|
+
|
21
|
+
### Example
|
22
|
+
|
23
|
+
class Foo
|
24
|
+
include CurrentRequest
|
25
|
+
|
26
|
+
def current_url
|
27
|
+
"#{current_request.protocol}://#{current_request.host}/#{current_request.path}
|
28
|
+
end
|
29
|
+
end
|
data/Rakefile
ADDED
data/lib/rack-current.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rack'
|
2
|
+
|
3
|
+
module Rack::Current
|
4
|
+
class Middleware
|
5
|
+
include Rack::Current
|
6
|
+
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
10
|
+
|
11
|
+
def call(env)
|
12
|
+
before = self.current_request
|
13
|
+
self.current_request = Rack::Request.new(env)
|
14
|
+
@app.call(env)
|
15
|
+
ensure
|
16
|
+
self.current_request = before
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def current_request=(request)
|
22
|
+
Thread.current[:_current_request] = request
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack-current/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["The Frontside Software"]
|
6
|
+
gem.email = ["info@thefrontside.net"]
|
7
|
+
gem.description = %q{Get the Currently running Rack::Request from anywhere inside the request-thread}
|
8
|
+
gem.summary = %q{Reach out and grab the current Rack::Request just by including CurrentRequest}
|
9
|
+
gem.homepage = "https://github.com/thefrontside/rack-current"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "rack-current"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = CurrentRequest::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'rack'
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake"
|
21
|
+
gem.add_development_dependency "rspec"
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rack-current'
|
3
|
+
describe Rack::Current::Middleware do
|
4
|
+
describe "inside an application's middleware stack" do
|
5
|
+
include Rack::Current
|
6
|
+
before do
|
7
|
+
@app = mock(:app)
|
8
|
+
@middleware = Rack::Current::Middleware.new(@app)
|
9
|
+
end
|
10
|
+
describe "when I invoke it" do
|
11
|
+
before do
|
12
|
+
@app.stub(:call) do |env|
|
13
|
+
@env = env
|
14
|
+
@current_request = current_request
|
15
|
+
end
|
16
|
+
@middleware.call({:request => :env})
|
17
|
+
end
|
18
|
+
it "makes the current request available" do
|
19
|
+
@current_request.should be_kind_of Rack::Request
|
20
|
+
@current_request.env.should eql ({:request => :env})
|
21
|
+
end
|
22
|
+
it "invokes the upstream app" do
|
23
|
+
@env.should eql ({:request => :env})
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
$:.unshift File.expand_path '../../lib', __FILE__
|
metadata
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-current
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- The Frontside Software
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Get the Currently running Rack::Request from anywhere inside the request-thread
|
63
|
+
email:
|
64
|
+
- info@thefrontside.net
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/rack-current.rb
|
75
|
+
- lib/rack-current/middleware.rb
|
76
|
+
- lib/rack-current/version.rb
|
77
|
+
- rack-current.gemspec
|
78
|
+
- spec/current_request_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
homepage: https://github.com/thefrontside/rack-current
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
hash: 4398315886198829610
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
hash: 4398315886198829610
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 1.8.24
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Reach out and grab the current Rack::Request just by including CurrentRequest
|
110
|
+
test_files:
|
111
|
+
- spec/current_request_spec.rb
|
112
|
+
- spec/spec_helper.rb
|