Using an Open-URI to retrieve XML and best practice in case of problems with a remote url that does not return / smooth?

The current code works until there is a 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 

But what if the remote URL expires or returns nothing? How do I detect this and return zero, for example?

And, does the Open-URI provide a way to determine how long to wait before giving up? This method is called while the user is waiting for a response, so how do we set the maximum timeoput before we give up and tell the user β€œit’s a pity that the remote server we were trying to access is not available right now”?

+4
source share
1 answer

Open-URI is convenient, but this ease of use means that they remove access to many configuration details that other HTTP clients, such as Net :: HTTP, can use.

It depends on which version of Ruby you are 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 

Then check the getresult length to see if you have content:

 if (getresult.empty?) puts "got nothing from url" end 

If you are using Ruby 1.9.2, you can add :read_timeout => 10 the open() parameter.


In addition, your code can be tightened and made a little more flexible. This will allow you to pass the URL or use the URL used by default. Also read the Nokogiri NodeSet 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 
+9
source

All Articles