rackif 0.0.3 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +15 -22
- data/VERSION +1 -1
- data/config.ru +5 -3
- data/lib/if.rb +18 -46
- metadata +2 -2
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
= Rack::If
|
2
2
|
|
3
|
-
Use or don't use rack apps based on a variety of environment variables.
|
3
|
+
Use or don't use rack apps based on a variety of environment variables. That was the original purpose of this gem, however Rack::If basically allows you make adjustment to the Rack stack based on environment variables.
|
4
4
|
|
5
5
|
== Use
|
6
6
|
|
@@ -9,9 +9,11 @@ gem install rackif
|
|
9
9
|
Example (in your config.ru):
|
10
10
|
|
11
11
|
require 'rackif'
|
12
|
-
use Rack::If
|
13
|
-
|
14
|
-
|
12
|
+
use Rack::If do
|
13
|
+
if path == '/protected' && method != 'GET'
|
14
|
+
use Rack::Auth::Basic, "Rack::If Example" do |username, password|
|
15
|
+
'secret' == password
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
|
@@ -19,27 +21,18 @@ Example (in your config.ru):
|
|
19
21
|
|
20
22
|
== Configuration
|
21
23
|
|
22
|
-
Rack::If
|
24
|
+
Rack::If block
|
25
|
+
Shortcut methods for path, method, user_agent, host, port, query_string, http_accept, and http_accept_encoding are available, anything else is available at env.
|
23
26
|
|
24
|
-
|
27
|
+
Example:
|
25
28
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
29
|
+
use Rack::If do
|
30
|
+
unless env['rack.session']['verified_identity']
|
31
|
+
use EasyRackOpenID
|
32
|
+
end
|
33
|
+
end
|
31
34
|
|
32
|
-
== Note on Patches/Pull Requests
|
33
|
-
|
34
|
-
* Fork the project.
|
35
|
-
* Make your feature addition or bug fix.
|
36
|
-
* Add tests for it. This is important so I don't break it in a
|
37
|
-
future version unintentionally.
|
38
|
-
* Commit, do not mess with rakefile, version, or history.
|
39
|
-
(if you want to have your own version, that is fine but
|
40
|
-
bump version in a commit by itself I can ignore when I pull)
|
41
|
-
* Send me a pull request. Bonus points for topic branches.
|
42
35
|
|
43
36
|
== Copyright
|
44
37
|
|
45
|
-
Copyright (c) 2009 Sam Schenkman-Moore. See LICENSE for details.
|
38
|
+
Copyright (c) 2009 Sam Schenkman-Moore, David Dollar. See LICENSE for details.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/config.ru
CHANGED
@@ -2,9 +2,11 @@ require 'rubygems'
|
|
2
2
|
require 'rack'
|
3
3
|
require 'lib/rackif'
|
4
4
|
|
5
|
-
use Rack::If
|
6
|
-
|
7
|
-
|
5
|
+
use Rack::If do
|
6
|
+
if path == '/protected' && method == 'GET'
|
7
|
+
use Rack::Auth::Basic, "Rack::If Example" do |username, password|
|
8
|
+
'secret' == password
|
9
|
+
end
|
8
10
|
end
|
9
11
|
end
|
10
12
|
|
data/lib/if.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
class Rack::If
|
2
2
|
|
3
|
-
attr_reader :app
|
3
|
+
attr_reader :app, :env
|
4
4
|
|
5
5
|
def initialize(app, options={}, all_or_any = :all, &block)
|
6
6
|
@methodology = all_or_any
|
@@ -8,63 +8,35 @@ class Rack::If
|
|
8
8
|
@app = app
|
9
9
|
|
10
10
|
@middleware = []
|
11
|
-
|
11
|
+
@block = block
|
12
12
|
end
|
13
13
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
def match_any?(env)
|
24
|
-
@options.inject(false) do |memo,pair|
|
25
|
-
if_key = pair[0]
|
26
|
-
rack_key = comparison_table[if_key]
|
27
|
-
if_value = pair[1]
|
28
|
-
memo || if_value === env[rack_key]
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def match_all?(env)
|
33
|
-
@options.inject(true) do |memo, pair|
|
34
|
-
if_key = pair[0]
|
35
|
-
rack_key = comparison_table[if_key]
|
36
|
-
if_value = pair[1]
|
37
|
-
memo && if_value === env[rack_key]
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def comparison_table
|
42
|
-
{
|
43
|
-
:path => "PATH_INFO",
|
44
|
-
:method => "REQUEST_METHOD",
|
45
|
-
:user_agent => "HTTP_USER_AGENT",
|
46
|
-
:host => "HTTP_HOST",
|
47
|
-
:port => "SERVER_PORT",
|
48
|
-
:query_string => "QUERY_STRING",
|
49
|
-
:http_accept => "HTTP_ACCEPT",
|
50
|
-
:http_accept_encoding => "HTTP_ACCEPT_ENCODING"
|
51
|
-
}
|
52
|
-
end
|
14
|
+
# shortcuts
|
15
|
+
def path ; env["PATH_INFO"] ; end
|
16
|
+
def method ; env["REQUEST_METHOD"] ; end
|
17
|
+
def user_agent ; env["HTTP_USER_AGENT"] ; end
|
18
|
+
def host ; env["HTTP_HOST"] ; end
|
19
|
+
def port ; env["SERVER_PORT"] ; end
|
20
|
+
def query_string ; env["QUERY_STRING"] ; end
|
21
|
+
def http_accept ; env["HTTP_ACCEPT"] ; end
|
22
|
+
def http_accept_encoding ; env["HTTP_ACCEPT_ENCODING"] ; end
|
53
23
|
|
54
|
-
def call(
|
55
|
-
|
24
|
+
def call(rack_environment)
|
25
|
+
@env = rack_environment
|
26
|
+
instance_eval(&@block)
|
27
|
+
if @middleware.any?
|
56
28
|
stack.call(env)
|
57
29
|
else
|
58
30
|
app.call(env)
|
59
31
|
end
|
60
32
|
end
|
61
|
-
|
33
|
+
|
62
34
|
def stack
|
63
35
|
@middleware.reverse.inject(app) { |app, mid| mid.call(app) }
|
64
36
|
end
|
65
|
-
|
37
|
+
|
66
38
|
def use(middleware, *args, &block)
|
67
39
|
@middleware << lambda { |app| middleware.new(app, *args, &block) }
|
68
40
|
end
|
69
41
|
|
70
|
-
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rackif
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sam Schenkman-Moore
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2009-
|
13
|
+
date: 2009-12-09 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|