A Google Wave robot container in Sinatra

Do you like this?

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!


Tagged In: , , , ,
Interested in learning more?  Sign up for our monthly webinar.
  • Paul: I'm glad it's working for you! The template doesn't have specific support for forms, but you can use the client library to do all of that. In your case, you would create a method in robot.rb called ROBOT_ADDED (or whatever the event is actually called, I've forgotten) and in that method you put your form creation code. If you need more specific help, send me an email
  • paul
    Hi mike, thanks for this great post. I have tried applying it and it worked. I have a question, is it possible to add a form elements using jruby or ruby when it gets loaded in a wave. For example when the robot got added in a wave, it will automatically create a form elements such as a textbox/textfield and then capture the events of that form? Thanks in advance.
  • Apparently it's no longer working?
  • Good to hear, Kevin, thanks. Let me know if you have any trouble getting it off the ground!
  • Kevin Radcliffe
    Very cool, thanks for the info (as well as the hurdles you encountered). Was going to try Python API, but being able to use Ruby is too tempting.
blog comments powered by Disqus