Searching Google Using SOAP <br />Over the past few years, the SOAP standard has gathered a lot of support, in part because it is now officially documented by the World Wide Web Consortium (W3C). Standardization makes working with most SOAP services dependable, at least as far as the information you need to know and the information you can reasonably expect to get back. Because of the W3C backing, it could be said that SOAP is now the preferred architecture for most web services.<br />Creating a SOAP client takes four simple steps:<br /><ul><li> Create an instance of a SOAP driver
Use the results in your Rails application</li></li></ul><li>To use the Google search API (and to test the sample code), you'll need a free Google Developer's Key, which you can get directly from Google at <br />https://www.google.com/accounts/NewAccount?continue=http://api.google.com/createkey&followup=http://api.google.com/createkey.<br />
Here's a controller that uses SOAP to find the first three results from a Google search. Update the code_controller.rb file from the previous example to contain this new googletest method:<br />class CodeController < ApplicationController<br /> def googletest<br />yourkey = 'YOUR GOOGLE DEVELOPER KEY' # Your Google dev key<br /> @yourquery = 'SEARCH TEXT' # Search value<br /> XSD::Charset.encoding = 'UTF8' # Set encoding for response<br />googleurl = "http://api.google.com/search/beta2"<br /> urn = "urn:GoogleSearch"<br /> driver = SOAP::RPC::Driver.new(googleurl, urn) # Create our driver<br />driver.add_method('doGoogleSearch', 'key', 'q', # Set up methods we'll call<br /> 'start', 'maxResults', 'filter', 'restrict',<br /> 'safeSearch', 'lr', 'ie', 'oe')<br /> @result = driver.doGoogleSearch(yourkey, # make our SOAP request<br /> @yourquery, 0, 3, false, '', false, '', '', '')<br /> end<br />end<br />
Here's the code for the view. Save it as googletest.rhtml in your app/views/code/ directory:<br /> Query for: <%= @yourquery %><br><br /> Found: <%= @result.estimatedTotalResultsCount %><br><br /> Query took about <%= @result.searchTime %> seconds<br><br /> <% @result.resultElements.each do |rec| %><br /> <b>Title:</b> <%= rec["title"] %><br><br /> <b>Summary:</b> <%= rec.snippet %><br><br /> <b>Link:</b> <a href="<%= rec["URL"] %>"><%= rec["URL"] %></a><br /> <br><br><br /> <% end %><br />
A particular slide catching your eye?
Clipping is a handy way to collect important slides you want to go back to later.