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.

About Kenneth Kousen
I teach software development training courses. I specialize in all areas of Java and XML, from EJB3 to web services to open source projects like Spring, Hibernate, Groovy, and Grails. Find me at Google+ on Google+ I am the author of "Making Java Groovy", a Java / Groovy integration book published by Manning in the Fall of 2013.

7 Responses to Ruby web service clients

  1. martina biermann says:

    i also tried to incoke extisting webservices, by these lines, but I filed

    ———-
    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’)
    ———–

    with this error message:
    Hello World

    C:/ruby/lib/ruby/1.8/xsd/xmlparser/xmlparser.rb:31:in `parse': syntax error (XMLParserError)

    from C:/ruby/lib/ruby/1.8/xsd/xmlparser/xmlparser.rb:31:in `do_parse’

    from C:/ruby/lib/ruby/1.8/wsdl/parser.rb:65:in `parse’

    from C:/ruby/lib/ruby/1.8/wsdl/importer.rb:29:in `parse’

    from C:/ruby/lib/ruby/1.8/wsdl/xmlSchema/importer.rb:30:in `import’

    from C:/ruby/lib/ruby/1.8/wsdl/importer.rb:18:in `import’

    from C:/ruby/lib/ruby/1.8/soap/wsdlDriver.rb:124:in `import’

    from C:/ruby/lib/ruby/1.8/soap/wsdlDriver.rb:28:in `initialize’

    from C:/Dokumente und Einstellungen/Martina.Biermann/Eigene Dateien/NetBeansProjects/RailsApplication1/app/controllers/ws_test2.rb:14:in `new’

    from C:/Dokumente und Einstellungen/Martina.Biermann/Eigene Dateien/NetBeansProjects/RailsApplication1/app/controllers/ws_test2.rb:14

    —————–
    this is my environment:
    Ruby version 1.8.6 (i386-mswin32)
    RubyGems version 0.9.2
    Rails version 1.2.5
    Active Record version 1.15.5
    Action Pack version 1.13.5
    Action Web Service version 1.2.5
    Action Mailer version 1.3.5
    Active Support version 1.4.4
    Application root C:/Dokumente und Einstellungen/Martina.Biermann/Eigene Dateien/NetBeansProjects/RailsApplication1
    Environment development
    Database adapter mysql
    Database schema version 1

    Do you have an idea where the problem is? My ba ruby 1.8.6

    I am new at ruby and also without experience in webservices.

  2. Pingback: Polymathic » Blog Archive » Rails + SugarCRM + SOAP - Get a list of Sugar accounts into Rails as an array

  3. dr.rasputin says:

    simply remove the ; at the end of the last line

  4. Akshay Jangid says:

    Hi All,

    I have created a stand alone webservice tool to access any webservice using ruby.
    Just give the url , it will show you all the available method and let u access them.
    You need ruby gem Saop4r for it. It uses WSDL2Ruby as the core implementation.
    you can download my source code from below location :-
    http://docs.google.com/uc?id=0By8L7mvqAaq6YjNhNjcxZjMtMzA4OS00Zjc0LWJiMWQtNGEzYzdjNDBiMzU0&export=download&hl=en

    The attachment is a .jpg file.Change its extension to .zip and then unzip it.

    Read the main.rb file for more instructions….
    Email me at akshay.dce@gmail.com for feedback or any queries…..

    Regards,
    Akshay Jangid

  5. Mark Kauffman says:

    Re: “Barnes and Noble has descended from a decent bookstore into practically garbage, especially as far as software development is concerned” This is the result of free-market capitalism at work. Let me explain. I love computers and computer science. When I was in the 5th grade around 1968 I read a book titled “Danny Dunn and the Homework Machine,” and fell in love. I worked hard to get my Bachelors and Masters degrees and have been working in the field for 30+ years, through the time period when B&N had tons of good software books on the shelves. Those were the days when US companies were hiring US software engineers. Those days are gone, and for the most part, no one is buying those books any longer. You can buy a book on how to use your iPad, but there are not many on how it works. On the other hand, if you are going into a field like Psychology, you have lots of options at B&N. You’ll learn how to console disgruntled, underemployed software engineers.

  6. Mark Kauffman says:

    Oh – and thank you Ken for putting this up!

    Best regards, Mark

  7. Ken Kousen says:

    All so true, if disappointing. If there was a market for IT developer books, they’d sell the books. These days you can see lots of Android and iPhone/iPad dev books, but not much else.

    As someone who has always been fond of books, I find it sad, if not surprising.

    Thanks for your comment!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 1,153 other followers

%d bloggers like this: