class Rack::RestfulSubmit

Constants

REWRITE_KEYWORD
REWRITE_MAP

Public Class Methods

new(app) click to toggle source
# File lib/rack/rack-restful_submit.rb, line 7
def initialize(app)
  @app = app
end

Public Instance Methods

call(env) click to toggle source
Calls superclass method
# File lib/rack/rack-restful_submit.rb, line 11
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 Instance Methods

rewrite(env, url, method) click to toggle source
# File lib/rack/rack-restful_submit.rb, line 34
def rewrite(env, url, method)
  rewrite_request(env, url)
  rewrite_method(env, method)
end
rewrite_method(env, method) click to toggle source
# File lib/rack/rack-restful_submit.rb, line 50
def rewrite_method(env, method)
  if HTTP_METHODS.include?(method)
    env["rack.methodoverride.original_method"] = env["REQUEST_METHOD"]
    env["REQUEST_METHOD"] = method
  end
end
rewrite_request(env, prefixed_uri) click to toggle source
# File lib/rack/rack-restful_submit.rb, line 39
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'].to_s)}\//, '/')

  env['REQUEST_URI'] = uri
  env['PATH_INFO'],env['QUERYSTRING']=uri,'' unless uri.index('?')
  env['PATH_INFO'],env['QUERYSTRING']=uri.split('?',2) if uri.index('?')
end