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)


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/ruby-web-search.rb', line 236

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.



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

def api_key
  @api_key
end

#country_codeObject

Returns the value of attribute country_code.



210
211
212
# File 'lib/ruby-web-search.rb', line 210

def country_code
  @country_code
end

#cursorObject

Returns the value of attribute cursor.



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

def cursor
  @cursor
end

#custom_request_urlObject

Returns the value of attribute custom_request_url.



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

def custom_request_url
  @custom_request_url
end

#custom_search_engine_idObject

Returns the value of attribute custom_search_engine_id.



211
212
213
# File 'lib/ruby-web-search.rb', line 211

def custom_search_engine_id
  @custom_search_engine_id
end

#filterObject

Returns the value of attribute filter.



210
211
212
# File 'lib/ruby-web-search.rb', line 210

def filter
  @filter
end

#language_codeObject

Returns the value of attribute language_code.



210
211
212
# File 'lib/ruby-web-search.rb', line 210

def language_code
  @language_code
end

#queryObject

Returns the value of attribute query.



210
211
212
# File 'lib/ruby-web-search.rb', line 210

def query
  @query
end

#refererObject

Returns the value of attribute referer.



211
212
213
# File 'lib/ruby-web-search.rb', line 211

def referer
  @referer
end

#request_urlObject

Returns the value of attribute request_url.



211
212
213
# File 'lib/ruby-web-search.rb', line 211

def request_url
  @request_url
end

#responseObject

Returns the value of attribute response.



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

def response
  @response
end

#safe_searchObject

Returns the value of attribute safe_search.



211
212
213
# File 'lib/ruby-web-search.rb', line 211

def safe_search
  @safe_search
end

#sizeObject

Returns the value of attribute size.



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

def size
  @size
end

#start_indexObject

Returns the value of attribute start_index.



210
211
212
# File 'lib/ruby-web-search.rb', line 210

def start_index
  @start_index
end

#typeObject

Returns the value of attribute type.



211
212
213
# File 'lib/ruby-web-search.rb', line 211

def type
  @type
end

#versionObject

Returns the value of attribute version.



211
212
213
# File 'lib/ruby-web-search.rb', line 211

def version
  @version
end

Instance Method Details

#build_requestObject



258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/ruby-web-search.rb', line 258

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



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/ruby-web-search.rb', line 273

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



316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/ruby-web-search.rb', line 316

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



297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/ruby-web-search.rb', line 297

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