Skip to content

Commit 66987e1

Browse files
authored
Merge pull request #1686 from yogaeasy/ipinfo-io-lite
Add IPinfo Lite service
2 parents 9769b16 + 0a62183 commit 66987e1

File tree

10 files changed

+119
-1
lines changed

10 files changed

+119
-1
lines changed

README_API_GUIDE.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,19 @@ IP Address Lookups
655655
* **Documentation**: https://ipinfo.io/developers
656656
* **Terms of Service**: https://ipinfo.io/terms-of-service
657657

658+
### IPInfo.io Lite (`:ipinfo_io_lite`)
659+
660+
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.
661+
662+
* **API key**: required
663+
* **Quota**: none
664+
* **Region**: world
665+
* **SSL support**: yes
666+
* **Languages**: English
667+
* **Documentation**: https://ipinfo.io/developers/lite-api
668+
* **Terms of Service**: https://ipinfo.io/terms-of-service
669+
* **Limitations**: country-level geolocation information and basic ASN information only
670+
658671
### IPQualityScore (`:ipqualityscore`)
659672

660673
* **API key**: required - see https://www.ipqualityscore.com/free-ip-lookup-proxy-vpn-test

lib/geocoder/lookup.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def ip_services
8787
:pointpin,
8888
:maxmind_geoip2,
8989
:ipinfo_io,
90+
:ipinfo_io_lite,
9091
:ipregistry,
9192
:ipapi_com,
9293
:ipdata_co,
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
require 'geocoder/lookups/base'
2+
require 'geocoder/results/ipinfo_io_lite'
3+
4+
module Geocoder::Lookup
5+
class IpinfoIoLite < Base
6+
def name
7+
'Ipinfo.io Lite'
8+
end
9+
10+
private # ---------------------------------------------------------------
11+
12+
def base_query_url(query)
13+
url = "#{protocol}://api.ipinfo.io/lite/#{query.sanitized_text}"
14+
url << '?' if configuration.api_key
15+
url
16+
end
17+
18+
def results(query)
19+
# don't look up a loopback or private address, just return the stored result
20+
return [reserved_result(query.text)] if query.internal_ip_address?
21+
22+
if !(doc = fetch_data(query)).is_a?(Hash) or doc['error']
23+
[]
24+
else
25+
[doc]
26+
end
27+
end
28+
29+
def reserved_result(ip)
30+
{
31+
'ip' => ip,
32+
'bogon' => true
33+
}
34+
end
35+
36+
def query_url_params(query)
37+
{
38+
token: configuration.api_key
39+
}.merge(super)
40+
end
41+
end
42+
end
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'geocoder/results/base'
2+
3+
module Geocoder::Result
4+
class IpinfoIoLite < Base
5+
6+
def self.response_attributes
7+
%w(ip asn as_name as_domain country country_code continent continent_code)
8+
end
9+
10+
response_attributes.each do |a|
11+
define_method a do
12+
@data[a]
13+
end
14+
end
15+
end
16+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"ip": "8.8.8.8",
3+
"asn": "AS15169",
4+
"as_name": "Google LLC",
5+
"as_domain": "google.com",
6+
"country_code": "US",
7+
"country": "United States",
8+
"continent": "North America",
9+
"continent_code": "NA"
10+
}

test/fixtures/ipinfo_io_lite_no_results

Whitespace-only changes.

test/test_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,14 @@ def default_fixture_filename
478478
end
479479
end
480480

481+
require 'geocoder/lookups/ipinfo_io_lite'
482+
class IpinfoIoLite
483+
private
484+
def default_fixture_filename
485+
"ipinfo_io_lite_8_8_8_8"
486+
end
487+
end
488+
481489
require 'geocoder/lookups/ipregistry'
482490
class Ipregistry
483491
private

test/unit/lookup_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_search_returns_empty_array_when_no_results
3232

3333
def test_query_url_contains_values_in_params_hash
3434
Geocoder::Lookup.all_services_except_test.each do |l|
35-
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
35+
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
3636
set_api_key!(l)
3737
url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
3838
"test", :params => {:one_in_the_hand => "two in the bush"}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
require 'test_helper'
2+
3+
class IpinfoIoLiteTest < GeocoderTestCase
4+
def test_ipinfo_io_lite_lookup_loopback_address
5+
Geocoder.configure(ip_lookup: :ipinfo_io_lite)
6+
result = Geocoder.search('127.0.0.1').first
7+
assert_equal '127.0.0.1', result.ip
8+
end
9+
10+
def test_ipinfo_io_lite_lookup_private_address
11+
Geocoder.configure(ip_lookup: :ipinfo_io_lite)
12+
result = Geocoder.search('172.19.0.1').first
13+
assert_equal '172.19.0.1', result.ip
14+
end
15+
16+
def test_ipinfo_io_lite_extra_attributes
17+
Geocoder.configure(ip_lookup: :ipinfo_io_lite, use_https: true)
18+
result = Geocoder.search('8.8.8.8').first
19+
assert_equal '8.8.8.8', result.ip
20+
assert_equal 'US', result.country_code
21+
assert_equal 'United States', result.country
22+
assert_equal 'North America', result.continent
23+
assert_equal 'NA', result.continent_code
24+
assert_equal 'Google LLC', result.as_name
25+
end
26+
end

test/unit/result_test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def test_forward_geocoding_result_has_required_attributes
99
:ip2location, # has pay-per-attribute pricing model
1010
:ip2location_io, # has pay-per-attribute pricing model
1111
:ip2location_lite, # no forward geocoding
12+
:ipinfo_io_lite, # does not support exact location
1213
:twogis, # cant find 'Madison Square Garden'
1314
].include?(l)
1415

@@ -26,6 +27,7 @@ def test_reverse_geocoding_result_has_required_attributes
2627
:ip2location, # has pay-per-attribute pricing model
2728
:ip2location_io, # has pay-per-attribute pricing model
2829
:ip2location_lite, # no reverse geocoding
30+
:ipinfo_io_lite, # no reverse geocoding
2931
:nationaal_georegister_nl, # no reverse geocoding
3032
:melissa_street, # reverse geocoding not implemented
3133
:twogis, # cant find 'Madison Square Garden'

0 commit comments

Comments
 (0)