Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 0.58 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def path matcher
  2.   lambda {|req|
  3.     return unless match = req.env["PATH_INFO"].match(/^\/(#{matcher})(\/|$)/)
  4.  
  5.     path = match.captures[0]
  6.  
  7.     req.env["SCRIPT_NAME"] += "/#{path}"
  8.     req.env["PATH_INFO"] = "/#{match.post_match}"
  9.   }
  10. end
  11.  
  12. app = lambda {|env|
  13.   req = Rack::Request.new(env)
  14.   res = Rack::Response.new
  15.  
  16.   case req
  17.   when path("users") then
  18.     case req
  19.     when path("all") then
  20.       res.write "here they are"
  21.     else
  22.       res.write "users"
  23.     end
  24.   when path("about") then
  25.     res.write "about"
  26.   else
  27.     res.write "fail"
  28.   end
  29.  
  30.   res.status = 200
  31.   res.finish
  32. }
  33.  
  34. run app