Class: RubyWebSearch::Yahoo::Query
- Inherits:
-
Object
- Object
- RubyWebSearch::Yahoo::Query
- Defined in:
- lib/ruby-web-search.rb
Defined Under Namespace
Classes: Error
Constant Summary collapse
- SEARCH_BASE_URLS =
{ :web => "http://boss.yahooapis.com/ysearch/web", }
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#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.
-
#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>
api_key<String>
start_index<Integer>
size<Integer> number of results default: 10
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)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
# File 'lib/ruby-web-search.rb', line 239 def initialize(={}) if [:custom_request_url] @custom_request_url = [:request_url] else @query = [:query] raise Yahoo::Query::Error, "You need to pass a query" unless @query @cursor = [:start_index] || 0 @filter = [:filter] @type = [:type] || :web @country_code = [:country_code] @language_code = [:language_code] @safe_search = [:safe_search] @custom_search_engine_id = [:custom_search_engine_id] @version = [:version] || "1" @referer = [:referer] || "http://github.com/mattetti/" @api_key = [:api_key] raise Yahoo::Query::Error, "You need to pass an api key" unless @api_key @size = [:size] || 10 end @response ||= Response.new(:query => (query || custom_request_url), :size => size) end |
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
215 216 217 |
# File 'lib/ruby-web-search.rb', line 215 def api_key @api_key end |
#country_code ⇒ Object
Returns the value of attribute country_code.
213 214 215 |
# File 'lib/ruby-web-search.rb', line 213 def country_code @country_code end |
#cursor ⇒ Object
Returns the value of attribute cursor.
215 216 217 |
# File 'lib/ruby-web-search.rb', line 215 def cursor @cursor end |
#custom_request_url ⇒ Object
Returns the value of attribute custom_request_url.
215 216 217 |
# File 'lib/ruby-web-search.rb', line 215 def custom_request_url @custom_request_url end |
#custom_search_engine_id ⇒ Object
Returns the value of attribute custom_search_engine_id.
214 215 216 |
# File 'lib/ruby-web-search.rb', line 214 def custom_search_engine_id @custom_search_engine_id end |
#filter ⇒ Object
Returns the value of attribute filter.
213 214 215 |
# File 'lib/ruby-web-search.rb', line 213 def filter @filter end |
#language_code ⇒ Object
Returns the value of attribute language_code.
213 214 215 |
# File 'lib/ruby-web-search.rb', line 213 def language_code @language_code end |
#query ⇒ Object
Returns the value of attribute query.
213 214 215 |
# File 'lib/ruby-web-search.rb', line 213 def query @query end |
#referer ⇒ Object
Returns the value of attribute referer.
214 215 216 |
# File 'lib/ruby-web-search.rb', line 214 def referer @referer end |
#request_url ⇒ Object
Returns the value of attribute request_url.
214 215 216 |
# File 'lib/ruby-web-search.rb', line 214 def request_url @request_url end |
#response ⇒ Object
Returns the value of attribute response.
215 216 217 |
# File 'lib/ruby-web-search.rb', line 215 def response @response end |
#safe_search ⇒ Object
Returns the value of attribute safe_search.
214 215 216 |
# File 'lib/ruby-web-search.rb', line 214 def safe_search @safe_search end |
#size ⇒ Object
Returns the value of attribute size.
215 216 217 |
# File 'lib/ruby-web-search.rb', line 215 def size @size end |
#start_index ⇒ Object
Returns the value of attribute start_index.
213 214 215 |
# File 'lib/ruby-web-search.rb', line 213 def start_index @start_index end |
#type ⇒ Object
Returns the value of attribute type.
214 215 216 |
# File 'lib/ruby-web-search.rb', line 214 def type @type end |
#version ⇒ Object
Returns the value of attribute version.
214 215 216 |
# File 'lib/ruby-web-search.rb', line 214 def version @version end |
Instance Method Details
#build_request ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/ruby-web-search.rb', line 261 def build_request if custom_request_url custom_request_url else @request_url = "#{SEARCH_BASE_URLS[type]}/v#{version}/#{CGI.escape(query)}" @request_url << "?appid=#{api_key}" @request_url << "&count=#{size}" if size @request_url << "&start=#{cursor}" if cursor > 0 @request_url << "&lang=#{language_code}®ion=#{country_code}" if language_code && country_code puts request_url if $RUBY_WEB_SEARCH_DEBUG request_url end end |
#build_requests ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/ruby-web-search.rb', line 276 def build_requests if custom_request_url requests = [custom_request_url] else requests = [] # limiting to 10 responses per request (size / 10.to_f).ceil.times do |n| url = "#{SEARCH_BASE_URLS[type]}/v#{version}/#{CGI.escape(query)}" url << "?appid=#{api_key}" url << "&count=#{size}" if size url << "&lang=#{language_code}®ion=#{country_code}" if language_code && country_code url << "&start=#{cursor}" if cursor > 0 @cursor += 10 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
319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/ruby-web-search.rb', line 319 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
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'lib/ruby-web-search.rb', line 300 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 |