The need to translate a visitor’s IP address to his physical location exists for more than 20 years, and multiple kinds of industries, from banking to e-commerce, are using IP geolocation technologies. Regional licensing, legal concerns, geo-targeting, marketing strategies, geo-blocking, political concerns are only a few of many examples where IP geolocation becomes a necessity.
In this article, we will focus on two different ways to implement IP geolocation in Ruby, which can also be used in a Ruby on Rails application.
At a high-level, IP geolocation consists of getting the visitor’s IP address, then querying a database to get data about the visitor’s physical location, such as GPS coordinates, time zone, country, city, and so on.
A developer needs to understand IP geolocation has some limitations. Every internet user is accessing the web from behind the infrastructure of their internet service provider, which means their public IP could translate into a physical location in the neighborhood of their current location.
Try it by yourself: search Google for “Where am I now” and look where those websites locate you. You will notice your location may be slightly out of place. This can even be worse for users behind a VPN, which can be used to appear to be in a different city, state, and even country.
Let’s now study two different methods to implement geolocation in Ruby
Geocoder is a well-maintained and popular Ruby gem available on Github. Among few other tools, it can be used to retrieve location from an IP address. Geocoder gem relies on an external API to provide the requested data. The list of supported API is available on their Github repository.
The tedious parts are obtaining a Key from your favorite API and configuring the gem, after which looking for an IP address location is very simple:
The accuracy of the location you can obtain depends on your visitor's internet provider's infrastructure, as mentioned above, and the quality of the API you are using.
Geocoder does not know how to perform geolocation by itself. It is simply a proxy for using an external API.
As mentioned above, and depending on your choice's API service, it may be difficult to create a key and authorize your application. As an example, Google Cloud Console can be very confusing.
An even simpler alternative to Geocoder is the use of Abstract IP Geolocation API. It’s a free, world-class API, extremely precise, and able to provide lots of details from an IP address, including city, region, country, GPS coordinates...
As for most API services, you will first have to create an account on the Abstract website, automatically providing you with the API key and giving you access to the documentation. Then you are already all set-up and ready to go!
At this stage, you can already test the API from your browser by opening a URL like the one below, replacing YOUR_API_KEY with the key you obtained from your account page in the Abstract website:
As a result, you will obtain multiple details about a location associated with this IP address. You can also try it with your own public IP address.
Now let’s implement this in a formal Ruby way, using the Net::HTTP Ruby standard library:
You can run this code in a ruby console. Here is the response you would get: