ruby - Rails/nokogiri_parse multiples sites handling errors -


i developping on rails 3 , using nokogiri. controller parses multiples sites , shows result on view. problem is, when 1 of these sites unavailable (403 or 503 error example), webapp crashes because of site.

my question: there way check availability of parsed pages before nokogiri opens it, or better, going through/ignoring unavailability ?

thanks

part of controller:

docvariable1 = nokogiri::html(open("http://www.site1.com/")) @variable1 = {} docvariable1.xpath('//div[6]/h3/a').each |link| @variable1[link.text.strip] = link['href'] end    docvariable2 = nokogiri::html(open("http://www.site2.com/")) @variable2 = {} docvariable2.xpath('//div[6]/h3/a').each |link| @variable2[link.text.strip] = link['href'] end   docvariable3 = nokogiri::html(open("http://www.site3.com/")) @variable3 = {} docvariable3.xpath('//div[6]/h3/a').each |link| @variable3[link.text.strip] = link['href'] end 

part of view

<% if @variable1 %> <% @variable1.each |key, value| %> <li ><a href=" <%= "#{value}" %>" target='_blank' ><%= "#{key}" %></a> <% end %> <% end %>  <% if @variable2 %> <% @variable1.each |key, value| %> <li ><a href=" <%= "#{value}" %>" target='_blank' ><%= "#{key}" %></a> <% end %> <% end %>  <% if @variable3 %> <% @variable1.each |key, value| %> <li ><a href=" <%= "#{value}" %>" target='_blank' ><%= "#{key}" %></a> <% end %> <% end %> 

ps: know code isn't quite "perfect" because opposite of "dry" principle, still learning ;)

you can try put each 1 of within begin -- rescue block, doesn't fail if 1 of them unavailable. can handle exceptions if necessary.

begin     docvariable1 = nokogiri::html(open("http://www.site1.com/"))     @variable1 = {}     docvariable1.xpath('//div[6]/h3/a').each |link|         @variable1[link.text.strip] = link['href']     end rescue     # handle exception end 

Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

VBA function to include CDATA -