Originally published on: Thu, 25 Mar 2010 02:15:20 +0000
I saw a news item this week about the 1.0 release of the Ruby Sinatra webapp library:
Sinatra 1.0 Released: Major Milestone for Ruby's Best Webapp DSL
I had heard of Sinatra, but I had been under the impression that it was a framework analagous to Ruby on Rails or the Django framework for Python. What caught my eye in the title was the reference to a "Webapp DSL". I was curious to find out if Sinatra did indeed expose a Domain-Specific Language that would allow one to quickly write web applications.
After reading the article, I was amazed to find that Sinatra applications are defined by using Ruby extensions. I didn't see any reference to any YAML or XML or anything like that.
First, I needed to download and install the Sinatra gem. I have been pretty happy with JRuby so far, so I tried to install the gem under JRuby instead of the native MRI for Windows.
It installed without any issues.
I then wrote a small Sinatra application that displays an HTML page with a link to this blog:
sinatra_test.rb
The invocation of the Sinatra get function looks like it pairs a URI with a Ruby block that yields text that is returned to the client browser. I wrote a few helper functions that generate some HTML markup around the text I intend to use.
By calling get with '/test' as the URI, the request for http://localhost:4567/test is routed to a handler that passes back the text from the block specified above.
I had to start the application before I could test this theory. A Sinatra application can be run just like any conventional Ruby script. It then provides its own web server to host itself as a web application:
I navigated to http://localhost:4567/test and saw my web page with a link to this blog:

Since the definition of a page was set up in just a handful of lines of Ruby code via a call to get, I reasoned that one could house many different pages in a single script. To try out this idea, I decided to put together a small web game consisting of a few static web pages and links to each. This game is akin to the "choose your own adventure" games usually found in the form of paperback books in the early 1980's.
cyoa.rb
To play the game, you must first run the cyoa.rb script. After the web-server starts up, you should navigate to localhost:4567/. That first page says:
You awaken in a cave. A cyclops holding a large club is standing over you.
What would you like to do?
Run for it!
Sing the cyclops a lullaby.
If you click Sing the cyclops a lullaby your browser will navigate to localhost:4567/lullaby. That page says:
The cyclops says 'Me no like Brahms!'.
He then hits you with his club. You lose consciousness.
Click here to play again
There are only a few pages in the adventure. If you click Run for it! from the first page and then click Go for a swim. after that, your character will end up safely at his home. All other paths lead to unconsciousness.
I have only used a very simple Sinatra function to make this skeletal game. I intend to delve into the library more deeply.
I suspect that this application will run with no changes under MRI Ruby, but I haven't tried that, yet.
Unless otherwise noted, all code and text entries are Copyright ©2010 by James K. Lawless
Save to del.icio.us
Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Subscribe!
Auto Save Clipboard Images Redux
Extending SpiderMonkey JavaScript on Windows
Rhino JavaScript to EXE with launch4j
Compiling Rhino JavaScript to Java
Directory Traversal in Rhino JavaScript
A Command-Line MP3 Player for Windows
Auto Save Images from the Clipboard
Java in a Windows EXE with launch4j
An Interview with Tom Zimmer: Forth System Developer
Setting Windows Console Text Colors in C
COM Scripting in C by way of JavaScript
Mad Schemes : Learning Lisp via SICP
PHP, Transparent GIF's, and Web Tracking
Compiling Rhino JavaScript to Java
Twimmando: A Command-line Twitter Client
Setting Windows Console Text Colors in C
Thwarting HTTP Referer Trackbacks
MicroISV on a Shoestring
DadHacker
The Bottom Feeder
Writin' That Code!
The Recursive ISV
The Thomsen Blog
Prototypically Speaking
The Reinvigorated Programmer