使用Open-URI来获取XML,以及在远程URL不能返回/超时的情况下的最佳做法? [英] Using Open-URI to fetch XML and the best practice in case of problems with a remote url not returning/timing out?
问题描述
目前的代码只要没有远程错误就可以工作:
Current code works as long as there is no remote error:
def get_name_from_remote_url
cstr = "http://someurl.com"
getresult = open(cstr, "UserAgent" => "Ruby-OpenURI").read
doc = Nokogiri::XML(getresult)
my_data = doc.xpath("/session/name").text
# => 'Fred' or 'Sam' etc
return my_data
end
但是,如果远程URL超时或什么也不返回?例如,我如何检测并返回nil?
But, what if the remote URL times out or returns nothing? How I detect that and return nil, for example?
而且,Open-URI是否提供了一种方法来定义放弃之前等待多久?在用户等待响应时调用此方法,那么我们如何在放弃之前设置最大时间间隔,并告诉用户对不起,我们尝试访问的远程服务器现在不可用?
And, does Open-URI give a way to define how long to wait before giving up? This method is called while a user is waiting for a response, so how do we set a max timeoput time before we give up and tell the user "sorry the remote server we tried to access is not available right now"?
推荐答案
Open-URI方便,但易用性意味着他们正在删除对其他HTTP客户端的大量配置详细信息的访问像Net :: HTTP允许。
Open-URI is convenient, but that ease of use means they're removing the access to a lot of the configuration details the other HTTP clients like Net::HTTP allow.
这取决于你使用的是什么版本的Ruby。对于1.8.7,您可以使用超时模块。从文档:
It depends on what version of Ruby you're using. For 1.8.7 you can use the Timeout module. From the docs:
require 'timeout'
begin
status = Timeout::timeout(5) {
getresult = open(cstr, "UserAgent" => "Ruby-OpenURI").read
}
rescue Timeout::Error => e
puts e.to_s
end
然后检查getresult的长度看看你是否有任何内容:
Then check the length of getresult to see if you got any content:
if (getresult.empty?)
puts "got nothing from url"
end
如果您使用的是Ruby 1.9.2,可以添加 :read_timeout =>
选项。 open()
方法中的
If you are using Ruby 1.9.2 you can add a :read_timeout => 10
option to the open()
method.
此外,您的代码可以收紧,并更灵活。这将让您传递URL或默认值到当前使用的URL。另请参阅Nokogiri的 NodeSet 文档,了解 xpath
, /
, css
和 at
,%
, at_css
, at_xpath
/ p>
Also, your code could be tightened up and made a bit more flexible. This will let you pass in a URL or default to the currently used URL. Also read Nokogiri's NodeSet docs to understand the difference between xpath
, /
, css
and at
, %
, at_css
, at_xpath
:
def get_name_from_remote_url(cstr = 'http://someurl.com')
doc = Nokogiri::XML(open(cstr, 'UserAgent' => 'Ruby-OpenURI'))
# xpath returns a nodeset which has to be iterated over
# my_data = doc.xpath('/session/name').text # => 'Fred' or 'Sam' etc
# at returns a single node
doc.at('/session/name').text
end
这篇关于使用Open-URI来获取XML,以及在远程URL不能返回/超时的情况下的最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!