提交 f69f9820 编写于 作者: J Joshua Peek

Wrap up http related routing helpers

上级 312c3bfa
......@@ -264,6 +264,42 @@ def match(*args)
end
end
module HttpHelpers
def get(*args, &block)
map_method(:get, *args, &block)
end
def post(*args, &block)
map_method(:post, *args, &block)
end
def put(*args, &block)
map_method(:put, *args, &block)
end
def delete(*args, &block)
map_method(:delete, *args, &block)
end
def redirect(path, options = {})
status = options[:status] || 301
lambda { |env|
req = Rack::Request.new(env)
url = req.scheme + '://' + req.host + path
[status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']]
}
end
private
def map_method(method, *args, &block)
options = args.extract_options!
options[:via] = method
args.push(options)
match(*args, &block)
self
end
end
class Constraints
def new(app, constraints = [])
if constraints.any?
......@@ -295,26 +331,11 @@ def call(env)
def initialize(set)
@set = set
extend HttpHelpers
extend Scoping
extend Resources
end
def get(*args, &block)
map_method(:get, *args, &block)
end
def post(*args, &block)
map_method(:post, *args, &block)
end
def put(*args, &block)
map_method(:put, *args, &block)
end
def delete(*args, &block)
map_method(:delete, *args, &block)
end
def root(options = {})
match '/', options.merge(:as => :root)
end
......@@ -375,15 +396,6 @@ def match(*args)
self
end
def redirect(path, options = {})
status = options[:status] || 301
lambda { |env|
req = Rack::Request.new(env)
url = req.scheme + '://' + req.host + path
[status, {'Location' => url, 'Content-Type' => 'text/html'}, ['Moved Permanently']]
}
end
private
def initialize_app_endpoint(options, defaults)
app = nil
......@@ -412,14 +424,6 @@ def validate_defaults!(app, defaults, segment_keys)
raise ArgumentError, "missing :action"
end
end
def map_method(method, *args, &block)
options = args.extract_options!
options[:via] = method
args.push(options)
match(*args, &block)
self
end
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册