diff options
author | jkeiser <jkeiser@opscode.com> | 2012-12-19 23:22:54 -0800 |
---|---|---|
committer | jkeiser <jkeiser@opscode.com> | 2012-12-19 23:23:21 -0800 |
commit | 666374b272a8851a2c57530a71a6183d4d06a648 (patch) | |
tree | b53efd8134aa36ab0d459f3e4e87e1a6bbd1fe21 /lib/chef_zero/router.rb | |
download | chef-zero-666374b272a8851a2c57530a71a6183d4d06a648.tar.gz |
Initial commit (moved/split up from jk/tiny-chef-server branch of jkeiser/chef)
Diffstat (limited to 'lib/chef_zero/router.rb')
-rw-r--r-- | lib/chef_zero/router.rb | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/lib/chef_zero/router.rb b/lib/chef_zero/router.rb new file mode 100644 index 0000000..0389c8a --- /dev/null +++ b/lib/chef_zero/router.rb @@ -0,0 +1,24 @@ +module ChefZero + class Router + def initialize(routes) + @routes = routes.map do |route, endpoint| + pattern = Regexp.new("^#{route.gsub('*', '[^/]*')}$") + [ pattern, endpoint ] + end + end + + attr_reader :routes + attr_accessor :not_found + + def call(env) + puts "#{env['REQUEST_METHOD']} #{env['PATH_INFO']}#{env['QUERY_STRING'] != '' ? "?" + env['QUERY_STRING'] : ''}" + clean_path = "/" + env['PATH_INFO'].split('/').select { |part| part != "" }.join("/") + routes.each do |route, endpoint| + if route.match(clean_path) + return endpoint.call(env) + end + end + not_found.call(env) + end + end +end |