Skip to content

Add IPinfo Lite service #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README_API_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,19 @@ IP Address Lookups
* **Documentation**: https://ipinfo.io/developers
* **Terms of Service**: https://ipinfo.io/terms-of-service

### IPInfo.io Lite (`:ipinfo_io_lite`)

A free-tier API access plan, which includes unlimited country-level geolocation information and unlimited basic ASN information. Geolocation information provided by the Lite API service includes country and continent information, while ASN information includes ASN, organization name, and domain name.

* **API key**: required
* **Quota**: none
* **Region**: world
* **SSL support**: yes
* **Languages**: English
* **Documentation**: https://ipinfo.io/developers/lite-api
* **Terms of Service**: https://ipinfo.io/terms-of-service
* **Limitations**: country-level geolocation information and basic ASN information only

### IPQualityScore (`:ipqualityscore`)

* **API key**: required - see https://www.ipqualityscore.com/free-ip-lookup-proxy-vpn-test
Expand Down
1 change: 1 addition & 0 deletions lib/geocoder/lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def ip_services
:pointpin,
:maxmind_geoip2,
:ipinfo_io,
:ipinfo_io_lite,
:ipregistry,
:ipapi_com,
:ipdata_co,
Expand Down
42 changes: 42 additions & 0 deletions lib/geocoder/lookups/ipinfo_io_lite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'geocoder/lookups/base'
require 'geocoder/results/ipinfo_io_lite'

module Geocoder::Lookup
class IpinfoIoLite < Base
def name
'Ipinfo.io Lite'
end

private # ---------------------------------------------------------------

def base_query_url(query)
url = "#{protocol}://api.ipinfo.io/lite/#{query.sanitized_text}"
url << '?' if configuration.api_key
url
end

def results(query)
# don't look up a loopback or private address, just return the stored result
return [reserved_result(query.text)] if query.internal_ip_address?

if !(doc = fetch_data(query)).is_a?(Hash) or doc['error']
[]
else
[doc]
end
end

def reserved_result(ip)
{
'ip' => ip,
'bogon' => true
}
end

def query_url_params(query)
{
token: configuration.api_key
}.merge(super)
end
end
end
16 changes: 16 additions & 0 deletions lib/geocoder/results/ipinfo_io_lite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'geocoder/results/base'

module Geocoder::Result
class IpinfoIoLite < Base

def self.response_attributes
%w(ip asn as_name as_domain country country_code continent continent_code)
end

response_attributes.each do |a|
define_method a do
@data[a]
end
end
end
end
10 changes: 10 additions & 0 deletions test/fixtures/ipinfo_io_lite_8_8_8_8
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"ip": "8.8.8.8",
"asn": "AS15169",
"as_name": "Google LLC",
"as_domain": "google.com",
"country_code": "US",
"country": "United States",
"continent": "North America",
"continent_code": "NA"
}
Empty file.
8 changes: 8 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,14 @@ def default_fixture_filename
end
end

require 'geocoder/lookups/ipinfo_io_lite'
class IpinfoIoLite
private
def default_fixture_filename
"ipinfo_io_lite_8_8_8_8"
end
end

require 'geocoder/lookups/ipregistry'
class Ipregistry
private
Expand Down
2 changes: 1 addition & 1 deletion test/unit/lookup_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_search_returns_empty_array_when_no_results

def test_query_url_contains_values_in_params_hash
Geocoder::Lookup.all_services_except_test.each do |l|
next if [:freegeoip, :maxmind_local, :telize, :pointpin, :geoip2, :maxmind_geoip2, :mapbox, :ipdata_co, :ipinfo_io, :ipapi_com, :ipregistry, :ipstack, :postcodes_io, :uk_ordnance_survey_names, :amazon_location_service, :ipbase, :ip2location_lite].include? l # does not use query string
next if [:freegeoip, :maxmind_local, :telize, :pointpin, :geoip2, :maxmind_geoip2, :mapbox, :ipdata_co, :ipinfo_io, :ipinfo_io_lite, :ipapi_com, :ipregistry, :ipstack, :postcodes_io, :uk_ordnance_survey_names, :amazon_location_service, :ipbase, :ip2location_lite].include? l # does not use query string
set_api_key!(l)
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
"test", :params => {:one_in_the_hand => "two in the bush"}
Expand Down
26 changes: 26 additions & 0 deletions test/unit/lookups/ipinfo_io_lite_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require 'test_helper'

class IpinfoIoLiteTest < GeocoderTestCase
def test_ipinfo_io_lite_lookup_loopback_address
Geocoder.configure(ip_lookup: :ipinfo_io_lite)
result = Geocoder.search('127.0.0.1').first
assert_equal '127.0.0.1', result.ip
end

def test_ipinfo_io_lite_lookup_private_address
Geocoder.configure(ip_lookup: :ipinfo_io_lite)
result = Geocoder.search('172.19.0.1').first
assert_equal '172.19.0.1', result.ip
end

def test_ipinfo_io_lite_extra_attributes
Geocoder.configure(ip_lookup: :ipinfo_io_lite, use_https: true)
result = Geocoder.search('8.8.8.8').first
assert_equal '8.8.8.8', result.ip
assert_equal 'US', result.country_code
assert_equal 'United States', result.country
assert_equal 'North America', result.continent
assert_equal 'NA', result.continent_code
assert_equal 'Google LLC', result.as_name
end
end
2 changes: 2 additions & 0 deletions test/unit/result_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def test_forward_geocoding_result_has_required_attributes
:ip2location, # has pay-per-attribute pricing model
:ip2location_io, # has pay-per-attribute pricing model
:ip2location_lite, # no forward geocoding
:ipinfo_io_lite, # does not support exact location
:twogis, # cant find 'Madison Square Garden'
].include?(l)

Expand All @@ -26,6 +27,7 @@ def test_reverse_geocoding_result_has_required_attributes
:ip2location, # has pay-per-attribute pricing model
:ip2location_io, # has pay-per-attribute pricing model
:ip2location_lite, # no reverse geocoding
:ipinfo_io_lite, # no reverse geocoding
:nationaal_georegister_nl, # no reverse geocoding
:melissa_street, # reverse geocoding not implemented
:twogis, # cant find 'Madison Square Garden'
Expand Down