Skip to content

Commit

Permalink
Add example rack app.
Browse files Browse the repository at this point in the history
  • Loading branch information
mynyml committed May 19, 2009
1 parent 25d85bb commit c252a33
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
pkg/
2 changes: 1 addition & 1 deletion Rakefile
Expand Up @@ -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|
Expand Down
45 changes: 45 additions & 0 deletions examples/rack_app.ru
@@ -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.