Permalink
Browse files

Add example rack app.

  • Loading branch information...
1 parent 25d85bb commit c252a336037bc52177856ae04f607cb43dfdb1a8 @mynyml committed May 19, 2009
Showing with 47 additions and 1 deletion.
  1. +1 −0 .gitignore
  2. +1 −1 Rakefile
  3. +45 −0 examples/rack_app.ru
View
@@ -0,0 +1 @@
+pkg/
View
@@ -30,7 +30,7 @@ spec = Gem::Specification.new do |s|
s.homepage = ''
s.has_rdoc = true
s.require_path = "lib"
- s.files = all_except(/doc\/.*/)
+ s.files = all_except([/doc\/.*/, /pkg\/*/])
end
Rake::GemPackageTask.new(spec) do |p|
View
@@ -0,0 +1,45 @@
+require 'pathname'
+root = Pathname(__FILE__).dirname.parent.expand_path
+$:.unshift(root.join('lib'))
+
+# run me with:
+# $rackup examples/rack_app.ru
+# --------------------------------------------------
+require 'rubygems'
+require 'simple_router'
+
+class App
+ include SimpleRouter::DSL
+
+ get '/' do |params|
+ <<-html
+ <pre>
+ params: #{params.inspect}
+ </pre>
+ html
+ end
+
+ get '/:foo.:type' do |foo, type, params|
+ <<-html
+ <pre>
+ foo: #{foo.inspect}
+ type: #{type.inspect}
+ params: #{params.inspect}
+ </pre>
+ html
+ end
+
+ def call(env)
+ request = Rack::Request.new(env)
+
+ verb = request.request_method.downcase.to_sym
+ path = Rack::Utils.unescape(request.path_info)
+
+ route, args = self.class.routes.match(verb, path)
+ route.nil? ?
+ [404, {'Content-Type' => 'text/html'}, '404 page not found'] :
+ [200, {'Content-Type' => 'text/html'}, [route.action.call(*args.push(request.params))]]
+ end
+end
+
+run App.new

0 comments on commit c252a33

Please sign in to comment.