2

I just created this little currency converter for my Rails 4 app:

module Currency

  def self.get_exchange_rate(from_curr = "EUR", to_curr = "USD")   
    if from_curr == to_curr
      result = 1
    else
      begin
        amount = 1
        url = "http://www.google.com/finance/converter?a=#{amount}&from=#{from_curr}&to=#{to_curr}"
        doc = Nokogiri::HTML(open(url))
        result_span = doc.css('span.bld').text    
        result = result_span.tr('^0-9.', '')
      rescue => e
        puts e
        result = 1
      end
    end
    result
  end

end

I haven't done this a lot, so my question would be: How can I deal with the (unlikely) event that Google Currency times out or is not available for some reason?

In that case I would like my result to be 1. How can this be achieved?

Thanks for any suggestions.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Tintin81
  • 9,821
  • 20
  • 85
  • 178

3 Answers3

2

You can simply rescue any error that Nokogiri might raise (or OpenURI) like so:

require 'nokogiri'
require 'open-uri'

def currency(a)
  Nokogiri::HTML(open(a))
rescue => e
  puts e
  1 # default value when error is raised
end

puts currency('https://www.somedomainthatdoesntexist.com')

That will print the backtrace of the error and then return the number 1

getaddrinfo: nodename nor servname provided, or not known
1
Anthony
  • 15,435
  • 4
  • 39
  • 69
2

For a custom timeout actions you can use Timeout module http://ruby-doc.org/stdlib-2.1.2/libdoc/timeout/rdoc/Timeout.html

lx00st
  • 1,566
  • 9
  • 17
0

It'd be worth looking at this answer to see how to get nokogiri to timeout : Adjusting timeouts for Nokogiri connections

The question makes use of the timeout module which is an option but I think it is better to get the connection to time out as per the accepted answer.

All that will mean that you'll get a Timeout::Error exception raised if the call times out which you then need to handle:

begin
  doc = ... use nokogiri ...
  result_span = doc.css('span.bld').text    
  result = result_span.tr('^0-9.', '')
rescue Timeout::Error
  result = 1
end
result
Community
  • 1
  • 1
Shadwell
  • 34,314
  • 14
  • 94
  • 99
  • 1
    Nokogiri doesn't have timeouts, but OpenURI does. Nokogiri might have parse errors, especially if that google page's css or html changes. – Anthony Apr 24 '15 at 09:18
  • 1
    Yep, I know that nokogiri doesn't which is why I linked to the question about adjusting timeouts for Nokogiri connections. I then go on to explain that once you've connected to nokogiri in a way that *will* timeout that you can rescue the timeout error. – Shadwell Apr 24 '15 at 09:39
  • OK, not sure if I can follow you guys. I updated my initial post with the new version. It is meant to catch *any* error that might come up (including timeouts and css changes) and then set the result to `1`. Do you think it is going to work? (I don't know how to test this because there haven't been any errors in Google's conversion service so far.) – Tintin81 Apr 24 '15 at 10:09