Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Handle timeout on bing requests.

  • Loading branch information...
commit d35e1030bbbf54e49df245d41e52e74b17986422 1 parent 228a136
@andlima andlima authored
Showing with 13 additions and 9 deletions.
  1. +13 −9 lib/support/bing.rb
View
22 lib/support/bing.rb
@@ -1,14 +1,18 @@
module Support::Bing
extend self
- def search(string, options = { :max_results => 5 })
- response_data = Bing.web(string)
- results = if response_data['Web'] && (bing_response = response_data.web).total > 1
- bing_response.results.first(options[:max_results])
- else
- []
- end
- { :results => results,
- :total_results_count => (bing_response && bing_response.total).to_i }
+ def search(string, options = { :max_results => 5, :timeout => 1 })
+ empty_response = { :results => [], :total_results_count => 0 }
+ timeout(options[:timeout]) do
+ response_data = Bing.web(string)
+ if response_data && response_data['Web'] && (bing_response = response_data.web).total > 1
+ results = bing_response.results.first(options[:max_results])
+ { :results => results, :total_results_count => bing_response.total.to_i }
+ else
+ empty_response
+ end
+ end
+ rescue Errno::ETIMEDOUT, Errno::ECONNREFUSED, Timeout::Error
+ empty_response
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.