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)
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(={}) 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.
212 213 214 |
# File 'lib/ruby-web-search.rb', line 212 def api_key @api_key end |
#country_code ⇒ Object
Returns the value of attribute country_code.
210 211 212 |
# File 'lib/ruby-web-search.rb', line 210 def country_code @country_code end |
#cursor ⇒ Object
Returns the value of attribute cursor.
212 213 214 |
# File 'lib/ruby-web-search.rb', line 212 def cursor @cursor end |
#custom_request_url ⇒ Object
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_id ⇒ Object
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 |
#filter ⇒ Object
Returns the value of attribute filter.
210 211 212 |
# File 'lib/ruby-web-search.rb', line 210 def filter @filter end |
#language_code ⇒ Object
Returns the value of attribute language_code.
210 211 212 |
# File 'lib/ruby-web-search.rb', line 210 def language_code @language_code end |
#query ⇒ Object
Returns the value of attribute query.
210 211 212 |
# File 'lib/ruby-web-search.rb', line 210 def query @query end |
#referer ⇒ Object
Returns the value of attribute referer.
211 212 213 |
# File 'lib/ruby-web-search.rb', line 211 def referer @referer end |
#request_url ⇒ Object
Returns the value of attribute request_url.
211 212 213 |
# File 'lib/ruby-web-search.rb', line 211 def request_url @request_url end |
#response ⇒ Object
Returns the value of attribute response.
212 213 214 |
# File 'lib/ruby-web-search.rb', line 212 def response @response end |
#safe_search ⇒ Object
Returns the value of attribute safe_search.
211 212 213 |
# File 'lib/ruby-web-search.rb', line 211 def safe_search @safe_search end |
#size ⇒ Object
Returns the value of attribute size.
212 213 214 |
# File 'lib/ruby-web-search.rb', line 212 def size @size end |
#start_index ⇒ Object
Returns the value of attribute start_index.
210 211 212 |
# File 'lib/ruby-web-search.rb', line 210 def start_index @start_index end |
#type ⇒ Object
Returns the value of attribute type.
211 212 213 |
# File 'lib/ruby-web-search.rb', line 211 def type @type end |
#version ⇒ Object
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_request ⇒ Object
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}®ion=#{country_code}" if language_code && country_code puts request_url if $RUBY_WEB_SEARCH_DEBUG request_url end end |
#build_requests ⇒ Object
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}®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
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_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
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 |