Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add external rules file option #22

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
r301 %r{/wiki/(\w+)_\w+}, '/$1'
end

=== Specify an external rules file AND additional rules in the block

config.middleware.insert_before(Rack::Lock, Rack::Rewrite, :rules => 'config/rules.rb') do
rewrite '/wiki/John_Trupiano', '/john'
r301 '/wiki/Yair_Flicker', '/yair'
r302 '/wiki/Greg_Jastrab', '/greg'
r301 %r{/wiki/(\w+)_\w+}, '/$1'
end

== Use Cases

=== Rebuild of existing site in a new technology
Expand Down
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
* OUTSTANDING
* Add :host support to restrict which URL's a rewrite rule matches [10/15/09]
* Add support for specifying a config file instead of passing a block (e.g. config/rewrite.rb) [10/15/09]
* Provide testing helpers (e.g. should_rewrite) to facilitate straightforward testing. [10/25/09]
* Allow rules to return arbitrary html (e.g. the contents of the maintenance page) [10/25/09]

Expand Down
7 changes: 6 additions & 1 deletion lib/rack/rewrite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ module Rack
# A rack middleware for defining and applying rewrite rules. In many cases you
# can get away with rack-rewrite instead of writing Apache mod_rewrite rules.
class Rewrite
def initialize(app, &rule_block)
def initialize(app, options = {}, &rule_block)
@app = app
@rule_set = RuleSet.new
@rule_set.instance_eval(&rule_block) if block_given?

rules_file = ::File.expand_path(options[:rules])
if ::File.exists?(rules_file)
@rule_set.instance_eval(::File.read(rules_file))
end
end

def call(env)
Expand Down