Git Product home page Git Product logo

google_custom_search's Introduction

Google Custom Search

This project is a Ruby API to Google’s Custom Search Engine (www.google.com/cse).

If you want a Google-like search engine for your web site, why not use Google? For $100/yr (more if you have over 1,000 pages) you can get access to Google search results for your site in XML format. The google_custom_search gem helps you access this web service and publish the results on your site however you like (all covered under Google’s acceptable use policy).

Google Custom Search is currently compatible with Rails 2.x and Rails 3.

Install

As a Gem

Add to your Gemfile:

gem "google_custom_search"

and run at the command prompt:

bundle install

Or As a Plugin

rails plugin install git://github.com/alexreisner/google_custom_search.git

Configure

You must define a constant in your application called GOOGLE_SEARCH_CX. For example, if you’re using Rails, create a file config/initializers/google_custom_search.rb:

GOOGLE_SEARCH_CX = "..."

You can find the CX value for your custom search engine via the search control panel on Google’s site (click the “Get code” link and you’ll see a hidden “cx” field in the sample HTML form).

Optionally, you can set default Google search params, such as encoding, by setting up the GOOGLE_SEARCH_PARAMS hash in the same initializer:

GOOGLE_SEARCH_PARAMS = {
  :ie => 'utf8',
  :oe => 'utf8'
}

If you’re working outside of Rails you’ll also need some require statements:

require 'rubygems'
require 'rexml/document'
require 'google_custom_search'

Use

To perform a search:

results = GoogleCustomSearch.search("Hank Aaron")

The results variable is now a GoogleCustomSearch::ResultSet object:

results.total            # number of results (integer)
results.pages            # array of result objects
results.suggestion       # suggested search term, if any

Iterate through the results:

results.pages.each do |result|
  result.title           # result title
  result.url             # result URL
  result.description     # excerpt, with terms highlighted
end

Future

  • prevent NameError when GOOGLE_SEARCH_CX is missing: show nice msg

  • access to all data returned by Google

  • support for features of CSE free version

  • support for multiple CSEs in one app (GOOGLE_SEARCH_CX should be a hash)

Copyright © 2009-11 Alex Reisner, released under the MIT license

google_custom_search's People

Contributors

alexreisner avatar herrri avatar mlineen avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.