A Google Wave robot container in Sinatra

A Google Wave robot container in Sinatra

Since I got a Google Wave account at Google IO, I felt like I should try to do something Ruby-ish in it.  I got talking to Charles Nutter, and he suggested a Sinatra template for Wave Robots.  So here it is.

I stole shamelessly from Tim Morgan (http://github.com/seven1m) and Sam Ruby (http://github.com/rubys) to make this work.  In the end you only have to change one code file, and I think it’s pretty nice!  As in the previous blog post on AppEngine, you need to get Jruby and the AppEngine SDK:

http://dist.codehaus.org/jruby/1.3.0RC2/jruby-bin-1.3.0RC2.zip

http://code.google.com/appengine/downloads.html

Grab the Sintra template from github:  http://github.com/MikeSofaer/Wave-Robot-Sinatra-Template/tree/master

cd into the war/WEB-INF directory, and edit robot.rb:


class Robot < AbstractRobot
  set_name "Sinatra Bot"
  add_cron :clock, 20

  def DOCUMENT_CHANGED(properties, context)
    wavelet = context.GetWavelets[0]
    blip = context.GetBlipById(wavelet.GetRootBlipId())
    blip.GetDocument.SetText('Only I get to edit the top blip!')
  end
  def clock(event, context)
    wavelet = context.GetWavelets[0]
    blip = context.GetBlipById(wavelet.GetRootBlipId())
    blip.GetDocument.SetText("It's " + Time.now.to_s)
  end
end

Set the app name in appengine-web.xml

appcfg update ..

You are done!  The robot will set the time in the root blip, and get very snippy with you if you try to edit it.

Probably the nastiest thing I hit on the way was that jruby-rack likes to take the request body JSON and replace all the + characters with spaces, which in this case was a Very Bad Thing.  Eventually I found that you can call request.env['rack.request.form_vars'] to get the original JSON back.

Enjoy!

Related Posts Plugin for WordPress, Blogger...