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
Views expressed in this blog are those of the author and do not necessary reflect those of the author's employer. Views expressed in the comments are those of the responding individual.

Save to StumbleUpon
Digg it
Save to Reddit
Share on Facebook
Share on Twitter
More bookmarks
Compiling C from the Command Line with Pelles C
A Forthcoming Marvel Movie Villain
Uninstalling Problematic Windows Software
A JavaScript REPL for Android Devices
An SMTP Server Simulator in Perl
A Scrolling Banner using Canvas and JavaScript
Scott Ballantyne: Blazin' Into Forth
TAP : A Command Processor Library
A Review of Kevin Mitnick's Book Ghost in the Wires
My First C64 Interrupt Program
Playing with OS/X Text to Speech
The Villain at the end of Marvel's Avengers Move is...
Processing GMail items with Ruby
Happy 30th Birthday, Commodore 64
MailWrench CSharp Command Line Mailer for Windows is now Free Software
Rainy Day Fun with the HTML DOM
Building a World War II Foxhole Radio
Prototerp – Unleashing a JavaScript REPL via the Mobile Browser