Bfec5f7d1a4aaafc5a2451be8c42d26a

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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 !

63b22ac9bff0cd55d8a91da4dbf00693

Dan Kubb

September 14, 2008, September 14, 2008 18:48, permalink

No rating. Login to rate!

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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

Your refactoring





Format Copy from initial code

or Cancel