Green Ruby Programmer

This space will chart my progress through the world of Ruby programming. Hopefully, it will conclude with me becoming a master Ruby programmer. With a moniker like this one, I shall have plenty of motivation to make that sooner rather than later! My other favorite programming languages these days are Python and Java. I have known both of them for about a decade.

Sunday, May 14, 2006

Invoking Yahoo Search Services from Ruby

Looking for a way to access Yahoo Search web services from Ruby programs?


Premshree Pillai has written a very clean-looking Ruby API for Yahoo! Search Web Services.

It is published in a syntax-colored, pretty-printed programming listing on a Plone content management system (CMS).

Plone is written in Python. Python and Ruby are somewhat programming language rivals.

So it is kind of funny to see such a powerful, useful Ruby program hanging out on a Plone server.


I really have to hand it to Premshree. This is a very clean piece of programming.

The program, which was written in mid-2005, has no dependencies on any software that is not included in the standard Ruby 1.8 distribution. That means it can be used without any hassle at all.

The search functionality is implemented in a 2-layer hierarchy of classes. They are:
  1. Search class with the generic mechanisms common to all the searches
  2. Specific class for each different concrete search type


This is an excellent demonstration of the use of inheritance as a practical way to approach specialization in object-oriented programs.


Here are the classes that Premshree has defined for the specific types of searches on Yahoo:
  1. ImagesSearch
  2. NewsSearch
  3. RelatedSuggestion
  4. RelatedTags (My Web)
  5. SpellingSuggestion
  6. TagSearch (My Web)
  7. UrlSearch (My Web)
  8. VideoSearch
  9. WebSearch


All requests are sent using the simple REST protocol that Yahoo has defined for their services.

Here is an example of the easy-to-use API being told to carry out a search for videos on the subject of ruby.

# assume that MY_APP_ID has been set to your Yahoo application ID
video_search = VideoSearch.new(MY_APP_ID, 'ruby')
ruby_movies_dict = video_search.parse_results



The ruby_movies_dict will contain a hash with the results from the search in it. Simple!


The only flaw with it that I can see is that there are no RDoc comments included for the methods.

The classes have obvious purpose from their names and their superclasses.

However, the methods would really benefit from a sample piece of code, demonstrating an actual call to them - and sample results that could be returned from that call.

That would eliminate any questions about what to do with whatever comes back. Of course, one could simply look at the documentation over at the Yahoo Developer web site, which spells out the format all of all XML data returned by each service.

The results returned vary a little from service to service. So one should be visiting that site for the info anyway. Still, shortcuts to enlightenment by the author of a reusable piece of code are always appreciated.



I think it is a Ruby programming work of art!


Technorati Tags:
, , , ,


0 Comments:

Post a Comment

<< Home

Related pages and news

Add this blog to my Technorati Favorites!
Click here to join ruby_programming_group
Click to join ruby_programming_group