Class: RubyWebSearch::Yahoo::Query

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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(options={})
  if options[:custom_request_url]
    @custom_request_url = options[:request_url]
  else
    @query = options[:query]
    raise Yahoo::Query::Error, "You need to pass a query" unless @query
    @cursor                   = options[:start_index] || 0
    @filter                   = options[:filter]
    @type                     = options[:type]        || :web
    @country_code             = options[:country_code]
    @language_code            = options[:language_code]
    @safe_search              = options[:safe_search]
    @custom_search_engine_id  = options[:custom_search_engine_id]
    @version                  = options[:version] || "1"
    @referer                  = options[:referer] ||  "http://github.com/mattetti/"
    @api_key                  = options[:api_key]
    raise Yahoo::Query::Error, "You need to pass an api key" unless @api_key
    @size                     = options[:size] || 10
  end
  @response ||= Response.new(:query => (query || custom_request_url), :size => size)
end

Instance Attribute Details

#api_keyObject

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_codeObject

Returns the value of attribute country_code.



213
214
215
# File 'lib/ruby-web-search.rb', line 213

def country_code
  @country_code
end

#cursorObject

Returns the value of attribute cursor.



215
216
217
# File 'lib/ruby-web-search.rb', line 215

def cursor
  @cursor
end

#custom_request_urlObject

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_idObject

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

#filterObject

Returns the value of attribute filter.



213
214
215
# File 'lib/ruby-web-search.rb', line 213

def filter
  @filter
end

#language_codeObject

Returns the value of attribute language_code.



213
214
215
# File 'lib/ruby-web-search.rb', line 213

def language_code
  @language_code
end

#queryObject

Returns the value of attribute query.



213
214
215
# File 'lib/ruby-web-search.rb', line 213

def query
  @query
end

#refererObject

Returns the value of attribute referer.



214
215
216
# File 'lib/ruby-web-search.rb', line 214

def referer
  @referer
end

#request_urlObject

Returns the value of attribute request_url.



214
215
216
# File 'lib/ruby-web-search.rb', line 214

def request_url
  @request_url
end

#responseObject

Returns the value of attribute response.



215
216
217
# File 'lib/ruby-web-search.rb', line 215

def response
  @response
end

#safe_searchObject

Returns the value of attribute safe_search.



214
215
216
# File 'lib/ruby-web-search.rb', line 214

def safe_search
  @safe_search
end

#sizeObject

Returns the value of attribute size.



215
216
217
# File 'lib/ruby-web-search.rb', line 215

def size
  @size
end

#start_indexObject

Returns the value of attribute start_index.



213
214
215
# File 'lib/ruby-web-search.rb', line 213

def start_index
  @start_index
end

#typeObject

Returns the value of attribute type.



214
215
216
# File 'lib/ruby-web-search.rb', line 214

def type
  @type
end

#versionObject

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_requestObject



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}&region=#{country_code}" if language_code && country_code

    puts request_url if $RUBY_WEB_SEARCH_DEBUG
    request_url
  end
end

#build_requestsObject



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}&region=#{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

#executeObject

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_unthreadedObject

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