From 2e6908465f0fb7ec477cac4fd75cc18b65bb5922 Mon Sep 17 00:00:00 2001 From: drKreso Date: Mon, 17 Jun 2013 20:05:09 +0200 Subject: [PATCH 1/3] Add optino to use mulitple google engines while remaining bacward compatible --- lib/google_custom_search_api.rb | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/lib/google_custom_search_api.rb b/lib/google_custom_search_api.rb index 7c01ee7..0e6a9a0 100644 --- a/lib/google_custom_search_api.rb +++ b/lib/google_custom_search_api.rb @@ -8,11 +8,11 @@ module GoogleCustomSearchApi extend self - + ## # Search the site. # - # opts + # opts # see list here for valid options http://code.google.com/apis/customsearch/v1/using_rest.html#query-params def search(query, opts = {}) # Get and parse results. @@ -27,12 +27,12 @@ def search(query, opts = {}) fname = "google_#{query.gsub(/[^0-9A-Za-z]/, '_')}_#{opts[:start]}.json" file = File.join(file_path, fname) File.delete(file) if File.exist?(file) - open(file,'w') do |f|; f.puts results.to_json; end + open(file,'w') do |f|; f.puts results.to_json; end end ResponseData.new(results) end - + def search_and_return_all_results(query, opts = {}) res = [] opts[:start] ||= 1 @@ -49,11 +49,11 @@ def search_and_return_all_results(query, opts = {}) end while opts[:start].nil? == false return res end - + # def read_search_data(fname) # JSON.parse(File.read("/Users/wiseleyb/dev/rubyx/icm/spec/fixtures/searches/#{fname}.json")) # end - + # Convenience wrapper for the response Hash. # Converts keys to Strings. Crawls through all # member data and converts any other Hashes it @@ -62,7 +62,7 @@ def search_and_return_all_results(query, opts = {}) # to camel case. # # Usage: - # + # # rd = ResponseData.new("AlphaBeta" => 1, "Results" => {"Gamma" => 2, "delta" => [3, 4]}) # puts rd.alpha_beta # => 1 @@ -97,10 +97,10 @@ def method_missing(*args) end end end - - + + private # ------------------------------------------------------------------- - + ## # Build search request URL. # @@ -108,20 +108,27 @@ def method_missing(*args) def url(query, opts = {}) opts[:q] = query opts[:alt] ||= "json" + if opts[:engine_keys].nil? + api_key = GOOGLE_API_KEY + search_cx = GOOGLE_SEARCH_CX + else + api_key = options[:engine_keys]["GOOGLE_API_KEY"] + search_cx = options[:engine_keys]["GOOGLE_SEARCH_CX"] + end uri = Addressable::URI.new uri.query_values = opts begin params.merge!(GOOGLE_SEARCH_PARAMS) rescue NameError end - "https://www.googleapis.com/customsearch/v1?key=#{GOOGLE_API_KEY}&cx=#{GOOGLE_SEARCH_CX}&#{uri.query}" + "https://www.googleapis.com/customsearch/v1?key=#{api_key}&cx=#{search_cx}&#{uri.query}" end - + ## # Query Google, and make sure it responds. # def fetch(url) return HTTParty.get(url) end - + end From 63adaa4730276c5954851944d80385121f92230e Mon Sep 17 00:00:00 2001 From: drKreso Date: Mon, 17 Jun 2013 20:16:05 +0200 Subject: [PATCH 2/3] typo --- lib/google_custom_search_api.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/google_custom_search_api.rb b/lib/google_custom_search_api.rb index 0e6a9a0..82256e5 100644 --- a/lib/google_custom_search_api.rb +++ b/lib/google_custom_search_api.rb @@ -112,8 +112,8 @@ def url(query, opts = {}) api_key = GOOGLE_API_KEY search_cx = GOOGLE_SEARCH_CX else - api_key = options[:engine_keys]["GOOGLE_API_KEY"] - search_cx = options[:engine_keys]["GOOGLE_SEARCH_CX"] + api_key = opts[:engine_keys]["GOOGLE_API_KEY"] + search_cx = opts[:engine_keys]["GOOGLE_SEARCH_CX"] end uri = Addressable::URI.new uri.query_values = opts From 38069f1d1115d87b13e80ddf27832e861fdf83af Mon Sep 17 00:00:00 2001 From: drKreso Date: Mon, 17 Jun 2013 20:21:57 +0200 Subject: [PATCH 3/3] Fix --- lib/google_custom_search_api.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/google_custom_search_api.rb b/lib/google_custom_search_api.rb index 82256e5..e06a18d 100644 --- a/lib/google_custom_search_api.rb +++ b/lib/google_custom_search_api.rb @@ -114,6 +114,7 @@ def url(query, opts = {}) else api_key = opts[:engine_keys]["GOOGLE_API_KEY"] search_cx = opts[:engine_keys]["GOOGLE_SEARCH_CX"] + opts.delete(:engine_keys) end uri = Addressable::URI.new uri.query_values = opts