Wednesday, September 15, 2010

REXML and WADL issues in Chapter 2 of OReilly's RESTful Webservicesw

So I am teaching a course on RESTful webservices and using Sam Ruby and Leonard Richardson's OReilly book as the main text. It's a great book, but things have probably changed in a few Ruby libraries since the book's publication in 2008, so a couple of examples in chapter 2 seem to have some problems. I am on OSX 10.6.4 using ruby 1.8.7

The first issue was:
$ ruby delicious-sax.rb tansaku
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/source.rb:227:in `position': undefined method `stat' for # (NoMethodError)
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/parsers/baseparser.rb:144:in `position'
from /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/rexml/parsers/sax2parser.rb:176:in `parse'
from delicious-sax.rb:21:in `print_my_recent_bookmarks'
from delicious-sax.rb:30
I had been searching around for anything on this error, including looking at the source, but had made no progress. I tried to subscribe to the REXML mailing list described here:

http://www.germane-software.com/software/rexml/index.html#id2248285

but got no response, so thought I would try ruby-talk. Unfortunately while I am subscribed to ruby-talk and don't seem to be able to unsubscribe I don't seem to be able to post there. I found at least a partial resolution by chatting with capnregx on ruby-lang irc at Freenode. The solution was to call xml.string to dump out a String object rather than passing in a StringIO which the REXML I am using is, somewhat surprisingly, unable to handle.

I also found a problem in the same chapter with another example involving WADL:
$ ruby delicious-wadl-ruby.rb tansaku
./wadl.rb:1248:in `lookup_param': No such param post (ArgumentError)
from ./wadl.rb:1255:in `each_by_param'
from delicious-wadl-ruby.rb:22
I took a brief look at the source and didn't have any immediate insight. After some more support from capnregex I identified a good approach of running the entire program in irb to see what was being passed back from the server. This is the annoying thing about del.icio.us as a programming example, since it is all over https, so no tracing it with tcpdump.

I got as far as seeing that the appropriate data did seem to be returned, but was residing in get.format rather than get.representation which was a REXML:Element rather than a WADL::XMLRepresentation as one would intuitively expect from the 'each_with_params' method call. get.headers.string appeared to contain the desired XML but not in a format (StringIO) that could be parsed the way the program was attempting. Tabbing on get.headers. suggested that each_by_param was an appropriate method, but gave an undefined method error so I gave up there and decided to drop the WADL example for the moment. We'll come back to WADL in a later chapter, but I am becoming increasingly dubious about it's value. At least the REST community does not seem to have sprung into action around it after the effort the book's authors put in to getting it off the ground.

All files required to run the above are in pastebin:

delicious-sax.rb http://pastebin.com/029SnVEW

delicious-wadl-ruby.rb: http://pastebin.com/Nz5hFGRN
wadl.rb (required by above): http://pastebin.com/AKRAETG8
delicious.xml (required by above): http://pastebin.com/7hUH7uwy

Of course you will need to use your own deli.icio.us username/password combinations for the examples to work :-)

2 comments:

Robert Brewer said...

Have you checked out the errata page for the book? Perhaps some of these are listed there, or at least you'll have some warning of upcoming potholes:

http://oreilly.com/catalog/errata.csp?isbn=9780596529260

Sam Joseph said...

I had checked previously - no joy there ...