Read_timeout in Nokigiri?

I am extracting some weather data from an online xml document using Nokogiri and I would like to set a timeout for a graceful recovery in case the source cannot be reached ...

My Google searches show a few possible methods for open-uri and Net :: HTTP, but none apply to Nokogiri. My attempts to use these methods will not work (not surprisingly):

begin currentloc = ("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" + @destination.weatherloc) currentloc.read_timeout = 10 # doc = Nokogiri::XML(open(currentloc)) rescue Timeout::Error return "Current weather for this location not available: request timed out." end 

returns "NoMethodError" and:

 begin currentloc = ("http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" + @destination.weatherloc) doc = Nokogiri::XML(open(currentloc), :read_timeout => 10) rescue Timeout::Error return "Current weather for this location not available: request timed out." end 

returns "TypeError cannot convert Hash to string"

Does Nokogiri support this method (and if so ... how?), Or should I look at some other solution?

Thanks.

+4
source share
1 answer

You can use the timeout module:

 require 'open-uri' require 'nokogiri' require 'timeout' begin timeout(10) do result = Nokogiri::XML(open(currentloc)) end rescue Timeout::Error return "Current weahter..." end 
+4
source

All Articles