Skip to content

Commit

Permalink
Removed the dependency of extended arrays (#20)
Browse files Browse the repository at this point in the history
Removed the dependency of extended arrays
  • Loading branch information
Nathan Fiscaletti committed Jun 27, 2019
2 parents ff0339d + 8217682 commit 69fb425
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 120 deletions.
32 changes: 15 additions & 17 deletions ReadMe.md
Expand Up @@ -35,10 +35,6 @@ $geoLookup = new GeoLookup(

#### Lookup a location for an IP Address

> Note: Locations are returned using a library called ExtendedArrays.
> This library gives us more options on how we access the properties of the location.
> See the [Acessing Array Elements](https://github.com/nathan-fiscaletti/extended-arrays/blob/master/Examples/Managing%20Arrays.md#accessing-array-elements) portion of the ExtendedArrays documentation for more information on this.
```php
// Lookup a location for an IP Address
// and catch any exceptions that might
Expand All @@ -51,29 +47,31 @@ try {
// or IP addresses.
$location = $geoLookup->getLocation('github.com');

// You can alternately look up the information
// for the current client's IP address.
$location = $geoLookup->getClientLocation();

// If we are unable to retrieve the location information
// for an IP address, null will be returned.
if ($location == null) {
if (\is_null($location)) {
echo 'Failed to find location.'.PHP_EOL;
} else {
// Convert the location to a standard PHP array.
print_r($location->_asStdArray());

// Any of these formats will work for
// retrieving a property.
echo $location->latitude . PHP_EOL;
echo $location['longitude'] . PHP_EOL;
echo $location->region_name() . PHP_EOL;
// Print the Location Object.
print_r($location);
}
} catch (\Exception $e) {
echo $e->getMessage();
}
```

#### Lookup IPs locations in bulk

You can also look up the location for multiple IP addresses at once.

> Note: This requires the PROFESSIONAL teir API key or higher and is limitted to 50 IPs at a time.
```php
$lookup = ['google.com', 'github.com', '1.1.1.1'];
$locations = $geoLookup->getLocations(...$lookup);
print_r($locations);
```

#### Using the the Legacy [FreeGeoIP Binary](https://github.com/fiorix/freegeoip/releases/)

You can still use the legacy FreeGeoIP Binary hosted on a server
Expand Down
12 changes: 8 additions & 4 deletions src/IPStack/GeoLookup.php
Expand Up @@ -55,7 +55,7 @@ public function __construct(
*
* @param string $ip
*
* @return \FreeGeoIp\PHP\Location|null
* @return array|null
* @throws \Exception
*/
public function getLocation(string $ip)
Expand All @@ -78,7 +78,7 @@ public function getLocation(string $ip)
throw new \Exception('Error: '.$compiled['error']['info']);
}

$ret = new Location($compiled);
$ret = $compiled;
}
} catch (\Exception $e) {
throw $e;
Expand All @@ -92,11 +92,15 @@ public function getLocation(string $ip)
*
* @param string ...$ips The IP addresses.
*
* @return \FreeGeoIp\PHP\Location|null
* @return array|null
* @throws \Exception
*/
public function getLocations(string ...$ips)
{
if (\count($ips) > 50) {
throw new \Exception('Error: Bulk lookup limitted to 50 IP addresses at a time.');
}

$ret = null;

try {
Expand All @@ -115,7 +119,7 @@ public function getLocations(string ...$ips)
throw new \Exception('Error: '.$compiled['error']['info']);
}

$ret = new Location($compiled);
$ret = $compiled;
}
} catch (\Exception $e) {
throw $e;
Expand Down
10 changes: 5 additions & 5 deletions src/IPStack/Legacy/FreeGeoIp.php
Expand Up @@ -51,10 +51,10 @@ public function __construct(
*
* @param string $ip
*
* @return \FreeGeoIp\PHP\Location|null
* @return array|null
* @throws \Exception
*/
public function getLocationFor(string $ip)
public function getLocation(string $ip)
{
$ret = null;
try {
Expand All @@ -63,8 +63,8 @@ public function getLocationFor(string $ip)
'timeout' => $this->timeout,
]))->get('json/'.$ip);
if ($response->getStatusCode() == 200) {
$ret = new Location(json_decode(
$response->getBody()->getContents(), true));
$ret = json_decode(
$response->getBody()->getContents(), true);
}
} catch (\Exception $e) {
throw $e;
Expand All @@ -76,7 +76,7 @@ public function getLocationFor(string $ip)
/**
* Returns a location for the current clients IP address.
*
* @return \FreeGeoIp\PHP\Location
* @return array|null
* @throws \Exception
*/
public function getClientLocation()
Expand Down
49 changes: 0 additions & 49 deletions src/IPStack/Location.php

This file was deleted.

21 changes: 18 additions & 3 deletions tests/GeoLookupTest.php
Expand Up @@ -16,7 +16,7 @@ public function testGetLocationForReturnsLocationObject()
$geo = new GeoLookup('d0164200acfaa5ad0a154d1a7398bc90');
$location = $geo->getLocation('github.com');

$this->assertInstanceOf(Location::class, $location);
$this->assertInternalType('array', $location);
}

public function testGetLocationWithHttpsForReturnsLocationObjectOnInvalidPlan()
Expand All @@ -28,7 +28,7 @@ public function testGetLocationWithHttpsForReturnsLocationObjectOnInvalidPlan()

$location = $geo->getLocation('github.com');

$this->assertInstanceOf(Location::class, $location);
$this->assertInternalType('array', $location);
}

public function testGetClientLocationOnUnableToFindClientIp()
Expand All @@ -52,6 +52,21 @@ public function testGetLocationsBulkRequestReturnsErrorOnMissingAPIPermissions()

$location = $geo->getLocations('1.1.1.1', '2.2.2.2');

$this->assertInstanceOf(Location::class, $location);
$this->assertInternalType('array', $location);
}

public function testGetLocationsBulkRequestReturnsExceptionOnMoreThan50IPs()
{
$geo = new GeoLookup('d0164200acfaa5ad0a154d1a7398bc90');

$this->expectException(\Exception::class);
$this->expectExceptionmessage('Error: Bulk lookup limitted to 50 IP addresses at a time.');

$input = [];
$count = 51;
while ($count--) {
$input[] = '1.1.1.1';
}
$location = $geo->getLocations(...$input);
}
}
42 changes: 0 additions & 42 deletions tests/LocationTest.php

This file was deleted.

0 comments on commit 69fb425

Please sign in to comment.