Class: RubyWebSearch::Bing::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://api.search.live.net/json.aspx?sources=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)


410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/ruby-web-search.rb', line 410

def initialize(options={})
  if options[:custom_request_url]
    @custom_request_url = options[:request_url]
  else
    @query = options[:query]
    raise Bing::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 Bing::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.



386
387
388
# File 'lib/ruby-web-search.rb', line 386

def api_key
  @api_key
end

#country_codeObject

Returns the value of attribute country_code.



384
385
386
# File 'lib/ruby-web-search.rb', line 384

def country_code
  @country_code
end

#cursorObject

Returns the value of attribute cursor.



386
387
388
# File 'lib/ruby-web-search.rb', line 386

def cursor
  @cursor
end

#custom_request_urlObject

Returns the value of attribute custom_request_url.



386
387
388
# File 'lib/ruby-web-search.rb', line 386

def custom_request_url
  @custom_request_url
end

#custom_search_engine_idObject

Returns the value of attribute custom_search_engine_id.



385
386
387
# File 'lib/ruby-web-search.rb', line 385

def custom_search_engine_id
  @custom_search_engine_id
end

#filterObject

Returns the value of attribute filter.



384
385
386
# File 'lib/ruby-web-search.rb', line 384

def filter
  @filter
end

#language_codeObject

Returns the value of attribute language_code.



384
385
386
# File 'lib/ruby-web-search.rb', line 384

def language_code
  @language_code
end

#queryObject

Returns the value of attribute query.



384
385
386
# File 'lib/ruby-web-search.rb', line 384

def query
  @query
end

#refererObject

Returns the value of attribute referer.



385
386
387
# File 'lib/ruby-web-search.rb', line 385

def referer
  @referer
end

#request_urlObject

Returns the value of attribute request_url.



385
386
387
# File 'lib/ruby-web-search.rb', line 385

def request_url
  @request_url
end

#responseObject

Returns the value of attribute response.



386
387
388
# File 'lib/ruby-web-search.rb', line 386

def response
  @response
end

#safe_searchObject

Returns the value of attribute safe_search.



385
386
387
# File 'lib/ruby-web-search.rb', line 385

def safe_search
  @safe_search
end

#sizeObject

Returns the value of attribute size.



386
387
388
# File 'lib/ruby-web-search.rb', line 386

def size
  @size
end

#start_indexObject

Returns the value of attribute start_index.



384
385
386
# File 'lib/ruby-web-search.rb', line 384

def start_index
  @start_index
end

#typeObject

Returns the value of attribute type.



385
386
387
# File 'lib/ruby-web-search.rb', line 385

def type
  @type
end

#versionObject

Returns the value of attribute version.



385
386
387
# File 'lib/ruby-web-search.rb', line 385

def version
  @version
end

Instance Method Details

#build_requestObject



432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'lib/ruby-web-search.rb', line 432

def build_request
  if custom_request_url
    custom_request_url
  else
    @request_url = "#{SEARCH_BASE_URLS[type]}&query=#{CGI.escape(query)}"
    @request_url << "&appid=#{api_key}"
    @request_url << "&web.count=#{size}" if size
    @request_url << "&web.offset=#{cursor}" if cursor > 0
    @request_url << "&market=#{language_code}-#{country_code}" if language_code && country_code

    puts request_url if $RUBY_WEB_SEARCH_DEBUG
    request_url
  end
end

#build_requestsObject



447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/ruby-web-search.rb', line 447

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]}&query=#{CGI.escape(query)}"
      url << "&appid=#{api_key}"
      url << "&web.count=#{size}" if size
      url << "&market=#{language_code}-#{country_code}" if language_code && country_code
      url << "&web.offset=#{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



490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/ruby-web-search.rb', line 490

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



471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
# File 'lib/ruby-web-search.rb', line 471

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