rackif 0.0.3 → 0.1.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.
Files changed (5) hide show
  1. data/README.rdoc +15 -22
  2. data/VERSION +1 -1
  3. data/config.ru +5 -3
  4. data/lib/if.rb +18 -46
  5. 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, {:path => /\A\/protected.*/, :method => 'POST'}, :any do
13
- use Rack::Auth::Basic, "Rack::If Example" do |username, password|
14
- 'secret' == password
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, options, all_or_any
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
- The third argument (:all or :any) is optional, defaults to :all.
27
+ Example:
25
28
 
26
- With :all, all options must match for the block to get run. In the above example, the path would need to start with /protected AND the method would need to be POST. With :any, if ANY of the criteria match (either POST or the path), the block is run.
27
-
28
- Options available are: path, method, user_agent, host, port, query_string, http_accept, and http_accept_encoding.
29
-
30
- Values given can be strings or regex (=== used for comparison).
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.3
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, {:path => '/protected', :method => 'POST'}, :any do
6
- use Rack::Auth::Basic, "Rack::If Example" do |username, password|
7
- 'secret' == password
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
- instance_eval(&block)
11
+ @block = block
12
12
  end
13
13
 
14
- def match?(env)
15
- case @methodology.to_s
16
- when 'all'
17
- match_all?(env)
18
- when 'any'
19
- match_any?(env)
20
- end
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(env)
55
- if match?(env)
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.3
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-11-19 00:00:00 -05:00
13
+ date: 2009-12-09 00:00:00 -05:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency