# This is what an application build with our framework would look like.
# Run with:
# rackup app.ru
app = App.new do
get "" do
render "hi" # Specifies the body of the response
end
get "echo/(.*)" do |text| # Regexp matches are passed as block args
render text
end
get "session/set" do
session[params.first.key] = params.first.value
end
get "session" do
render session.inspect
end
get "form" do
render erb(:form) # Can have helper methods to render erb and the like
end
post "form" do
render params.inspect
end
end
run app
Refactorings
No refactoring yet !
Dan Kubb
September 14, 2008, September 14, 2008 18:48, permalink
What about being able to nest blocks according to their path?
Also, why the explicit render() call? Just make it so the return value from the block is the text that is rendered in the response body.
Here's an example:
app = App.new do
get do
"hi"
end
get "echo/(.*)" do |text| # Regexp matches are passed as block args
text
end
# nested
path "session" do
get "set" do
session[params.first.key] = params.first.value
end
get do
session.inspect
end
end
path "form" do
get do
erb(:form) # Can have helper methods to render erb and the like
end
post do
params.inspect
end
end
end
run app
I'll be presenting http://rack.rubyforge.org this Tuesday at the first Montreal Against Rails (http://www.montrealonrails.com/2008/09/11/montreal_against_rails-tuesday).
I'll show how to use Rack and then I'd like to try something new (and probably crazy-stupid). Building a web framework with Rack is so easy, I'll be doing pair programming with anyone from the audience to create our own custom framework live during the presentation (in 30 min). We'll start with the code posted here on RefactorMyCode as the application code, we'll implement the framework code during the presentation. So submit your ideas here before the event.
And make sure to RSVP (http://upcoming.yahoo.com/event/1093407) if your in Montreal next Tuesday!