rack-if 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ .DS_Store
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in rack-if.gemspec
4
+ gemspec
5
+
6
+ group :demo do
7
+ gem 'shotgun'
8
+ gem 'ruby-debug'
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Sam Schenkman-Moore, David Dollar
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,38 @@
1
+ = Rack::If
2
+
3
+ Use or don't use rack apps based on a variety of environment variables.
4
+
5
+ == Use
6
+
7
+ gem install rackif
8
+
9
+ Example (in your config.ru):
10
+
11
+ require 'rackif'
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
17
+ end
18
+ end
19
+
20
+ run MyApp.new # when above matches, this is protected
21
+
22
+ == Configuration
23
+
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.
26
+
27
+ Example:
28
+
29
+ use Rack::If do
30
+ unless env['rack.session']['verified_identity']
31
+ use EasyRackOpenID
32
+ end
33
+ end
34
+
35
+
36
+ == Copyright
37
+
38
+ Copyright (c) 2009 Sam Schenkman-Moore, David Dollar. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/config.ru ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rack'
3
+ require 'lib/rack-if'
4
+
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
10
+ end
11
+ end
12
+
13
+ run lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'Hi'] }
data/lib/rack-if.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "#{File.dirname(__FILE__)}/rack-if/server"
2
+
3
+ module Rack
4
+ module If
5
+ def self.new(*args, &blk)
6
+ lambda { |env| Server.new(*args, &blk).call(env) }
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,44 @@
1
+ module Rack
2
+ module If
3
+ class Server
4
+
5
+ attr_reader :app, :env
6
+
7
+ def initialize(app, options={}, &block)
8
+ @options = options
9
+ @app = app
10
+
11
+ @middleware = []
12
+ @block = block
13
+ end
14
+
15
+ # shortcuts
16
+ def path ; env["PATH_INFO"] ; end
17
+ def method ; env["REQUEST_METHOD"] ; end
18
+ def user_agent ; env["HTTP_USER_AGENT"] ; end
19
+ def host ; env["HTTP_HOST"] ; end
20
+ def port ; env["SERVER_PORT"] ; end
21
+ def query_string ; env["QUERY_STRING"] ; end
22
+ def http_accept ; env["HTTP_ACCEPT"] ; end
23
+ def http_accept_encoding ; env["HTTP_ACCEPT_ENCODING"] ; end
24
+
25
+ def call(rack_environment)
26
+ @env = rack_environment
27
+ instance_eval(&@block)
28
+ if @middleware.any?
29
+ stack.call(env)
30
+ else
31
+ app.call(env)
32
+ end
33
+ end
34
+
35
+ def stack
36
+ @middleware.reverse.inject(app) { |app, mid| mid.call(app) }
37
+ end
38
+
39
+ def use(middleware, *args, &block)
40
+ @middleware << lambda { |app| middleware.new(app, *args, &block) }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module If
3
+ VERSION = "0.1.2"
4
+ end
5
+ end
data/rack-if.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "rack-if/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "rack-if"
7
+ s.version = Rack::If::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Sam Schenkman-Moore"]
10
+ s.email = ["samsm@samsm.com"]
11
+ s.homepage = "http://github.com/samsm/rack-if"
12
+ s.summary = %q{Conditional use of rack apps.}
13
+ s.description = %q{Use or don't use Rack apps based on a variety of environment factors.}
14
+
15
+ s.rubyforge_project = "rack-if"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'rack', ['> 1']
23
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'rack-if'
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -0,0 +1,63 @@
1
+ require "#{File.dirname(__FILE__)}/helper"
2
+ require 'rack/request'
3
+ require 'rack/lint'
4
+
5
+ class TestRackIf < Test::Unit::TestCase
6
+
7
+ class BadMethodMiddleWare
8
+ def initialize(*args) ; end
9
+ def call(env)
10
+ [405, {'Content-Type' => 'text/plain'}, 'That method wasn\'t allowed.']
11
+ end
12
+ end
13
+
14
+ def setup
15
+ app = lambda { |env| [200, {'Content-Type' => 'text/plain'}, 'Hi'] }
16
+ @if = Rack::If.new(app) do
17
+ if method != 'GET'
18
+ use BadMethodMiddleWare
19
+ end
20
+ end
21
+ end
22
+
23
+ def mock_request(options = {})
24
+ {
25
+ "HTTP_CACHE_CONTROL"=>"max-age=0",
26
+ "HTTP_HOST"=>"localhost:9292",
27
+ "HTTP_ACCEPT"=>"application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
28
+ "SERVER_NAME"=>"localhost",
29
+ "REQUEST_PATH"=>"/protected",
30
+ "rack.url_scheme"=>"http",
31
+ "HTTP_USER_AGENT"=>"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1",
32
+ "REMOTE_HOST"=>"localhost",
33
+ "HTTP_ACCEPT_LANGUAGE"=>"en-us",
34
+ "SERVER_PROTOCOL"=>"HTTP/1.1",
35
+ "rack.input"=>Rack::Lint::InputWrapper.new('test'),
36
+ "rack.version"=>[1, 1],
37
+ "rack.run_once"=>false,
38
+ "SERVER_SOFTWARE"=>"WEBrick/1.3.1 (Ruby/1.8.7/2010-08-16)",
39
+ "REMOTE_ADDR"=>"127.0.0.1",
40
+ "PATH_INFO"=>"/protected",
41
+ "HTTP_AUTHORIZATION"=>"Basic OnNlY3JldA==",
42
+ "SCRIPT_NAME"=>"",
43
+ "HTTP_VERSION"=>"HTTP/1.1",
44
+ "rack.multithread"=>true,
45
+ "rack.multiprocess"=>false,
46
+ "REQUEST_URI"=>"http://localhost:9292/protected",
47
+ "SERVER_PORT"=>"9292",
48
+ "REQUEST_METHOD"=>"GET",
49
+ "HTTP_ACCEPT_ENCODING"=>"gzip, deflate",
50
+ "HTTP_CONNECTION"=>"keep-alive",
51
+ "QUERY_STRING"=>"",
52
+ "GATEWAY_INTERFACE"=>"CGI/1.1"
53
+ }.merge(options)
54
+ end
55
+
56
+ def test_pass_through_when_no_middleware_added
57
+ assert_equal 200, @if.call(mock_request({'REQUEST_METHOD' => 'GET'})).first
58
+ end
59
+
60
+ def test_use_middleware_when_conditions_add_it
61
+ assert_equal 405, @if.call(mock_request({'REQUEST_METHOD' => 'PUT'})).first
62
+ end
63
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-if
3
+ version: !ruby/object:Gem::Version
4
+ hash: 31
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 2
10
+ version: 0.1.2
11
+ platform: ruby
12
+ authors:
13
+ - Sam Schenkman-Moore
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-26 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rack
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">"
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ version: "1"
33
+ type: :runtime
34
+ version_requirements: *id001
35
+ description: Use or don't use Rack apps based on a variety of environment factors.
36
+ email:
37
+ - samsm@samsm.com
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - .gitignore
46
+ - Gemfile
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - config.ru
51
+ - lib/rack-if.rb
52
+ - lib/rack-if/server.rb
53
+ - lib/rack-if/version.rb
54
+ - rack-if.gemspec
55
+ - test/.DS_Store
56
+ - test/helper.rb
57
+ - test/test_rack-if.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/samsm/rack-if
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project: rack-if
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Conditional use of rack apps.
92
+ test_files:
93
+ - test/helper.rb
94
+ - test/test_rack-if.rb