Add Google Custom Search to your Rails Application (Business Edition)

Posted by anup.narkhede on July 17, 2008

Here are the steps to set up a Google CSE for your rails application. In order to use the xml api, first of all you need to have a business account. You can get the xml query format url or more precisely the cx parameter (unique ID of the CSE) from the control panel.

Setup:

Identify your resource to be searched with the help of url patterns. For ex, if the application is RESTful, it is easy to index something like http://someshop.com/products. Configure your CSE for the above url pattern so that http://someshop.com/products/1, http://someshop.com/products/2.. etc get indexed by the search engine.

Code:

The GoogleSearch class sends a GET request to fetch the results xml, which is parsed using the Hpricot parser.

Output:

Now you could get the urls of search results as an array. Likewise, you can fetch other parameters from resultant xml by referring the this doc..