Permalink
Browse files

Handle timeout on bing requests.

  • Loading branch information...
1 parent 228a136 commit d35e1030bbbf54e49df245d41e52e74b17986422 @andlima andlima committed Nov 21, 2011
Showing with 13 additions and 9 deletions.
  1. +13 −9 lib/support/bing.rb
View
@@ -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

0 comments on commit d35e103

Please sign in to comment.