Sha256: 6414c0d2113f4e69e3f08665bcdf3808184fc09a844dd8198421986f6ef4c8e2

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

module Rack
  class RestfulSubmit < MethodOverride

    REWRITE_KEYWORD = '__rewrite'.freeze
    REWRITE_MAP = '__map'.freeze

    def initialize(app)
      @app = app
    end

    def call(env)
      if env["REQUEST_METHOD"] == "POST"
        req = Request.new(env)

        if req.params[REWRITE_KEYWORD] && req.params[REWRITE_MAP]
          action = req.params[REWRITE_KEYWORD].keys.first # Should be always just one.
          mapping = req.params[REWRITE_MAP][action]

          if mapping && mapping['url'] && mapping['method']
            rewrite(env, mapping['url'], mapping['method'])
          else
            return super(env)
          end
        else
          return super(env)
        end
      end

      @app.call(env)
    end

    private

    def rewrite(env, url, method)
      rewrite_request(env, url)
      rewrite_method(env, method)
    end

    def rewrite_request(env, prefixed_uri)
      # rails 3 expects that relative_url_root is not part of
      # requested uri, this fix also expects that mapping['url']
      # contains only path (not full url)
      uri = prefixed_uri.sub(/^#{Regexp.escape(env['SCRIPT_NAME'])}\//, '/')

      env['PATH_INFO'], env['QUERYSTRING'] = uri.split("?",2)
    end

    def rewrite_method(env, method)
      if HTTP_METHODS.include?(method)
        env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
        env["REQUEST_METHOD"] = method
      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-restful_submit-1.2.0 lib/rack/rack-restful_submit.rb