Using the Geocoder gem to do IP 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.
The downsides of the Geocoder gem
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.
Setting up the localization in your Rails project via I18n
To translate all the string used by core Rails, you need only to install the rails-i18n gem, which provides the translation in more than a hundred languages. You can also write your own translation in the files located in the config/locales directory.
Add one of the following lines to your Gemfile:
Then update your bundle:
You must then set up a mechanism that allows the controller to determine which locale to use for the current request. Here is the list of elements to consider:
- the locale is saved in the user's session,
- a user can manually change the locale if he prefers to use your site in another language
- the system falls back to the default locale.
You can implement this function in a before_action of all your controllers:
You also need to save the selected locale in the user's session:
Add automatic locale detection with IP geolocation
One of the most convenient features for a website is to automatically detect new visitors' geographical location and display itself in the locality corresponding to their region. This can be implemented through geolocation.
To do so, it is possible to use the geoip gem, which searches through a database to match an IP address to its geographical location.
Install the geoip gem in your Gemfile:
Then update your bundle:
Download the geolocation database from the MaxMind website, and extract the files in your Rails directory.
You can then use the gem's API to obtain the geographical location of your visitor from their IP address:
However, this is not the easiest method, mainly because it requires a lot of maintenance as you will have to regularly download every MaxMind database updates in order to keep your geolocation accurate. It may not contain all the information you need, such as whether the IP address is using a VPN or proxy service.
Using the free Abstract IP geolocation service (easier)
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... You can even pair it with Abstract's timezone API to get even more granular data.
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: