Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7fcf0d1

Browse files
committed
Provided a way to use IpInfoDB country precision
This is faster and the recommended way when only requiring the country or country code.
1 parent a1e07fe commit 7fcf0d1

File tree

5 files changed

+98
-8
lines changed

5 files changed

+98
-8
lines changed

src/Geocoder/Provider/IpInfoDb.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Geocoder\Exception\InvalidCredentials;
1414
use Geocoder\Exception\NoResult;
1515
use Geocoder\Exception\UnsupportedOperation;
16+
use Geocoder\Exception\InvalidArgument;
1617
use Ivory\HttpAdapter\HttpAdapterInterface;
1718

1819
/**
@@ -23,22 +24,50 @@ class IpInfoDb extends AbstractHttpProvider implements Provider
2324
/**
2425
* @var string
2526
*/
26-
const ENDPOINT_URL = 'http://api.ipinfodb.com/v3/ip-city/?key=%s&format=json&ip=%s';
27+
const CITY_PRECISION_ENDPOINT_URL = 'http://api.ipinfodb.com/v3/ip-city/?key=%s&format=json&ip=%s';
28+
29+
/**
30+
* @var string
31+
*/
32+
const COUNTRY_PRECISION_ENDPOINT_URL = 'http://api.ipinfodb.com/v3/ip-country/?key=%s&format=json&ip=%s';
2733

2834
/**
2935
* @var string
3036
*/
3137
private $apiKey;
3238

3339
/**
34-
* @param HttpAdapterInterface $adapter An HTTP adapter
35-
* @param string $apiKey An API key
40+
* @var string
41+
*/
42+
private $endpointUrl;
43+
44+
/**
45+
* @param HttpAdapterInterface $adapter An HTTP adapter.
46+
* @param string $apiKey An API key.
47+
* @param string $precision The endpoint precision. Either "city" or "country" (faster)
48+
*
49+
* @throws Geocoder\Exception\InvalidArgument
3650
*/
37-
public function __construct(HttpAdapterInterface $adapter, $apiKey)
51+
public function __construct(HttpAdapterInterface $adapter, $apiKey, $precision = 'city')
3852
{
3953
parent::__construct($adapter);
4054

4155
$this->apiKey = $apiKey;
56+
switch ($precision) {
57+
case 'city':
58+
$this->endpointUrl = self::CITY_PRECISION_ENDPOINT_URL;
59+
break;
60+
61+
case 'country':
62+
$this->endpointUrl = self::COUNTRY_PRECISION_ENDPOINT_URL;
63+
break;
64+
65+
default:
66+
throw new InvalidArgument(sprintf(
67+
'Invalid precision value "%s" (allowed values: "city", "country").',
68+
$precision
69+
));
70+
}
4271
}
4372

4473
/**
@@ -47,11 +76,12 @@ public function __construct(HttpAdapterInterface $adapter, $apiKey)
4776
public function geocode($address)
4877
{
4978
if (null === $this->apiKey) {
50-
throw new InvalidCredentials('No API Key provided');
79+
throw new InvalidCredentials('No API Key provided.');
5180
}
5281

5382
if (!filter_var($address, FILTER_VALIDATE_IP)) {
5483
throw new UnsupportedOperation('The IpInfoDb provider does not support street addresses, only IPv4 addresses.');
84+
5585
}
5686

5787
// This API does not support IPv6
@@ -63,7 +93,7 @@ public function geocode($address)
6393
return $this->returnResults([ $this->getLocalhostDefaults() ]);
6494
}
6595

66-
$query = sprintf(self::ENDPOINT_URL, $this->apiKey, $address);
96+
$query = sprintf($this->endpointUrl, $this->apiKey, $address);
6797

6898
return $this->executeQuery($query);
6999
}
@@ -86,6 +116,8 @@ public function getName()
86116

87117
/**
88118
* @param string $query
119+
*
120+
* @return \Geocoder\Model\Address[]
89121
*/
90122
private function executeQuery($query)
91123
{
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
s:136:"{
2+
"statusCode" : "OK",
3+
"statusMessage" : "",
4+
"ipAddress" : "74.125.45.100",
5+
"countryCode" : "US",
6+
"countryName" : "UNITED STATES"
7+
}";
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
s:294:"{
2+
"statusCode" : "OK",
3+
"statusMessage" : "",
4+
"ipAddress" : "74.125.45.100",
5+
"countryCode" : "US",
6+
"countryName" : "UNITED STATES",
7+
"regionName" : "CALIFORNIA",
8+
"cityName" : "MOUNTAIN VIEW",
9+
"zipCode" : "94043",
10+
"latitude" : "37.406",
11+
"longitude" : "-122.079",
12+
"timeZone" : "-08:00"
13+
}";

tests/Geocoder/Tests/CachedResponseAdapter.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Ivory\HttpAdapter\HttpAdapterInterface;
77
use Ivory\HttpAdapter\Message\InternalRequestInterface;
88
use Ivory\HttpAdapter\Message\Stream\StringStream;
9+
use Ivory\HttpAdapter\Message\RequestInterface;
910

1011
class CachedResponseAdapter extends AbstractHttpAdapter
1112
{
@@ -44,7 +45,7 @@ protected function doSend(InternalRequestInterface $internalRequest)
4445
$body = new StringStream($content);
4546

4647
$response = $this->adapter->getConfiguration()->getMessageFactory()->createResponse(
47-
200, 'OK', '1.1', [], $body
48+
200, 'OK', RequestInterface::PROTOCOL_VERSION_1_1, [], $body
4849
);
4950

5051
if (!empty($content)) {

tests/Geocoder/Tests/Provider/IpInfoDbTest.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77

88
class IpInfoDbTest extends TestCase
99
{
10+
/**
11+
* @expectedException \Geocoder\Exception\InvalidArgument
12+
* @expectedExceptionMessage Invalid precision value "foo" (allowed values: "city", "country").
13+
*/
14+
public function testConstructWithInvalidPrecision()
15+
{
16+
new IpInfoDb($this->getMockAdapter($this->never()), 'api_key', 'foo');
17+
}
18+
1019
public function testGetName()
1120
{
1221
$provider = new IpInfoDb($this->getMockAdapter($this->never()), 'api_key');
@@ -136,7 +145,7 @@ public function testGeocodeWithRealIPv4()
136145
$this->assertEquals('CALIFORNIA', $result->getRegion()->getName());
137146
$this->assertEquals('UNITED STATES', $result->getCountry()->getName());
138147
$this->assertEquals('US', $result->getCountry()->getCode());
139-
$this->assertEquals('America/Denver', $result->getTimezone());
148+
$this->assertEquals('America/Los_Angeles', $result->getTimezone());
140149
}
141150

142151
/**
@@ -153,6 +162,34 @@ public function testGeocodeWithRealIPv6()
153162
$provider->geocode('::ffff:74.125.45.100');
154163
}
155164

165+
/**
166+
* @group temp
167+
*/
168+
public function testGetGeocodedDataWithCountryPrecision()
169+
{
170+
if (!isset($_SERVER['IPINFODB_API_KEY'])) {
171+
$this->markTestSkipped('You need to configure the IPINFODB_API_KEY value in phpunit.xml');
172+
}
173+
174+
$provider = new IpInfoDb($this->getAdapter(), $_SERVER['IPINFODB_API_KEY'], 'country');
175+
$results = $provider->geocode('74.125.45.100');
176+
177+
$this->assertInternalType('array', $results);
178+
$this->assertCount(1, $results);
179+
180+
/** @var \Geocoder\Model\Address $result */
181+
$result = $results[0];
182+
$this->assertInstanceOf('\Geocoder\Model\Address', $result);
183+
$this->assertNull($result->getLatitude());
184+
$this->assertNull($result->getLongitude());
185+
$this->assertNull($result->getPostalCode());
186+
$this->assertNull($result->getLocality());
187+
$this->assertNull($result->getRegion()->getName());
188+
$this->assertEquals('UNITED STATES', $result->getCountry()->getName());
189+
$this->assertEquals('US', $result->getCountry()->getCode());
190+
$this->assertNull($result->getTimezone());
191+
}
192+
156193
/**
157194
* @expectedException \Geocoder\Exception\UnsupportedOperation
158195
* @expectedExceptionMessage The IpInfoDb provider is not able to do reverse geocoding.

0 commit comments

Comments
 (0)