To determine the usage type of an IP address using IP2Location.io in Ruby, you can use the IP2Location.io API to retrieve geolocation and related data. The usage type field specifies how the IP is being used. For usage types supported, please see API documentation for more info.
require 'net/http'
require 'json'
key = 'YOUR_API_KEY'
ip = '8.8.8.8'
uri = URI('https://api.ip2location.io/')
params = { :key => key, :ip => ip }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
if !res.body.nil?
begin
myobj = JSON.parse(res.body)
if res.is_a?(Net::HTTPSuccess)
if myobj.key? 'usage_type'
puts 'The usage type for IP ' + ip + ' is ' + myobj['usage_type'] + '.'
else
puts 'ERROR: The usage_type field requires a paid subscription to the Starter plan or higher.'
end
else
if res.body.include? 'error_message'
puts 'ERROR: ' + myobj['error']['error_message']
else
puts 'ERROR: ' + res.body
end
end
rescue JSON::ParserError
puts 'ERROR: Invalid JSON in response.'
end
else
puts 'ERROR: Error calling API.'
end
ruby test.rb
This script will output the usage type of specified IP address. Make sure to replace 8.8.8.8 with the IP address you want and replace YOUR_API_KEY to your own API key.
Empower your applications with accurate IP geolocation information now.
Try It for Free