You want to use Googles web services to perform searches and grab their results within your Ruby application.
Google exposes a SOAP API to its search functionality, and some other miscellaneous methods like spellcheck. Call these methods with the SOAP client that comes with Rubys standard library:
$KCODE = u # This lets us parse UTF characters
require soap/wsdlDriver
class Google
@@key = JW/JqyXMzCsv7k/dxqR9E9HF+jiSgbDL
# Get a key at http://www.google.com/apis/
@@driver = SOAP::WSDLDriverFactory.
new(http://api.google.com/GoogleSearch.wsdl).create_rpc_driver
def self.search(query, options={})
@@driver.doGoogleSearch(
@@key,
query,
options[:offset] || 0,
options[:limit] || 10, # Note that this value cannot exceed 10
options[:filter] || true,
options[:restricts] || ,
options[:safe_search] || false,
options[:lr] || ,
options[:ie] || ,
options[:oe] ||
)
end
def self.count(query, options={})
search(query, options).estimatedTotalResultsCount
end
def self.spell(phrase)
@@driver.doSpellingSuggestion(@@key, phrase)
end
end
Here it is in action:
Google.count "Ruby Cookbook site:oreilly.com" # => 368 results = Google.search "Ruby Cookbook site:oreilly.com", :limit => 7 results.resultElements.size # => 7 results.resultElements.first["title"] # => "oreilly.com -- Online Catalog: Ruby Cookbook…" results.resultElements.first["URL"] # => "http://www.oreilly.com/catalog/rubyckbk/" results.resultElements.first["snippet"] # => "The Ruby Cookbook is a new addition to …" Google.spell "tis is te centence" # => "this is the sentence"
Each of the options defined in Google.search corresponds to an option in the Google search API.
Strings
Numbers
Date and Time
Arrays
Hashes
Files and Directories
Code Blocks and Iteration
Objects and Classes8
Modules and Namespaces
Reflection and Metaprogramming
XML and HTML
Graphics and Other File Formats
Databases and Persistence
Internet Services
Web Development Ruby on Rails
Web Services and Distributed Programming
Testing, Debugging, Optimizing, and Documenting
Packaging and Distributing Software
Automating Tasks with Rake
Multitasking and Multithreading
User Interface
Extending Ruby with Other Languages
System Administration