Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Label support.

  • Loading branch information...
commit 700119559746f3973d8eac69a9e83d418524298c 1 parent a10f4df
@jasiek authored
View
39 lib/google_custom_search.rb
@@ -6,17 +6,10 @@
require 'active_support/core_ext'
module GoogleCustomSearch
- extend self
-
- ##
- # Quick Struct-based class to hold a collection of search result data.
- #
- class ResultSet < Struct.new(:total, :pages, :suggestion, :labels); end
+ autoload :ResultSet, 'result_set'
+ autoload :Result, 'result'
- ##
- # Quick Struct-based class to hold data for a single search result.
- #
- class Result < Struct.new(:url, :title, :description); end
+ extend self
##
# Search the site.
@@ -30,17 +23,12 @@ def search(query, offset = 0, length = 20)
# 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
- )
+ ResultSet.create(data)
else
- ResultSet.new(0, [], nil)
+ ResultSet.create_empty()
end
end
-
private # -------------------------------------------------------------------
##
@@ -74,21 +62,4 @@ def fetch_xml(url)
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 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'].try(:sub, / \[[^\]]*\]$/, ''), # title
- r['S'].try(:gsub, '<br>', '') # desciption
- )
- end
- out
- end
end
View
22 lib/result.rb
@@ -0,0 +1,22 @@
+##
+# Single search result.
+#
+class GoogleCustomSearch::Result
+ attr_reader :url, :title, :description, :labels
+
+ def self.parse(res_r)
+ res_r = [res_r] if res_r.is_a?(Hash)
+
+ res_r.collect do |r|
+ self.new(r['U'], # url
+ r['T'].try(:sub, / \[[^\]]*\]$/, ''), # title
+ r['S'].try(:gsub, '<br>', ''), # desciption
+ r['Label'].is_a?(String) ? [r['Label']] : r['Label']) # array of labels
+ end
+ end
+
+ def initialize(url, title, description, labels)
+ @url, @title, @description, @labels = url, title, description, labels
+ end
+end
+
View
36 lib/result_set.rb
@@ -0,0 +1,36 @@
+##
+# Simple class to hold a collection of search result data.
+#
+class GoogleCustomSearch::ResultSet
+ attr_reader :total, :pages, :suggestion, :labels
+
+ def self.create(xml_hash)
+ self.new(xml_hash['RES']['M'].to_i,
+ parse_results(xml_hash['RES']['R']),
+ spelling = xml_hash['SPELLING'] ? spelling['SUGGESTION'] : nil,
+ parse_labels(xml_hash))
+ end
+
+ def self.create_empty
+ self.new(0, [], nil, {})
+ end
+
+ def self.parse_results(res_r)
+ GoogleCustomSearch::Result.parse(res_r)
+ end
+
+ def self.parse_labels(xml_hash)
+ return {} unless context = xml_hash['Context'] and facets = context['Facet']
+ facets.inject({}) do |h, facet_item|
+ facet_item['FacetItem'].each do |element|
+ h[element['label']] = element['anchor_text']
+ end
+ h
+ end
+ end
+
+ def initialize(total, pages, suggestion, labels)
+ @total, @pages, @suggestion, @labels = total, pages, suggestion, labels
+ end
+end
+
View
20 test/google_custom_search_test.rb
@@ -2,8 +2,24 @@
class GoogleCustomSearchTest < ActiveSupport::TestCase
test "returns an array of results" do
- results = GoogleCustomSearch.search('science')
- assert(results.pages.size > 0)
+ results = GoogleCustomSearch.search('engineer')
+ assert(! results.pages.empty?)
+ assert(! results.labels.empty?)
+
+ label_set = results.pages.inject(Set[]) do |label_set, page|
+ assert(page.title.is_a?(String))
+ assert(! page.title.empty?)
+
+ assert(page.url.is_a?(String))
+ URI.parse(page.url)
+
+ assert(page.description.is_a?(String))
+ assert(! page.description.empty?)
+
+ label_set += page.labels
+ end
+
+ assert(! (label_set & results.labels.keys.to_set).empty?)
end
end
View
11 test/test_helper.rb
@@ -4,5 +4,12 @@
require 'test/unit'
require 'google_custom_search'
-# http://groups.google.com/a/googleproductforums.com/forum/#!category-topic/customsearch/implementation/EMyXtR5CV4E
-GOOGLE_SEARCH_CX = '014399221313847880480:c7jsxy-iymw'
+# Google Custom Search (Google Jobs) :)
+# GOOGLE_SEARCH_CX = '016766180714503271468:t099t-w3qse'
+
+# IBM Magazine
+# GOOGLE_SEARCH_CX = '003630504832992121813:duvzt8xkd_8'
+
+# fbi.gov
+
+GOOGLE_SEARCH_CX = '004748461833896749646:e41lgwqry7w'
Please sign in to comment.
Something went wrong with that request. Please try again.