Sha256: be21c27e045cd382dfd3d6930afa09040a3abf2b87218954efc4082ff1ac8341

Contents?: true

Size: 802 Bytes

Versions: 1

Compression:

Stored size: 802 Bytes

Contents

module Maskara
  class Middleware
    def initialize(app)
      @app = app
    end

    def call(env)
      new_env = env
      if maskara_path? env['PATH_INFO']
        log "Maskara path: #{env['PATH_INFO']}"
        new_env = env.dup
        new_env['MASKARA_REQUEST'] = true
        new_env['REQUEST_URI'] = new_env['PATH_INFO'] = env['PATH_INFO'].sub(maskara_regex, '/')
        new_env['REQUEST_URI'] += "?#{env['QUERY_STRING']}" unless env['QUERY_STRING'].nil? || env['QUERY_STRING'].empty?
      end
      @app.call(new_env)
    end

    def maskara_path? path
      path =~ maskara_regex
    end

    def maskara_regex
      /\A(\/#{Maskara.path_stub}\/)/
    end

    def log msg
      if (defined? Rails)
        Rails.logger.debug msg
      else
        puts msg
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
maskara-1.0.0 lib/maskara/middleware.rb