Skip to content
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

Add IP2Location.io Driver #150

Merged
merged 10 commits into from
Nov 23, 2023
5 changes: 5 additions & 0 deletions config/location.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -102,6 +103,10 @@
'token' => env('IPDATA_TOKEN'),
],

'ip2locationio' => [
'token' => env('IP2LOCATIONIO_TOKEN'),
],

/*
|--------------------------------------------------------------------------
| Kloudend ~ ipapi.co Configuration
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
56 changes: 56 additions & 0 deletions src/Drivers/Ip2locationio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Stevebauman\Location\Drivers;

use Illuminate\Support\Fluent;
use Stevebauman\Location\Position;

class Ip2locationio extends HttpDriver
{
/**
* {@inheritdoc}
*/
public function url(string $ip): string
{
$token = config('location.ip2locationio.token');

return "https://api.ip2location.io/?key={$token}&ip={$ip}";
}

/**
* {@inheritdoc}
*/
protected function hydrate(Position $position, Fluent $location): Position
{
$position->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;
}
}
68 changes: 68 additions & 0 deletions tests/Ip2locationioTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Stevebauman\Location\Tests;

use Illuminate\Support\Fluent;
use Mockery as m;
use Stevebauman\Location\Drivers\Ip2locationio;
use Stevebauman\Location\Facades\Location;
use Stevebauman\Location\Position;

it('it can process fluent response', function () {
$driver = m::mock(Ip2locationio::class)->makePartial();

$response = new Fluent([
'country_name' => 'United States of America',
'country_code' => 'US',
'region_name' => 'California',
'city_name' => 'Mountain View',
'zip_code' => '94043',
'latitude' => '37.405992',
'longitude' => '-122.078515',
'time_zone' => '-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 of America',
'countryCode' => 'US',
'regionCode' => null,
'regionName' => 'California',
'cityName' => 'Mountain View',
'zipCode' => '94043',
'isoCode' => null,
'postalCode' => '94043',
'latitude' => '37.405992',
'longitude' => '-122.078515',
'metroCode' => null,
'areaCode' => null,
'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),
]);
});
Loading