2009-09-25 1 views
4

나는 노코 기리를 사용하여 온라인의 XML 문서에서 일부 기상 데이터를 가져 오는거야, 나는 소스가 도달 할 수없는 경우에는 우아한 복구를위한 시간 제한을 설정하고 싶습니다 open-uri 및 Net :: HTTP에 대해 가능한 메소드가 있지만 Nokogiri에만 해당하는 메소드는 없습니다. 나의 이러한 방법을 사용하려고 시도합니다 (너무 놀라 울) 실패 :read_timeout?</p>이 <p>내 구글 검색 몇 가지 보여 ...

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 

반환 "NoMethodError"및 :

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 

반환 "형식 오류가 문자열로 해시를 변환 할 수 없습니다"

Nokogiri는 이러한 종류의 방법을 지원합니까 (그렇다면 ... 어떻게?), 아니면 다른 해결책을 찾아야합니까?

감사합니다.

답변

4

당신은 시간 제한 모듈을 사용할 수 있습니다

require 'open-uri' 
require 'nokogiri' 
require 'timeout' 

begin 
    timeout(10) do 
    result = Nokogiri::XML(open(currentloc)) 
    end 
rescue Timeout::Error 
    return "Current weahter..." 
end 
+0

이 작동하는 (적어도이 중단되지 않습니다) 나타납니다,하지만 난 eth0를을 분리하여 테스트 할 때, 나는 소켓 오류를 받고 있어요. 조건부 테스트도 필요하다고 생각합니다. 그러나 추가 구조 문 (구조 SocketError)을 추가하는 데 실패했습니다. 감사합니다. –

+0

나 자신을 더 자세하게 볼 수 있도록 코드를 게시 할 수 있습니까? – Vincent