Google::Search
require 'google' KEY = File.open("#{ENV['HOME']}/.google_key") {|kf| kf.readline.chomp} query = ARGV.shift || 'ruby programming language' google = Google::Search.new(KEY) i = 0 q = google.search(query) q.response['resultElements'].each do |result| printf "\nResult # %d\n\n", i += 1 result.each do |key| printf("%s = %s\n", key, result.attribute[key]) end end puts '---------------------------------' printf "Estimated number of results is %d.\n", q.response['estimatedTotalResultsCount'] printf "Your query took %6f seconds.\n", q.response['searchTime']
Google::Search allows you to programmatically query the Google search-engine at www.google.com. It is currently in the alpha stage and the interface is liable to change at any time.
Search.new(key)
This constructs a new Google search object. The key parameter is the key that Google assigned to you when you registered for the Web API download. If you don't yet have a key, go to Google and obtain one.
Search#search(query, start, max, filter, restrict, safe, lr, ie, oe)
This performs a standard Google query search. Only the query parameter is mandatory.
The meaning of the other parameters can be obtained from section 2.1 of the APIs_Reference.html file that came with your Google API archive.
This method returns a Google::Response object, the members of which are described in section 3.1 of APIs_Reference.html.
The resultElements element is an array of Google::ResultElement objects. Elements of the Google::ResultElement object are described in section 3.2 of APIs_Reference.html.
Search#spell(phrase)
This attempts to perform a Google spell-check on phrase. If Google has no spelling suggestions to make, nil is returned. Otherwise, a String is returned.
Search#cache(url)
This attempts to retrieve the cached Google copy of the page corresponding to url. If Google has not cached the URL in question, a page containing a message to this effect will be returned by the search-engine.
This method always returns a String.
Written by Ian Macdonald <ian@caliban.org>.
Copyright (c) 2002 Ian Macdonald This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
http://www.caliban.org/ruby http://www.google.com/
Send all bug reports, enhancement requests and patches to the author.
$Id: google.rb,v 1.9 2002/04/15 17:51:57 ianmacd Exp $