Monday, November 10th, 2008

yboss – a wrapper for Yahoo’s BOSS API

Category: Examples, JavaScript, JSON, Yahoo!

BOSS – Build Your Own Search Service (the your is silent for reasons I cannot tell you as it would endanger the lives of our agents in the field) is a Yahoo! API to access their search index and get the data back either as XML or JSON. Whilst there is ample documentation available it can still be a bit daunting to use the API purely in JavaScript – especially when you want to have several asynchronous calls – for example when you want to search the images, news and web sites for a certain query.

I’ve had several complaints of Hackers at the Open Hack Day in Brazil about this and wanted to make their lives easier by writing a wrapper for the API.

This wrapper is yboss and it was a big success at the Hack Day with the winning hack in the BOSS category actually being based on it.

To use the wrapper all you need to do is to embed it in your document

  1. <div id="results"></div>
  2. <script type="text/javascript" src="yboss-lib.js"></script>

Then you have access to the get method of the wrapper which searches all the defined search options with the query you provided. You can define a callback method that will be called when all searches have been successfully performed.

javascript

  1. YBOSS.get(
  2.   {
  3.     searches:'search,images,news',
  4.     query:'obama',
  5.     count:10,
  6.     callback:seeddata
  7.   }
  8. );
  9. function seeddata(o){
  10.  var all = '<h4>Web Sites</h4>' + o.webHTML +
  11.               '<h4>News</h4>' + o.newsHTML +
  12.              '<h4>Images</h4>' + o.imagesHTML;
  13.  var out = document.getElementById('results');
  14.  out.innerHTML = all;
  15. }

The data is provided either as a JSON object with all the mandatory properties for a BOSS result display or – as shown here – as HTML lists that can be written out via innerHTML.

Posted by Chris Heilmann at 6:06 pm
1 Comment

+++--
3.5 rating from 15 votes

1 Comment »

Comments feed TrackBack URI

Typo in the second code snippet, line 13: “vvar”.

Anything that makes API easier to use gets a thumbs up in my books.

Comment by Unfocused — November 10, 2008

Leave a comment

You must be logged in to post a comment.