Skip to content

Commit

Permalink
Merge 5794482 into 5540c0c
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Fiscaletti committed Jun 27, 2019
2 parents 5540c0c + 5794482 commit af6b233
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion ReadMe.md
Expand Up @@ -49,7 +49,7 @@ try {
//
// This function will work with hostnames
// or IP addresses.
$location = $geoLookup->getLocationFor('github.com');
$location = $geoLookup->getLocation('github.com');

// You can alternately look up the information
// for the current client's IP address.
Expand Down
39 changes: 38 additions & 1 deletion src/IPStack/GeoLookup.php
Expand Up @@ -58,7 +58,7 @@ public function __construct(
* @return \FreeGeoIp\PHP\Location|null
* @throws \Exception
*/
public function getLocationFor(string $ip)
public function getLocation(string $ip)
{
$ret = null;

Expand Down Expand Up @@ -87,6 +87,43 @@ public function getLocationFor(string $ip)
return $ret;
}

/**
* Retrieve the location information for a batch of IP addresses.
*
* @param string ...$ips The IP addresses.
*
* @return \FreeGeoIp\PHP\Location|null
* @throws \Exception
*/
public function getLocations(string ...$ips)
{
$ret = null;

try {
$response = (new Client([
'base_uri' => (
($this->use_https)
? 'https'
: 'http'
).'://api.ipstack.com/',
'timeout' => $this->timeout,
]))->get(implode(',', $ips).'?access_key='.$this->api_key.'&output=json');

if ($response->getStatusCode() == 200) {
$compiled = json_decode($response->getBody()->getContents(), true);
if (array_key_exists('error', $compiled)) {
throw new \Exception('Error: '.$compiled['error']['info']);
}

$ret = new Location($compiled);
}
} catch (\Exception $e) {
throw $e;
}

return $ret;
}

/**
* Returns a location for the current clients IP address.
*
Expand Down
20 changes: 16 additions & 4 deletions tests/GeoLookupTest.php
Expand Up @@ -14,7 +14,7 @@ final class GeoLookupTest extends TestCase
public function testGetLocationForReturnsLocationObject()
{
$geo = new GeoLookup('d0164200acfaa5ad0a154d1a7398bc90');
$location = $geo->getLocationFor('github.com');
$location = $geo->getLocation('github.com');

$this->assertInstanceOf(Location::class, $location);
}
Expand All @@ -26,7 +26,7 @@ public function testGetLocationWithHttpsForReturnsLocationObjectOnInvalidPlan()
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Error: Access Restricted - Your current Subscription Plan does not support HTTPS Encryption.');

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

$this->assertInstanceOf(Location::class, $location);
}
Expand All @@ -38,8 +38,20 @@ public function testGetClientLocationOnUnableToFindClientIp()
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Error: Unable to find client IP address.');

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

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

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

$this->expectException(\Exception::class);
$this->expectExceptionmessage('Error: Bulk requests are not supported on your plan. Please upgrade your subscription.');

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

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

0 comments on commit af6b233

Please sign in to comment.