Class: RubyWebSearch::Google::Query
- Inherits:
-
Object
- Object
- RubyWebSearch::Google::Query
- Defined in:
- lib/ruby-web-search.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- SEARCH_BASE_URLS =
{ :web => "http://ajax.googleapis.com/ajax/services/search/web", :local => "http://ajax.googleapis.com/ajax/services/search/local", :video => "http://ajax.googleapis.com/ajax/services/search/video", :blog => "http://ajax.googleapis.com/ajax/services/search/blogs", :news => "http://ajax.googleapis.com/ajax/services/search/news", :book => "http://ajax.googleapis.com/ajax/services/search/books", :image => "http://ajax.googleapis.com/ajax/services/search/images", :patent => "http://ajax.googleapis.com/ajax/services/search/patent" }
Instance Attribute Summary collapse
-
#country_code ⇒ Object
Returns the value of attribute country_code.
-
#cursor ⇒ Object
Returns the value of attribute cursor.
-
#custom_request_url ⇒ Object
Returns the value of attribute custom_request_url.
-
#custom_search_engine_id ⇒ Object
Returns the value of attribute custom_search_engine_id.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#language_code ⇒ Object
Returns the value of attribute language_code.
-
#query ⇒ Object
Returns the value of attribute query.
-
#referer ⇒ Object
Returns the value of attribute referer.
-
#request_url ⇒ Object
Returns the value of attribute request_url.
-
#response ⇒ Object
Returns the value of attribute response.
-
#result_size ⇒ Object
Returns the value of attribute result_size.
-
#safe_search ⇒ Object
Returns the value of attribute safe_search.
-
#size ⇒ Object
Returns the value of attribute size.
-
#start_index ⇒ Object
Returns the value of attribute start_index.
-
#type ⇒ Object
Returns the value of attribute type.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
- #build_request ⇒ Object
- #build_requests ⇒ Object
-
#execute ⇒ Object
Makes the request to Google if a larger set was requested than what is returned, more requests are made until the correct amount is available.
-
#execute_unthreaded ⇒ Object
Makes the request to Google if a larger set was requested than what is returned, more requests are made until the correct amount is available.
-
#initialize(options = {}) ⇒ Query
constructor
You can overwrite the query building process by passing the request url to use.
Constructor Details
#initialize(options = {}) ⇒ Query
You can overwrite the query building process by passing the request url to use.
Params
query<String>
start_index<Integer>
size<Integer> number of results default: 4
filter
country_code<String> 2 letters language code for the country you want
to limit to
language_code<String> (Web only)
safe_search<String> active, moderate or off. Default: active (web only)
custom_search_engine_id<String> optional argument supplying the unique id for
the Custom Search Engine that should be used for the request (e.g., 000455696194071821846:reviews).
(web only)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/ruby-web-search.rb', line 63 def initialize(={}) if [:custom_request_url] @custom_request_url = [:request_url] else @query = [:query] raise Google::Query::Error, "You need to pass a query" unless @query @cursor = [:start_index] || 0 @result_size = [:result_size] @filter = [:filter] @type = [:type] || :web @country_code = [:country_code] @language_code = [:language_code] ? "lang_#{[:language_code]}" : nil @safe_search = [:safe_search] @custom_search_engine_id = [:custom_search_engine_id] @version = [:version] || "1.0" @referer = [:referer] || "http://github.com/mattetti/" @size = [:size] || 4 @result_size = "large" if size > 4 # increase the result set size to avoid making too many requests @size = 8 if (@result_size == "large" && size < 8) end @response ||= Response.new(:query => (query || custom_request_url), :size => size) end |
Instance Attribute Details
#country_code ⇒ Object
Returns the value of attribute country_code.
31 32 33 |
# File 'lib/ruby-web-search.rb', line 31 def country_code @country_code end |
#cursor ⇒ Object
Returns the value of attribute cursor.
33 34 35 |
# File 'lib/ruby-web-search.rb', line 33 def cursor @cursor end |
#custom_request_url ⇒ Object
Returns the value of attribute custom_request_url.
33 34 35 |
# File 'lib/ruby-web-search.rb', line 33 def custom_request_url @custom_request_url end |
#custom_search_engine_id ⇒ Object
Returns the value of attribute custom_search_engine_id.
32 33 34 |
# File 'lib/ruby-web-search.rb', line 32 def custom_search_engine_id @custom_search_engine_id end |
#filter ⇒ Object
Returns the value of attribute filter.
31 32 33 |
# File 'lib/ruby-web-search.rb', line 31 def filter @filter end |
#language_code ⇒ Object
Returns the value of attribute language_code.
31 32 33 |
# File 'lib/ruby-web-search.rb', line 31 def language_code @language_code end |
#query ⇒ Object
Returns the value of attribute query.
31 32 33 |
# File 'lib/ruby-web-search.rb', line 31 def query @query end |
#referer ⇒ Object
Returns the value of attribute referer.
32 33 34 |
# File 'lib/ruby-web-search.rb', line 32 def referer @referer end |
#request_url ⇒ Object
Returns the value of attribute request_url.
32 33 34 |
# File 'lib/ruby-web-search.rb', line 32 def request_url @request_url end |
#response ⇒ Object
Returns the value of attribute response.
33 34 35 |
# File 'lib/ruby-web-search.rb', line 33 def response @response end |
#result_size ⇒ Object
Returns the value of attribute result_size.
31 32 33 |
# File 'lib/ruby-web-search.rb', line 31 def result_size @result_size end |
#safe_search ⇒ Object
Returns the value of attribute safe_search.
32 33 34 |
# File 'lib/ruby-web-search.rb', line 32 def safe_search @safe_search end |
#size ⇒ Object
Returns the value of attribute size.
33 34 35 |
# File 'lib/ruby-web-search.rb', line 33 def size @size end |
#start_index ⇒ Object
Returns the value of attribute start_index.
31 32 33 |
# File 'lib/ruby-web-search.rb', line 31 def start_index @start_index end |
#type ⇒ Object
Returns the value of attribute type.
32 33 34 |
# File 'lib/ruby-web-search.rb', line 32 def type @type end |
#version ⇒ Object
Returns the value of attribute version.
32 33 34 |
# File 'lib/ruby-web-search.rb', line 32 def version @version end |
Instance Method Details
#build_request ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ruby-web-search.rb', line 86 def build_request if custom_request_url custom_request_url else @request_url = "#{SEARCH_BASE_URLS[type]}?v=#{version}&q=#{CGI.escape(query)}" @request_url << "&rsz=#{result_size}" if result_size @request_url << "&start=#{cursor}" if cursor > 0 @request_url << "&lr=#{language_code}" if language_code @request_url << "&hl=#{country_code}" if country_code puts request_url if $RUBY_WEB_SEARCH_DEBUG request_url end end |
#build_requests ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/ruby-web-search.rb', line 101 def build_requests if custom_request_url requests = [custom_request_url] else requests = [] # create an array of requests based on the fact that google limits # us to 8 responses per request but let us use a cursor (size / 8.to_f).ceil.times do |n| url = "#{SEARCH_BASE_URLS[type]}?v=#{version}&q=#{CGI.escape(query)}" url << "&rsz=#{result_size}" if result_size url << "&lr=#{language_code}" if language_code url << "&hl=#{country_code}" if country_code url << "&start=#{cursor}" @cursor += 8 requests << url end puts requests.inspect if $RUBY_WEB_SEARCH_DEBUG requests end end |
#execute ⇒ Object
Makes the request to Google if a larger set was requested than what is returned, more requests are made until the correct amount is available
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/ruby-web-search.rb', line 145 def execute threads = build_requests.map do |req| Thread.new do curl_request = ::Curl::Easy.new(req){ |curl| curl.headers["Referer"] = referer } curl_request.perform JSON.load(curl_request.body_str) end end threads.each do |t| response.process(t.value) end response.limit(size) end |
#execute_unthreaded ⇒ Object
Makes the request to Google if a larger set was requested than what is returned, more requests are made until the correct amount is available
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ruby-web-search.rb', line 126 def execute_unthreaded @curl_request ||= ::Curl::Easy.new(){ |curl| curl.headers["Referer"] = referer } @curl_request.url = build_request @curl_request.perform results = JSON.load(@curl_request.body_str) response.process(results) @cursor = response.results.size - 1 if ((cursor + 1) < size && custom_request_url.nil?) puts "cursor: #{cursor} requested results size: #{size}" if $RUBY_WEB_SEARCH_DEBUG execute_unthreaded else response.limit(size) end end |