Sha256: af0b92893729caf9ac00114ecdd56391d5ad47f4278af1b5e932555d703b4655

Contents?: true

Size: 947 Bytes

Versions: 2

Compression:

Stored size: 947 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

2 entries across 2 versions & 1 rubygems

Version Path
urb-0.1.5 lib/urb/middleware.rb
urb-0.1.4 lib/urb/middleware.rb