Sha256: be3714845a73b73869aa7c0a8e204e954280ee32aee65174d7cf2f35a7e1904c

Contents?: true

Size: 946 Bytes

Versions: 4

Compression:

Stored size: 946 Bytes

Contents

module URB
  class Middleware

    def initialize(app)
      @app = app
    end

    def call(env)
      if env["PATH_INFO"].match(/^#{URB::PATH}(\w+)?/)
        request = Rack::Request.new env
        path = request.get? ? URB.fetch($1) : request.params["path"]

        if path
          return [302, {"Location" => "#{URB::PATH}#{URB.store(path)}"}, ["Moved Temporarily"]] unless request.get?

          path, query_string = path.match(/^([^\?]+)\?(.*)$/).captures
          params = Rack::Utils.parse_nested_query query_string
          params.each{|key, value| request.update_param key, value}

          env["PATH_INFO"] = path
          env["REQUEST_URI"] = "#{env["HTTP_ORIGIN"]}#{path}?#{query_string}" if env["REQUEST_URI"]
          env["REQUEST_PATH"] = path if env["REQUEST_PATH"]
          env["ORIGINAL_FULLPATH"] = "#{path}?#{query_string}" if env["ORIGINAL_FULLPATH"]

        end
      end
      @app.call env
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
urb-0.1.3 lib/urb/middleware.rb
urb-0.1.2 lib/urb/middleware.rb
urb-0.1.1 lib/urb/middleware.rb
urb-0.1.0 lib/urb/middleware.rb