remove rack mapper support

This commit is contained in:
Josh Hull 2012-04-11 11:36:33 -07:00
parent 108d2dbdb2
commit 2e5e206f39
5 changed files with 12 additions and 109 deletions

View File

@ -1,19 +1,18 @@
require 'http_router'
HttpRouter::Rack.override_rack_builder!
run HttpRouter.new do
add('/get/:id', :match_with => {:id => /\d+/}) { |env|
[200, {'Content-type' => 'text/plain'}, ["My id is #{env['router.params'][:id]}, which is a number\n"]]
}
map('/get/:id', :match_with => {:id => /\d+/}) { |env|
[200, {'Content-type' => 'text/plain'}, ["My id is #{env['router.params'][:id]}, which is a number\n"]]
}
# you have post, get, head, put and delete.
post('/get/:id') { |env|
[200, {'Content-type' => 'text/plain'}, ["My id is #{env['router.params'][:id]} and you posted!\n"]]
}
map('/get/:id') { |env|
[200, {'Content-type' => 'text/plain'}, ["My id is #{env['router.params'][:id]}\n"]]
}
# you have post, get, head, put and delete.
post('/get/:id') { |env|
[200, {'Content-type' => 'text/plain'}, ["My id is #{env['router.params'][:id]} and you posted!\n"]]
}
map('/get/:id') { |env|
[200, {'Content-type' => 'text/plain'}, ["My id is #{env['router.params'][:id]}\n"]]
}
end
# $ curl http://127.0.0.1:3000/get/foo
# => My id is foo

View File

@ -11,7 +11,6 @@ require 'http_router/generator'
require 'http_router/route_helper'
require 'http_router/generation_helper'
require 'http_router/regex_route_generation'
require 'http_router/rack'
require 'http_router/util'
class HttpRouter

View File

@ -1,19 +0,0 @@
class HttpRouter
module Rack
autoload :URLMap, 'http_router/rack/url_map'
autoload :Builder, 'http_router/rack/builder'
autoload :BuilderMixin, 'http_router/rack/builder'
# Monkey-patches Rack::Builder to use HttpRouter.
# See examples/rack_mapper.rb
def self.override_rack_builder!
::Rack::Builder.class_eval("remove_method :map; include HttpRouter::Rack::BuilderMixin")
end
# Monkey-patches Rack::URLMap to use HttpRouter.
# See examples/rack_mapper.rb
def self.override_rack_urlmap!
::Rack.class_eval("OriginalURLMap = URLMap; HttpRouterURLMap = HttpRouter::Rack::URLMap; remove_const :URLMap; URLMap = HttpRouterURLMap")
end
end
end

View File

@ -1,60 +0,0 @@
require 'http_router'
# Replacement for {Rack::Builder} which using HttpRouter to map requests instead of a simple Hash.
# As well, add convenience methods for the request methods.
module HttpRouter::Rack::BuilderMixin
def router
@router ||= HttpRouter.new
end
# Maps a path to a block.
# @param path [String] Path to map to.
# @param options [Hash] Options for added path.
# @see HttpRouter#add
def map(path, options = {}, method = nil, &block)
route = router.send(method || :add, path, options)
route.to(&block)
@ins << router unless @ins.last == router
route
end
# Maps a path with request methods `HEAD` and `GET` to a block.
# @param path [String] Path to map to.
# @param options [Hash] Options for added path.
# @see HttpRouter#add
def get(path, options = {}, &block)
map(path, options, :get, &block)
end
# Maps a path with request methods `POST` to a block.
# @param path [String] Path to map to.
# @param options [Hash] Options for added path.
# @see HttpRouter#add
def post(path, options = {}, &block)
map(path, options, :post, &block)
end
# Maps a path with request methods `PUT` to a block.
# @param path [String] Path to map to.
# @param options [Hash] Options for added path.
# @see HttpRouter#add
def put(path, options = {}, &block)
map(path, options, :put, &block)
end
# Maps a path with request methods `DELETE` to a block.
# @param path [String] Path to map to.
# @param options [Hash] Options for added path.
# @see HttpRouter#add
def delete(path, options = {}, &block)
map(path, options, :delete, &block)
end
def options(path, options = {}, &block)
map(path, options, :options, &block)
end
end
class HttpRouter::Rack::Builder < ::Rack::Builder
include HttpRouter::Rack::BuilderMixin
end

View File

@ -1,16 +0,0 @@
require 'http_router'
class HttpRouter
module Rack
class URLMap < ::Rack::URLMap
def initialize(map = {})
@router = HttpRouter.new
map.each { |path, app| (path =~ /^(https?):\/\/(.*?)(\/.*)/ ? @router.add($3).host($2).scheme($1) : @router.add(path)).partial.to(app) }
end
def call(env)
@router.call(env)
end
end
end
end