How to use IP2Location.io library in Ruby on Rails

IP2Location.io library in Ruby on Rails

Ruby on Rails, commonly known as Rails, is a powerful, open-source web application framework designed with the Ruby programming language. It follows the Model-View-Controller (MVC) architecture, which divides application logic into three distinct yet interconnected components: models handle data and business logic, views manage the user interface, and controllers connect the two by processing user inputs.

Rails is built on the principles of convention over configuration, reducing the need for extensive initial setup by providing default structures and rules. This feature accelerates development and promotes best practices, making Rails particularly popular for building scalable, database-driven web applications. Its elegant syntax and robust ecosystem contribute to its wide adoption in the development community.

In the context of Rails, a library often referred to as a “gem” is a reusable package of code that enhances the framework’s functionality. These gems can offer various capabilities, including user authentication, payment processing or API integrations. Easily integrated into a project through the Gemfile and managed with Bundler, gems simplify development by offering pre-built solutions. This allows developers to focus on creating unique features rather than reinventing common functionalities.

An example of the Ruby on Rails library is IP2Location.io Ruby on Rails library that enables user to query for an enriched data set, such as country, region, district, city, latitude & longitude, ZIP code, time zone, ASN, ISP, domain, net speed, IDD code, area code, weather station data, MNC, MCC, mobile brand, elevation, usage type, address type, advertisement category and proxy data with an IP address. It requires a IP2Location.io API key to function. You may sign up for a free API key at https://www.ip2location.io/pricing

This tutorial demonstrates how to utilize the IP2Location.io library within a Ruby on Rails application to fetch geolocation data. Before proceeding, it is assumed that the Ruby on Rails framework has already been correctly installed and configured.

Display Geolocation Data Sample Code

  1. Add this lines to your Ruby on Rails server Gemfile.
gem 'ip2location_io_ruby'
gem 'ip2location_io_rails'
  1. Execute it by running the command.

$ bundle install

  1. Open the preferred file in the config/environments directory. Add the following code to the chosen configuration file after the Rails.application.configure do line.
config.ip2location_io_key = 'YOUR_API_KEY'
  1. Create a TestController using the below command line
bin/rails generate controller Test index --skip-routes
  1. Open the app/controllers/test_controller.rb in any text editor.
  2. Add the below lines into the controller file.
require 'ip2location_io_rails'

class TestController < ApplicationController
  def index
    location_service = Ip2locationIORails.new('8.8.8.8')
    @location = location_service.location.body
  end
end
  1. Open the app/views/test/index.html.erb in any text editor and add the below lines into it.
<p>IP2Location.io data for IP 8.8.8.8: <%= @location %></p>
  1. Add the following line into the config/routes.rb file after the Rails.application.routes.draw do line.
get "/test", to: "test#index"
  1. Restart your development server.
$ bin/rails server
  1. Enter the URL /test and run. You should see the information of 8.8.8.8 IP address.
  2. Done.

Upon completing this tutorial, you will be equipped to perform IP geolocation lookups using the IP2Location.io library within a Ruby on Rails application. You’ll gain the ability to extract detailed geographical data linked to an IP address, such as the user’s location, including country, region and city. This information can be invaluable for enhancing data analytics, personalizing user experiences and understanding regional access patterns. Additionally, it can help improve decision-making processes by providing insights into user demographics and behaviors.

Was this article helpful?

Related Articles