-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from ip2location/master
Add IP2Location.io Driver
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
]); | ||
}); |