From 73aa58d017a58f920526d80541f0d36b4306af30 Mon Sep 17 00:00:00 2001 From: ip2location Date: Wed, 25 Oct 2023 11:56:15 +0800 Subject: [PATCH 1/9] Add IP2Location.io Driver --- config/location.php | 5 ++++ readme.md | 1 + src/Drivers/Ip2locationio.php | 56 +++++++++++++++++++++++++++++++++++ tests/Ip2locationioTest.php | 53 +++++++++++++++++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 src/Drivers/Ip2locationio.php create mode 100644 tests/Ip2locationioTest.php diff --git a/config/location.php b/config/location.php index bd12adc..5212419 100644 --- a/config/location.php +++ b/config/location.php @@ -26,6 +26,7 @@ */ 'fallbacks' => [ + Stevebauman\Location\Drivers\Ip2locationio::class, Stevebauman\Location\Drivers\IpInfo::class, Stevebauman\Location\Drivers\GeoPlugin::class, Stevebauman\Location\Drivers\MaxMind::class, @@ -102,6 +103,10 @@ 'token' => env('IPDATA_TOKEN'), ], + 'ip2locationio' => [ + 'token' => env('IP2LOCATIONIO_TOKEN'), + ], + /* |-------------------------------------------------------------------------- | Kloudend ~ ipapi.co Configuration diff --git a/readme.md b/readme.md index 586c609..e19ba09 100644 --- a/readme.md +++ b/readme.md @@ -83,6 +83,7 @@ Available drivers: - [GeoPlugin](http://www.geoplugin.com) - [MaxMind](https://www.maxmind.com/en/home) - [Cloudflare](https://support.cloudflare.com/hc/en-us/articles/200168236-Configuring-IP-geolocation) +- [IP2Location.io](https://www.ip2location.io/) #### Setting up MaxMind with a self-hosted database (optional) diff --git a/src/Drivers/Ip2locationio.php b/src/Drivers/Ip2locationio.php new file mode 100644 index 0000000..4a8e4e7 --- /dev/null +++ b/src/Drivers/Ip2locationio.php @@ -0,0 +1,56 @@ +countryName = $location->country_name; + $position->countryCode = $location->country_code; + $position->regionCode = $location->region['code'] ?? null; + $position->regionName = $location->region_name; + $position->cityName = $location->city_name; + $position->zipCode = $location->zip_code; + $position->postalCode = $location->zip_code; + $position->latitude = (string) $location->latitude; + $position->longitude = (string) $location->longitude; + $position->timezone = $location->time_zone; + $position->currencyCode = $location->country['currency']['code'] ?? null; + $position->metroCode = $location->geotargeting['metro'] ?? null; + $position->areaCode = $location->area_code ?? null; + $position->isp = $location->isp ?? null; + $position->asn = $location->asn ?? null; + $position->asName = $location->as ?? null; + $position->domain = $location->domain ?? null; + $position->netSpeed = $location->net_speed ?? null; + $position->iddCode = $location->idd_code ?? null; + $position->weatherStationCode = $location->weather_station_code ?? null; + $position->weatherStationName = $location->weather_station_name ?? null; + $position->mcc = $location->mcc ?? null; + $position->mnc = $location->mnc ?? null; + $position->mobileBrand = $location->mobile_brand ?? null; + $position->elevation = $location->elevation ?? null; + $position->usageType = $location->usage_type ?? null; + $position->addressType = $location->address_type ?? null; + $position->isProxy = $location->is_proxy ?? null; + + return $position; + } +} \ No newline at end of file diff --git a/tests/Ip2locationioTest.php b/tests/Ip2locationioTest.php new file mode 100644 index 0000000..51354fa --- /dev/null +++ b/tests/Ip2locationioTest.php @@ -0,0 +1,53 @@ +makePartial(); + + $response = new Fluent([ + 'country_name' => 'United States of America', + 'country_code' => 'US', + 'region_name' => 'California', + 'city_name' => 'Mountain View', + 'zip' => '94043', + 'lat' => '37.405992', + 'lon' => '-122.078515', + 'timezone' => '-07:00', + ]); + + $driver + ->shouldAllowMockingProtectedMethods() + ->shouldReceive('process')->once()->andReturn($response); + + Location::setDriver($driver); + + $position = Location::get(); + + expect($position)->toBeInstanceOf(Position::class); + + expect($position->toArray())->toEqual([ + 'countryName' => 'United States', + 'countryCode' => 'US', + 'regionCode' => null, + 'regionName' => 'California', + 'cityName' => 'Mountain View', + 'zipCode' => '94043', + 'isoCode' => null, + 'postalCode' => null, + 'latitude' => '37.405992', + 'longitude' => '-122.078515', + 'metroCode' => null, + 'areaCode' => 'CA', + 'ip' => '66.102.0.0', + 'currencyCode' => null, + 'timezone' => '-07:00', + 'driver' => get_class($driver), + ]); +}); From a0594f509e090dbd4cbeb882d433b10c54cc4af7 Mon Sep 17 00:00:00 2001 From: IP2Location Date: Fri, 3 Nov 2023 13:06:24 +0800 Subject: [PATCH 2/9] Update Ip2locationio.php --- src/Drivers/Ip2locationio.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Drivers/Ip2locationio.php b/src/Drivers/Ip2locationio.php index 4a8e4e7..8271c78 100644 --- a/src/Drivers/Ip2locationio.php +++ b/src/Drivers/Ip2locationio.php @@ -53,4 +53,4 @@ protected function hydrate(Position $position, Fluent $location): Position return $position; } -} \ No newline at end of file +} From 8d2962b07bf10aae3591ecb8c9093e0a76a33bfe Mon Sep 17 00:00:00 2001 From: ip2location Date: Thu, 23 Nov 2023 10:48:34 +0800 Subject: [PATCH 3/9] Update Ip2locationioTest.php --- tests/Ip2locationioTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Ip2locationioTest.php b/tests/Ip2locationioTest.php index 51354fa..a8eb25a 100644 --- a/tests/Ip2locationioTest.php +++ b/tests/Ip2locationioTest.php @@ -33,11 +33,11 @@ expect($position)->toBeInstanceOf(Position::class); expect($position->toArray())->toEqual([ - 'countryName' => 'United States', - 'countryCode' => 'US', + 'country_name' => 'United States', + 'country_code' => 'US', 'regionCode' => null, - 'regionName' => 'California', - 'cityName' => 'Mountain View', + 'region_name' => 'California', + 'city_name' => 'Mountain View', 'zipCode' => '94043', 'isoCode' => null, 'postalCode' => null, From 58ed478b1f391345c887effd52447bfaaf31e3d4 Mon Sep 17 00:00:00 2001 From: ip2location Date: Thu, 23 Nov 2023 10:50:53 +0800 Subject: [PATCH 4/9] Update Ip2locationioTest.php --- tests/Ip2locationioTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Ip2locationioTest.php b/tests/Ip2locationioTest.php index a8eb25a..6212f79 100644 --- a/tests/Ip2locationioTest.php +++ b/tests/Ip2locationioTest.php @@ -33,11 +33,11 @@ expect($position)->toBeInstanceOf(Position::class); expect($position->toArray())->toEqual([ - 'country_name' => 'United States', - 'country_code' => 'US', + 'countryName' => 'United States of America', + 'countryCode' => 'US', 'regionCode' => null, - 'region_name' => 'California', - 'city_name' => 'Mountain View', + 'regionName' => 'California', + 'cityName' => 'Mountain View', 'zipCode' => '94043', 'isoCode' => null, 'postalCode' => null, From 6346665049415f1aa424eb51b269603076e4b764 Mon Sep 17 00:00:00 2001 From: ip2location Date: Thu, 23 Nov 2023 10:52:07 +0800 Subject: [PATCH 5/9] Update Ip2locationioTest.php --- tests/Ip2locationioTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Ip2locationioTest.php b/tests/Ip2locationioTest.php index 6212f79..d612a52 100644 --- a/tests/Ip2locationioTest.php +++ b/tests/Ip2locationioTest.php @@ -12,8 +12,8 @@ $driver = m::mock(IpApi::class)->makePartial(); $response = new Fluent([ - 'country_name' => 'United States of America', - 'country_code' => 'US', + 'countryName' => 'United States of America', + 'countryCode' => 'US', 'region_name' => 'California', 'city_name' => 'Mountain View', 'zip' => '94043', @@ -44,7 +44,7 @@ 'latitude' => '37.405992', 'longitude' => '-122.078515', 'metroCode' => null, - 'areaCode' => 'CA', + 'areaCode' => null, 'ip' => '66.102.0.0', 'currencyCode' => null, 'timezone' => '-07:00', From bdec9606a63bf1fb1d87159963603e733444d9ff Mon Sep 17 00:00:00 2001 From: ip2location Date: Thu, 23 Nov 2023 10:53:21 +0800 Subject: [PATCH 6/9] Update Ip2locationioTest.php --- tests/Ip2locationioTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Ip2locationioTest.php b/tests/Ip2locationioTest.php index d612a52..5b0d6f7 100644 --- a/tests/Ip2locationioTest.php +++ b/tests/Ip2locationioTest.php @@ -14,8 +14,8 @@ $response = new Fluent([ 'countryName' => 'United States of America', 'countryCode' => 'US', - 'region_name' => 'California', - 'city_name' => 'Mountain View', + 'regionName' => 'California', + 'cityName' => 'Mountain View', 'zip' => '94043', 'lat' => '37.405992', 'lon' => '-122.078515', From 46423e9549a7aca39425c4882817658b8dbf490e Mon Sep 17 00:00:00 2001 From: ip2location Date: Thu, 23 Nov 2023 10:59:07 +0800 Subject: [PATCH 7/9] Update Ip2locationioTest.php --- tests/Ip2locationioTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/Ip2locationioTest.php b/tests/Ip2locationioTest.php index 5b0d6f7..efcbc9e 100644 --- a/tests/Ip2locationioTest.php +++ b/tests/Ip2locationioTest.php @@ -4,18 +4,18 @@ use Illuminate\Support\Fluent; use Mockery as m; -use Stevebauman\Location\Drivers\IpApi; +use Stevebauman\Location\Drivers\Ip2locationio; use Stevebauman\Location\Facades\Location; use Stevebauman\Location\Position; it('it can process fluent response', function () { - $driver = m::mock(IpApi::class)->makePartial(); + $driver = m::mock(Ip2locationio::class)->makePartial(); $response = new Fluent([ - 'countryName' => 'United States of America', - 'countryCode' => 'US', - 'regionName' => 'California', - 'cityName' => 'Mountain View', + 'country_name' => 'United States of America', + 'country_code' => 'US', + 'region_name' => 'California', + 'city_name' => 'Mountain View', 'zip' => '94043', 'lat' => '37.405992', 'lon' => '-122.078515', From 6f2d01d4ae577a5260458011d80721561c8e9b19 Mon Sep 17 00:00:00 2001 From: ip2location Date: Thu, 23 Nov 2023 11:01:10 +0800 Subject: [PATCH 8/9] Update Ip2locationioTest.php --- tests/Ip2locationioTest.php | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/tests/Ip2locationioTest.php b/tests/Ip2locationioTest.php index efcbc9e..5fbf106 100644 --- a/tests/Ip2locationioTest.php +++ b/tests/Ip2locationioTest.php @@ -16,10 +16,10 @@ 'country_code' => 'US', 'region_name' => 'California', 'city_name' => 'Mountain View', - 'zip' => '94043', - 'lat' => '37.405992', - 'lon' => '-122.078515', - 'timezone' => '-07:00', + 'zip_code' => '94043', + 'latitude' => '37.405992', + 'longitude' => '-122.078515', + 'time_zone' => '-07:00', ]); $driver @@ -48,6 +48,21 @@ 'ip' => '66.102.0.0', 'currencyCode' => null, 'timezone' => '-07:00', + 'isp' => null, + 'asn' => null, + 'asName' => null, + 'domain' => null, + 'netSpeed' => null, + 'iddCode' => null, + 'weatherStationCode' => null, + 'weatherStationName' => null, + 'mcc' => null, + 'mnc' => null, + 'mobileBrand' => null, + 'elevation' => null, + 'usageType' => null, + 'addressType' => null, + 'isProxy' => null, 'driver' => get_class($driver), ]); }); From f8c96791e71746447ea522efe7d9a6d92551976c Mon Sep 17 00:00:00 2001 From: ip2location Date: Thu, 23 Nov 2023 11:02:13 +0800 Subject: [PATCH 9/9] Update Ip2locationioTest.php --- tests/Ip2locationioTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Ip2locationioTest.php b/tests/Ip2locationioTest.php index 5fbf106..ba2e110 100644 --- a/tests/Ip2locationioTest.php +++ b/tests/Ip2locationioTest.php @@ -40,7 +40,7 @@ 'cityName' => 'Mountain View', 'zipCode' => '94043', 'isoCode' => null, - 'postalCode' => null, + 'postalCode' => '94043', 'latitude' => '37.405992', 'longitude' => '-122.078515', 'metroCode' => null,