Ruby web service clients

Yesterday I was in a Barnes and Noble bookstore and I started browsing through the Ruby Cookbook by Carlson and Richardson.

Quick aside: I still can’t believe how quickly Barnes and Noble has descended from a decent bookstore into practically garbage, especially as far as software development is concerned. The number of development books is down to a tiny fraction of that a few years ago, certainly no more than 5%. I was so happy when a BN opened in Glastonbury near my (old) house and unfortunately it’s still the closest bookstore to me, but now it’s just depressing. Even leaving aside the drop in selection, as Malcolm Gladwell points out in The Tipping Point, context matters. If nothing else, the managers of the store in Glastonbury could help themselves considerably if they would just oil the stupid door hinges (it’s been literally years since this was done) and clean the bathrooms every once in a while. If there was a Borders anywhere near them I’d never go in that store again.

Bruce Tate in his book Beyond Java points out that Java developers rarely look outside of Java because the field keeps moving so quickly they feel they have to run as fast as they can just to keep up. I really understand that feeling. At the end of December of 2005 I decided I was going to learn Ruby and Rails. I spent the next couple of months really digging into them and made a lot of progress. Ruby is sufficiently different from Java that it’s not at all an easy transition, though, despite what Rubyists say, and I learned just enough to get stuck on a regular basis.

Eventually I had to go back to Java, partly because I understand it and partly because it’s still paying the bills. I really need to become good at Hibernate, Spring, JSF, Tapestry, etc, and they all take time. Consequently, around the middle of March I switched gears and began really digging into Hibernate again, as I’ve mentioned here on several occasions.

Still, the allure of Ruby is strong. I think I may be past the newbie stage, but I’m hardly any good at it yet. On a scale from 1 to 10, I’d give myself about a 4, and that mostly based on reading rather than experience. I did read the pickaxe book (Programming Ruby by Dave Thomas) and R4R (Ruby for Rails by David Black) as well as the RoR books Agile Web Development with Rails (Dave Thomas again, among others) and a couple of the O’Reilly Rough Cuts. In other words, I’ve read the background material but haven’t yet gone through more than a couple of the Ten Canonical Errors in each technology.

Anyway, I mentioned here that about a week ago I decided to write a quick web service client for my Rensselaer students. I used the Amazon web service, connected with the URL class in Java, and parsed the resulting DOM tree (not fun). So yesterday I was browsing through the Ruby Cookbook, as I said, and found this code from Recipe 16.1:

require 'amazon/search'

$AWS_KEY = 'Your AWS key goes here' # See below.
def price_books(keyword)
  req = Amazon::Search::Request.new($AWS_KEY)
  req.keyword_search(keyword, 'books', Amazon::Search::LIGHT) do |product|
    newp = product.our_price || 'Not available'
    usedp = product.used_price || 'not available'
    puts "#{product.product_name}: #{newp} new, #{usedp} used."
  end
end

price_books(‘ruby cookbook’)Wow. It depends on a third party library, but I found that easy to download and install. I tested it out using my RadRails editor and it worked like a charm, with all the product information already converted into a Product class.

Even more amazing is this little snippet from Recipe 16.7, Using a WSDL File to Make SOAP Calls Easier:

require 'soap/wsdlDriver'
wsdl = 'http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl'
driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver

puts "Stock price: %.2f" % driver.getQuote('TR')

Holy cow. That’s three lines, really, and you can invoke a web service without even having to download the WSDL file. I wrapped it in a class and tried various web services at Xmethods.net and it worked without a problem. Not only that, but the “soap/wsdlDriver” library is built into Ruby 1.8.4.

I don’t even need a comment here. I simply have to find time to build up some experience with Ruby, and then go back to Rails, definitely by the time the next version of the AWDR book comes out. I purchase the “beta book” version from the Pragmatic Programmers already, but I think the hard copy becomes available in the Fall.

This week’s activities

This week I'm spending time working through David Black's Ruby for Rails book (hereinafter referred to as R4R) and helping Will Provost with his courseware. I'm working through the labs and demos in his Intro Java course, now that they've been ported to Java 5.

I'm okay with the enhanced for loop, and with variable arguments, and am getting used to enums. Generics are just plain ugly, and the inheritance aspect of them is very awkward. I thought I was going to really like generics, but now that I'm getting used to a dynamic typing language like Ruby, it all seems so unnecessary.

Six months ago if you'd asked me about dynamically typed languages, I would have said they were too error-prone to be useful. In other words, I would have given the standard Java answer. Now that I've spent time with Ruby and with JavaScript (of all things, but thanks, Ajax), I'm starting to see why they work so well. I'm also beginning to understand why every SmallTalk developer I've ever met loved that language.

I really need to assemble a "Ruby Traps for Java Developers" page, though.

%d bloggers like this: