How to geolocate an IP in Ruby
1 min readAug 11, 2019
Let’s make this as simple as possible, only two steps!
Step 1. Add Cloudmersive’s validate API client to your Gemfile:
gem 'cloudmersive-validate-api-client', '~> 1.3.2'
Step 2. Call the function i_p_address_post:
# load the gem
require 'cloudmersive-validate-api-client'
# setup authorization
CloudmersiveValidateApiClient.configure do |config|
# Configure API key authorization: Apikey
config.api_key['Apikey'] = 'YOUR API KEY'
# Uncomment the following line to set a prefix for the API key, e.g. 'Bearer' (defaults to nil)
#config.api_key_prefix['Apikey'] = 'Bearer'
endapi_instance = CloudmersiveValidateApiClient::IPAddressApi.newvalue = "value_example" # String | IP address to geolocate, e.g. \"55.55.55.55\". The input is a string so be sure to enclose it in double-quotes.begin
#Geolocate an IP address
result = api_instance.i_p_address_post(value)
p result
rescue CloudmersiveValidateApiClient::ApiError => e
puts "Exception when calling IPAddressApi->i_p_address_post: #{e}"
end
Here’s an example output for 12.34.56.78:
{
"CountryCode": "US",
"CountryName": "United States",
"City": "Columbus",
"RegionCode": "OH",
"RegionName": "Ohio",
"ZipCode": "43204",
"TimezoneStandardName": "America/New York",
"Latitude": 39.953,
"Longitude": -83.0767
}
Notice that we are provided with the city, state or province, zip code, country name and code, as well as the actual latitude and longitude coordinates.