Sha256: 2225da37a6335a71b5bf7764352c53d311048bc51dbe6ea607c890e661f0d3fc

Contents?: true

Size: 886 Bytes

Versions: 9

Compression:

Stored size: 886 Bytes

Contents

module Rack
  autoload :RuleSet, 'rack/rewrite/rule'
  autoload :VERSION, 'rack/rewrite/version'
  
  # 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)
      @app = app
      @rule_set = RuleSet.new
      @rule_set.instance_eval(&rule_block) if block_given?
    end
    
    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
        
    private
      def find_first_matching_rule(env) #:nodoc:
        @rule_set.rules.detect { |rule| rule.matches?(env) }
      end    
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
rack-rewrite-1.4.01 lib/rack/rewrite.rb
rack-rewrite-matches-1.3.3 lib/rack/rewrite.rb
rack-rewrite-1.3.3 lib/rack/rewrite.rb
rack-rewrite-1.3.2 lib/rack/rewrite.rb
rack-rewrite-1.3.1 lib/rack/rewrite.rb
rack-rewrite-1.3.0 lib/rack/rewrite.rb
rack-rewrite-1.2.1 lib/rack/rewrite.rb
rack-rewrite-1.2.0 lib/rack/rewrite.rb
rack-rewrite-1.1.0 lib/rack/rewrite.rb