giovedì 28 giugno 2007

RESTful Web Services Example 2.1 - Yahoo! Web Search

Example 2.1 of the RESTful Web Services book shows a simple REST Web Service client, for the Yahoo! Web Search service.

Since the book has a detailed description of the client, I'll just show the code:
Chapter2 class>>yahooWebSearch: aString
| baseURI document term xml |
"Code taken from RESTful Web Services. Copyright © 2007 O'Reilly Media, Inc.
All rights reserved. Used with permission."
baseURI := 'http://api.search.yahoo.com/WebSearchService/V1/webSearch'.
term := aString encodeForHTTP.
xml := HTTPSocket httpGet: baseURI , '?appid=restbook&query=', term .
document := XMLDOMParser parseDocumentFrom: xml.
document ResultSet Result Title
do: [:element |
Transcript
show: (element contentString);
cr]

The code may be found on the RWS repository on SqueakSource and is included in the RWS-gc.2 package.

If you confront the Squeak code with the original Ruby code from the book, you'll notice a couple of differences. First of all, this method takes a single String argument, which can have either one search key or more than one, separated by spaces. So the #yahooWebSearch: method actually corresponds to the #print_page_titles method in the Ruby source.

Another difference may be seen on lines 5-6 of this code snippet. Where the Ruby example uses open-uri and #open():
require 'open-uri'
#...
xml = open(BASE_URI + "?appid=restbook&query=#{term}").read

# Parse the XML document into a data structure.
document = REXML::Document.new(xml)

I use the HTTPSocket class and its #httpGet: message. This message returns a stream that may be passed to XMLDOMParser>>parseDocumentFrom:. But the most glaring difference is on line 7: instead of the XPath expression used in the Ruby code, I use
document ResultSet Result Title do: [...]

that's because Squeak's XPath library, being just a 0.1.1 release, has some problems with all but the most simple XPath expressions. For this reason I concocted a small hack that allows me to navigate a DOM structure by sending messages whose selectors are the same as the XML nodes I'm interested into. In the future, either I'll build a plugin to connect to Libxml2, or I'll build a full-blown, pure-Squeak, higher-order-messaging-based system.

For the next post in this series, I'll show a Squeak version of the del.icio.us client described in chapter 2.

4 commenti:

Andrey Esipenko ha detto...

Thank you!

Could you please explain the meaning of 'ResultSet Result Title' messages?

I tested your example and it's working pretty fine. But i can't catch that hack. Can't find no implementers, no references to that capitalized selectors. What's the magic behind that?

Andrey Esipenko ha detto...

Well, i finally found appropriate class extensions to XMLElement and others in monticello snapshot. Not a quite obvious, but really fun on the other hand.

Steve Elkins ha detto...

At the moment there are 4 versions in the repository. Looks like RWS-gc.2.mcz is the one to get since the doesNotUnderstand: hack is missing from 3 and 4.

Anyway, I'm happy you're doing this (translating the code to Squeak). I'll be following along behind.

Steve Elkins ha detto...

Now I see! Loaded Pastell (mentioned in your next article) and now the latest version of RWS works. Thanks.