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:
- Search class with the generic mechanisms common to all the searches
- 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:
- ImagesSearch
- NewsSearch
- RelatedSuggestion
- RelatedTags (My Web)
- SpellingSuggestion
- TagSearch (My Web)
- UrlSearch (My Web)
- VideoSearch
- 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:
programming, ruby, software, yahoo, searching
0 Comments:
Post a Comment
<< Home