Skip to content

Commit

Permalink
V1.6.1 (#26)
Browse files Browse the repository at this point in the history
V1.6.1
  • Loading branch information
Nathan Fiscaletti authored Jun 27, 2019
2 parents 2bd0915 + 4b20e3a commit 049e597
Showing 1 changed file with 21 additions and 35 deletions.
56 changes: 21 additions & 35 deletions src/IPStack/GeoLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,6 @@ public function __construct(string $api_key)
*/
public function getLocation(string $ip)
{
$ret = null;

try {
$response = (new Client([
'base_uri' => (
Expand All @@ -93,19 +91,10 @@ public function getLocation(string $ip)
'&language='.$this->language
);

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 = $compiled;
}
return $this->processResponse($response);
} catch (\Exception $e) {
throw $e;
}

return $ret;
}

/**
Expand All @@ -122,8 +111,6 @@ public function getLocations(string ...$ips)
throw new \Exception('Error: Bulk lookup limitted to 50 IP addresses at a time.');
}

$ret = null;

try {
$response = (new Client([
'base_uri' => (
Expand All @@ -140,19 +127,10 @@ public function getLocations(string ...$ips)
'&language='.$this->language
);

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 = $compiled;
}
return $this->processResponse($response);
} catch (\Exception $e) {
throw $e;
}

return $ret;
}

/**
Expand All @@ -163,8 +141,6 @@ public function getLocations(string ...$ips)
*/
public function getOwnLocation()
{
$ret = null;

try {
$response = (new Client([
'base_uri' => (
Expand All @@ -181,19 +157,29 @@ public function getOwnLocation()
'&language='.$this->language
);

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 = $compiled;
}
return $this->processResponse($response);
} catch (\Exception $e) {
throw $e;
}
}

/**
* Processes a response to be sent back to the user.
*
* @param object $response
*
* @return array|null
*/
private function processResponse($response)
{
if ($response->getStatusCode() == 200) {
$compiled = json_decode($response->getBody()->getContents(), true);
if (array_key_exists('error', $compiled)) {
throw new \Exception('Error: '.$compiled['error']['info']);
}

return $ret;
return $compiled;
}
}

/**
Expand Down

0 comments on commit 049e597

Please sign in to comment.