Coming up next in this section is the Internet checks in the ‘modules/internet_check.rb’ module.
I’ll walk you through the interesting parts of this code. The first thing that we can see is that I have extra libraries.
Those are used for DNS resolution and ping. In old Ruby versions there used to be a ‘Ping’ namespace which was part of the standard library but it was deprecated in favor of ‘net/ping’ which is what we’re going to use.
Next we’ll look into the ‘check_resolv’ method. In Ruby most of the things you’ll do will be methods. I break this rule occasionally but sticking to it will make your code cleaner and more easy to test.
The idea of this section is simple. I want to resolve ‘google.com’ and return its IP address. There are two main purposes of this method. The first one is to return false if it cannot resolve the host. Let’s test it in the Interactive Ruby REPL (irb).
Notice that before I closed the method I said ‘resolved’. This is the same like ‘return resolved’ and it will return the variable. Now notice what happens if I do the some thing but with bad URL.
Having this ‘false’ being returned if URL is not resolved is important. That’s how in future calls I’ll know if the DNS is working or not. The second purpose of this method is to return IP. Every method in Ruby returns something. Having this something hold meaning is a good practice.
Now that I took care of the DNS check I’ll have to take care also about ping to the IP I got. That’s what this method is about:
Notice that this time I’m also taking a variable called ‘host’ and passing it to the ‘Net::Ping::External’ method from the ‘Net::Ping’ namespace. Let’s play a bit in the irb so we see what’s happening.
OK but what’s this ‘ping?’ and where did it come from? Well… let’s see.
Take a closer look at what I just did. This will help you a lot on the long run. I assigned the method call to a variable called ‘check’ just like in my original code. But then I called the ‘.class’ method on this variable. I did so because I want to understand what kind of object it is. This will help me see what behavior I can expect from it. I’ll go even further and call the ‘.ancestors’ method on the namespace that has the behavior of my new variable.
So, now I can see a lot. I see that it’s part of (inherits) Object. In other words all methods available to Object will also be available to Net::Ping and therefore to Net::Ping::External. Further down we’ll have also all methods from Kernel and BasicObject which are not that many but they are critical to having a working namespace.
I’m not going to explain it all in details but I’ll give you one last hint. You can call the ‘methods’ method on both namespace and our new variable to see what’s available to you.
This will answer also where ‘ping?’ comes from. It comes from the inheritance which is core concept of Object Oriented Programming.
And now last but not least we call the Internet restore module if we we cannot ping hosts.
Coming up next is my favorite module so far - the one that will reboot my router in case of connection issues.
The whole code of my Home Automation project is available on Github.