From fc530b617ca39a9aaebc6b50f09f233b7ec43346 Mon Sep 17 00:00:00 2001 From: maraby Date: Sun, 6 Jan 2008 18:06:33 -0500 Subject: [PATCH] Fixed the example application to be more thorough in documenting how to handle the route and dispatch requests. --- mrouter/mrouter.rb | 14 ++++++++++---- mrouter/rackapp.rb | 20 ++++++++++++++++++-- mrouter/rackapp.ru | 2 ++ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/mrouter/mrouter.rb b/mrouter/mrouter.rb index 9065075..33352bd 100644 --- a/mrouter/mrouter.rb +++ b/mrouter/mrouter.rb @@ -19,13 +19,12 @@ end module Rack - # = Routing - # - # Handles routing. + # = MRouter, the Rack Merb Router # # == Usage # - # class Xy + # class RackApp + # include MRouterHelper # route do |r| # r.match('/path/to/match').to(:action => 'do_stuff') # {:action => 'not_found'} # the default route @@ -33,6 +32,9 @@ module Rack # def do_stuff(params) # [200, {}, 'OK'] # end + # def call(env) + # [200, {'Content-Type'=>'text/plain'}, env['merb.route'].inspect] + # end # end # # == Default Routes @@ -45,6 +47,10 @@ module Rack # In order to set a different default route, simply end the call to +route+ # with a hash containing the action to run along with any other params. # + # If your particular app requires another field to default to, such as a + # controller of some sort, defining a default route will be necessary as + # MRouter only knows it should default to +not_found+. + # # == The Hard Work # # The mechanics of the router are solely from the efforts of the Merb diff --git a/mrouter/rackapp.rb b/mrouter/rackapp.rb index d12a657..e94c5e5 100755 --- a/mrouter/rackapp.rb +++ b/mrouter/rackapp.rb @@ -8,12 +8,28 @@ class Rap include MRouterHelper route do |r| - r.match('/').to(:action => 'foo') + r.match('/').to(:action => 'foo', :random => 'arbitrary') + r.match('/hello/:name').to(:action => 'greeter') end def call(env) - [200, {'Content-Type'=>'text/plain'}, env['merb.route'].inspect] + @env = env + @route = env['merb.route'].dup + send(@route.delete(:action).to_sym, @route) end + + def foo(params) + [200, {'Content-Type'=>'text/plain'}, @route.inspect] + end + + def greeter(params) + [200, {'Content-Type'=>'text/plain'}, "Hello #{params[:name]}"] + end + + def not_found(params) + [404, {'Content-Type'=>'text/plain'}, 'Not Found'] + end + end Rack::Handler::Mongrel.run Rack::MRouter.new(Rap.new), :Port => 4400 if $0 == __FILE__ diff --git a/mrouter/rackapp.ru b/mrouter/rackapp.ru index ca7f02c..c25dfd7 100644 --- a/mrouter/rackapp.ru +++ b/mrouter/rackapp.ru @@ -1,3 +1,5 @@ +# call with: rackup -p 4400 rackapp.ru + require 'rackapp' use Rack::MRouter -- 2.11.4.GIT