Guides
Last updated
December 28, 2020

How to geolocate an IP address in Ruby

Emma Jagger
Emma Jagger

Table of Contents:

Get your free
IP Geolocation
Ā API key now
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

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.

Key concepts in IP address geolocation

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.

Enter an IP address to start
Need inspiration? Try
73.162.0.1
LOCATE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Checking
5
Results for
ip address
Country:
TEST
Region:
TEST
City:
TEST
Coordinates:
TEST
Zip code:
TEST
Connection:
TEST
Get free credits, more data, and faster results

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:



results = Geocoder.search("172.56.21.89")
results.first.coordinates
=> [30.267153, -97.7430608]
results.first.country
=> "United States"

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.

IP Geolocation in Ruby using Abstract (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...

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:



https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_API_KEY&ip_address=92.184.105.98

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:



require 'net/http'
require 'net/https'

def make_abstract_request
uri = URI('https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_API_KEY&ip_address=92.184.105.98')

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

request = Net::HTTP::Get.new(uri)

response = http.request(request)

return response.body
rescue StandardError => error
puts "Error (#{ error.message })"
end

pp JSON.parse(make_abstract_request)

You can run this code in a ruby console. Here is the response you would get:



{
"ip_address": "92.184.105.98",
"city": "Daglan",
"city_geoname_id": 3021928,
"region": "Nouvelle-Aquitaine",
"region_iso_code": "NAQ",
"region_geoname_id": 11071620,
"postal_code": "24250",
"country": "France",
"country_code": "FR",
"country_geoname_id": 3017382,
"country_is_eu": true,
"continent": "Europe",
"continent_code": "EU",
"continent_geoname_id": 6255148,
"longitude": 1.19282,
"latitude": 44.7419,
"security": {
"is_vpn": false
},
"timezone": {
"name": "Europe/Paris",
"abbreviation": "CET",
"gmt_offset": 1,
"current_time": "11:52:28",
"is_dst": false
},
"flag": {
"emoji": "šŸ‡«šŸ‡·",
"unicode": "U+1F1EB U+1F1F7",
"png": "https://static.abstractapi.com/country-flags/FR_flag.png",
"svg": "https://static.abstractapi.com/country-flags/FR_flag.svg"
},
"currency": {
"currency_name": "Euros",
"currency_code": "EUR"
},
"connection": {
"autonomous_system_number": 3215,
"autonomous_system_organization": "Orange S.A.",
"connection_type": "Cellular",
"isp_name": "Orange S.A.",
"organizaton_name": "Internet OM"
}
}

Frequently Asked Questions

What is IP geolocation in Ruby?

IP geolocation in Ruby is the process of taking a visitor's IP address and querying a database or API to retrieve their physical location data, such as country, city, GPS coordinates, and timezone. You can implement this in Ruby using either the Geocoder gem or by calling an external geolocation API directly with Ruby's built-in Net::HTTP library.

How do I use the Geocoder gem to geolocate an IP address in Ruby?

Install the Geocoder gem, configure it with your chosen external API key, then call Geocoder.search("your_ip_address") to retrieve location results. The gem acts as an abstraction layer over multiple geolocation APIs, so you can swap providers without rewriting your lookup logic.

How do I call Abstract's IP Geolocation API directly from Ruby?

Use Ruby's standard Net::HTTP library to make a GET request to Abstract's IP Geolocation API endpoint, passing your API key and the target IP address as query parameters. Enable SSL on the HTTP connection to ensure the request is sent over HTTPS, then parse the JSON response to extract the location fields you need.

What is the difference between using Geocoder and calling an API directly in Ruby?

The Geocoder gem adds a convenient abstraction and supports multiple backend APIs, but it requires extra configuration and adds a dependency to your project. Calling an API like Abstract directly with Net::HTTP involves less setup and gives you full control over the request, at the cost of writing a bit more boilerplate code.

Can I use IP geolocation in a Ruby on Rails application?

Yes. Both the Geocoder gem and direct API integrations work within Rails. The Geocoder gem is especially popular in Rails apps because it integrates with ActiveRecord and can resolve the request IP directly from a controller using request.remote_ip. The same Net::HTTP approach used in plain Ruby works in Rails without modification.

How accurate is IP geolocation in Ruby, and does it work with VPNs?

IP geolocation accuracy varies depending on the provider and how ISPs allocate their address ranges: results can be off by a city or even a region. Users behind a VPN will appear to be located wherever the VPN server is, not their actual location, which is a known limitation of any IP-based geolocation approach regardless of the Ruby library or API used.

Emma Jagger
Emma Jagger

Emma Jagger is an experienced engineer and Google alumna with a degree from Carnegie Mellon University. She specializes in email validation, IP geolocation, and API integration, focusing on creating practical and scalable solutions through her technical writing.

Get your free
IP Geolocation
key now
See why the best developers build on Abstract
get started for free

Related Articles

Get your free
IP Geolocation
key now
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required
Guides
Last updated
December 28, 2020

How to geolocate an IP address in Ruby

Emma Jagger
Emma Jagger

Table of Contents:

Get your free
IP Geolocation
Ā API key now
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

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.

Key concepts in IP address geolocation

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.

Enter an IP address to start
Need inspiration? Try
73.162.0.1
LOCATE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Checking
5
Results for
ip address
Country:
TEST
Region:
TEST
City:
TEST
Coordinates:
TEST
Zip code:
TEST
Connection:
TEST
Get free credits, more data, and faster results

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:



results = Geocoder.search("172.56.21.89")
results.first.coordinates
=> [30.267153, -97.7430608]
results.first.country
=> "United States"

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.

IP Geolocation in Ruby using Abstract (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...

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:



https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_API_KEY&ip_address=92.184.105.98

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:



require 'net/http'
require 'net/https'

def make_abstract_request
uri = URI('https://ipgeolocation.abstractapi.com/v1/?api_key=YOUR_API_KEY&ip_address=92.184.105.98')

http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER

request = Net::HTTP::Get.new(uri)

response = http.request(request)

return response.body
rescue StandardError => error
puts "Error (#{ error.message })"
end

pp JSON.parse(make_abstract_request)

You can run this code in a ruby console. Here is the response you would get:



{
"ip_address": "92.184.105.98",
"city": "Daglan",
"city_geoname_id": 3021928,
"region": "Nouvelle-Aquitaine",
"region_iso_code": "NAQ",
"region_geoname_id": 11071620,
"postal_code": "24250",
"country": "France",
"country_code": "FR",
"country_geoname_id": 3017382,
"country_is_eu": true,
"continent": "Europe",
"continent_code": "EU",
"continent_geoname_id": 6255148,
"longitude": 1.19282,
"latitude": 44.7419,
"security": {
"is_vpn": false
},
"timezone": {
"name": "Europe/Paris",
"abbreviation": "CET",
"gmt_offset": 1,
"current_time": "11:52:28",
"is_dst": false
},
"flag": {
"emoji": "šŸ‡«šŸ‡·",
"unicode": "U+1F1EB U+1F1F7",
"png": "https://static.abstractapi.com/country-flags/FR_flag.png",
"svg": "https://static.abstractapi.com/country-flags/FR_flag.svg"
},
"currency": {
"currency_name": "Euros",
"currency_code": "EUR"
},
"connection": {
"autonomous_system_number": 3215,
"autonomous_system_organization": "Orange S.A.",
"connection_type": "Cellular",
"isp_name": "Orange S.A.",
"organizaton_name": "Internet OM"
}
}

Frequently Asked Questions

What is IP geolocation in Ruby?

IP geolocation in Ruby is the process of taking a visitor's IP address and querying a database or API to retrieve their physical location data, such as country, city, GPS coordinates, and timezone. You can implement this in Ruby using either the Geocoder gem or by calling an external geolocation API directly with Ruby's built-in Net::HTTP library.

How do I use the Geocoder gem to geolocate an IP address in Ruby?

Install the Geocoder gem, configure it with your chosen external API key, then call Geocoder.search("your_ip_address") to retrieve location results. The gem acts as an abstraction layer over multiple geolocation APIs, so you can swap providers without rewriting your lookup logic.

How do I call Abstract's IP Geolocation API directly from Ruby?

Use Ruby's standard Net::HTTP library to make a GET request to Abstract's IP Geolocation API endpoint, passing your API key and the target IP address as query parameters. Enable SSL on the HTTP connection to ensure the request is sent over HTTPS, then parse the JSON response to extract the location fields you need.

What is the difference between using Geocoder and calling an API directly in Ruby?

The Geocoder gem adds a convenient abstraction and supports multiple backend APIs, but it requires extra configuration and adds a dependency to your project. Calling an API like Abstract directly with Net::HTTP involves less setup and gives you full control over the request, at the cost of writing a bit more boilerplate code.

Can I use IP geolocation in a Ruby on Rails application?

Yes. Both the Geocoder gem and direct API integrations work within Rails. The Geocoder gem is especially popular in Rails apps because it integrates with ActiveRecord and can resolve the request IP directly from a controller using request.remote_ip. The same Net::HTTP approach used in plain Ruby works in Rails without modification.

How accurate is IP geolocation in Ruby, and does it work with VPNs?

IP geolocation accuracy varies depending on the provider and how ISPs allocate their address ranges: results can be off by a city or even a region. Users behind a VPN will appear to be located wherever the VPN server is, not their actual location, which is a known limitation of any IP-based geolocation approach regardless of the Ruby library or API used.

Emma Jagger
Emma Jagger

Emma Jagger is an experienced engineer and Google alumna with a degree from Carnegie Mellon University. She specializes in email validation, IP geolocation, and API integration, focusing on creating practical and scalable solutions through her technical writing.

Get your free
IP Geolocation
key now
See why the best developers build on Abstract
get started for free

Related Articles

Get your free
IP Geolocation
key now
stars rating
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required