2

How to check for internet connection using Ruby.

rƒc
  • 124
  • 9
  • Why do you need to? You should add more detail, because this will get downvoted as it is – Elizabeth Buckwalter Oct 29 '09 at 06:48
  • Also let us know which platform you're on. Detecting a connection to the internet depends on the platform. Even then you wouldn't be able to distinguish an internet connection from a network connection without sending any packets – EmFi Oct 29 '09 at 07:08
  • Sorry for not being detailed. I am trying to code an app to fetch an XML file from a website. Intended platform will be Win32. – rƒc Oct 29 '09 at 21:50

2 Answers2

2

You could try using Net::HTTP and 'pinging' a reliable URL such as Google and check for an exception indicating they do not have an internet connection.

Kris
  • 19,188
  • 9
  • 91
  • 111
1

Using Ping is deprecated in Ruby 1.9.x as explained here: https://stackoverflow.com/a/8317838

A more future-proof, as well as more code-relevant solution would be the solution given by that answer, using open-uri and returning false on exception.

Community
  • 1
  • 1
xiy
  • 808
  • 6
  • 13