Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 7e8bba1293adc13adf4fecf6d3f881512b5f27b4 @alexreisner committed Oct 14, 2009
Showing with 305 additions and 0 deletions.
  1. +5 −0 .gitignore
  2. +8 −0 CHANGELOG.rdoc
  3. +20 −0 LICENSE
  4. +67 −0 README.rdoc
  5. +56 −0 Rakefile
  6. +1 −0 VERSION
  7. +51 −0 google_custom_search.gemspec
  8. +1 −0 init.rb
  9. +86 −0 lib/google_custom_search.rb
  10. +6 −0 test/google_custom_search_test.rb
  11. +4 −0 test/test_helper.rb
5 .gitignore
@@ -0,0 +1,5 @@
+*.sw?
+.DS_Store
+coverage
+rdoc
+pkg
8 CHANGELOG.rdoc
@@ -0,0 +1,8 @@
+= Changelog
+
+Per-release changes to Handler.
+
+
+== 0.8.0 (2009 Oct 12)
+
+First release.
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2009 Alex Reisner
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
67 README.rdoc
@@ -0,0 +1,67 @@
+= Google Custom Search
+
+Ruby API to Google Custom Search Engine (http://www.google.com/cse). Works with the paid version of CSE where you get results in XML format.
+
+
+== 1. Install
+
+Install either as a Rails plugin:
+
+ script/plugin install git://github.com/alexreisner/google_custom_search.git
+
+or as a gem:
+
+ # add to config/environment.rb:
+ config.gem "google_custom_search", :source => "http://gemcutter.org/"
+
+ # at command prompt:
+ sudo rake gems:install
+
+or as a standalone gem (outside of Rails):
+
+ sudo gem install google_custom_search --source http://gemcutter.org
+
+
+== 2. Configure
+
+Firstly, you MUST define a constant in your application called <tt>GOOGLE_SEARCH_CX</tt>. For example, if you're using Rails, create a file <tt>config/initializers/google_custom_search.rb</tt>:
+
+ 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).
+
+If you're working outside of Rails you'll also need some +require+ statements:
+
+ require 'rubygems'
+ require 'rexml/document'
+ require 'google_custom_search'
+
+
+== 3. Use
+
+To perform a search:
+
+ results = GoogleCustomSearch.search("Hank Aaron")
+
+The +results+ variable is now a GoogleCustomSearch::ResultSet object:
+
+ results.total # 5080
+ results.pages # array of GoogleCustomSearch::Result objects
+ results.suggestion # string with suggested search term, if any
+
+Iterate through the results:
+
+ results.pages.each do |r|
+ r.title # page title
+ r.url # page URL
+ r.description # Google's excerpt, with terms highlighted
+ end
+
+
+== Future
+
+* access to all data returned by Google
+* support for features of CSE free version
+
+
+Copyright (c) 2009 Alex Reisner, released under the MIT license
56 Rakefile
@@ -0,0 +1,56 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "google_custom_search"
+ gem.summary = %Q{Ruby API to Google Custom Search Engine.}
+ gem.description = %Q{Ruby API to Google Custom Search Engine. Works with the paid version of CSE where you get results in XML format.}
+ gem.email = "alex@alexreisner.com"
+ gem.homepage = "http://github.com/alexreisner/google_custom_search"
+ gem.authors = ["Alex Reisner"]
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
+end
+
+require 'rake/testtask'
+Rake::TestTask.new(:test) do |test|
+ test.libs << 'lib' << 'test'
+ test.pattern = 'test/**/*_test.rb'
+ test.verbose = true
+end
+
+begin
+ require 'rcov/rcovtask'
+ Rcov::RcovTask.new do |test|
+ test.libs << 'test'
+ test.pattern = 'test/**/*_test.rb'
+ test.verbose = true
+ end
+rescue LoadError
+ task :rcov do
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
+ end
+end
+
+task :test => :check_dependencies
+
+task :default => :test
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ if File.exist?('VERSION')
+ version = File.read('VERSION')
+ else
+ version = ""
+ end
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "Google Custom Search #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
1 VERSION
@@ -0,0 +1 @@
+0.3.0
51 google_custom_search.gemspec
@@ -0,0 +1,51 @@
+# Generated by jeweler
+# DO NOT EDIT THIS FILE
+# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{google_custom_search}
+ s.version = "0.3.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Alex Reisner"]
+ s.date = %q{2009-10-14}
+ s.description = %q{Ruby API to Google Custom Search Engine. Works with the paid version of CSE where you get results in XML format.}
+ s.email = %q{alex@alexreisner.com}
+ s.extra_rdoc_files = [
+ "LICENSE",
+ "README.rdoc"
+ ]
+ s.files = [
+ ".gitignore",
+ "CHANGELOG.rdoc",
+ "LICENSE",
+ "README.rdoc",
+ "Rakefile",
+ "VERSION",
+ "google_custom_search.gemspec",
+ "init.rb",
+ "lib/google_custom_search.rb",
+ "test/google_custom_search_test.rb",
+ "test/test_helper.rb"
+ ]
+ s.homepage = %q{http://github.com/alexreisner/google_custom_search}
+ s.rdoc_options = ["--charset=UTF-8"]
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.3.5}
+ s.summary = %q{Ruby API to Google Custom Search Engine.}
+ s.test_files = [
+ "test/google_custom_search_test.rb",
+ "test/test_helper.rb"
+ ]
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
1 init.rb
@@ -0,0 +1 @@
+require 'google_custom_search'
86 lib/google_custom_search.rb
@@ -0,0 +1,86 @@
+##
+# Add search functionality (via Google Custom Search). Protocol reference at:
+# http://www.google.com/coop/docs/cse/resultsxml.html
+#
+module GoogleCustomSearch
+
+ ##
+ # Quick Struct-based class to hold a collection of search result data.
+ #
+ class ResultSet < Struct.new(:total, :pages, :suggestion); end
+
+ ##
+ # Quick Struct-based class to hold data for a single search result.
+ #
+ class Result < Struct.new(:url, :title, :description); end
+
+ ##
+ # Search the site.
+ #
+ def self.search(query, offset = 0, length = 20)
+
+ # Get and parse results.
+ url = url(query, offset, length)
+ return nil unless xml = fetch_xml(url)
+ data = Hash.from_xml(xml)['GSP']
+
+ # Extract and return search result data, if exists.
+ if data['RES']
+ ResultSet.new(
+ data['RES']['M'].to_i, # total
+ parse_results(data['RES']['R']), # pages
+ data['SPELLING'] ? data['SPELLING']['SUGGESTION'] : nil # suggestion
+ )
+ else
+ ResultSet.new(0, [], nil)
+ end
+ end
+
+
+ private # -------------------------------------------------------------------
+
+ ##
+ # Build search request URL.
+ #
+ def self.url(query, offset = 0, length = 20)
+ params = {
+ :q => query,
+ :start => offset,
+ :num => length,
+ :client => "google-csbe",
+ :output => "xml_no_dtd",
+ :cx => GOOGLE_SEARCH_CX
+ }
+ "http://www.google.com/search?" + params.to_query
+ end
+
+ ##
+ # Query Google, and make sure it responds.
+ #
+ def self.fetch_xml(url)
+ begin
+ resp = nil
+ timeout(3) do
+ resp = Net::HTTP.get_response(URI.parse(url))
+ end
+ rescue SocketError, TimeoutError; end
+ (resp and resp.code == "200") ? resp.body : nil
+ end
+
+ ##
+ # Transform an array of Google search results (XML parsed by REXML) into
+ # a more useful format.
+ #
+ def self.parse_results(results)
+ out = []
+ results = [results] if results.is_a?(Hash) # no array if only one result
+ results.each do |r|
+ out << Result.new(
+ r['U'], # url
+ r['T'].sub(/ \[[^\]]*\]$/, ''), # title
+ r['S'].gsub('<br>', '') # desciption
+ )
+ end
+ out
+ end
+end
6 test/google_custom_search_test.rb
@@ -0,0 +1,6 @@
+require 'test_helper'
+
+class GoogleCustomSearchTest < ActiveSupport::TestCase
+
+end
+
4 test/test_helper.rb
@@ -0,0 +1,4 @@
+require 'rubygems'
+require 'test/unit'
+require 'google_custom_search'
+

0 comments on commit 7e8bba1

Please sign in to comment.