Class: Rack::Rewrite

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/rewrite.rb,
lib/rack/rewrite/rule.rb,
lib/rack/rewrite/version.rb

Overview

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.

Defined Under Namespace

Classes: Rule, RuleSet

Constant Summary collapse

VERSION =
File.read File.join(File.expand_path("..", __FILE__), "..", "..", "..", "VERSION")

Instance Method Summary collapse

Constructor Details

#initialize(app, &rule_block) ⇒ Rewrite

Returns a new instance of Rewrite.



8
9
10
11
12
# File 'lib/rack/rewrite.rb', line 8

def initialize(app, &rule_block)
  @app = app
  @rule_set = RuleSet.new
  @rule_set.instance_eval(&rule_block) if block_given?
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/rack/rewrite.rb', line 14

def call(env)
  if matched_rule = find_first_matching_rule(env)
    rack_response = matched_rule.apply!(env)
    # Don't invoke the app if applying the rule returns a rack response
    return rack_response unless rack_response === true
  end
  @app.call(env)
end