Permalink
Browse files

Adding provider ipstack (#879)

* adding ipstack as a provider

* fixing the styleci errors

* forgot to remove a space for styleci

* adding cache for phpunit

* added locale to the provider

* fix test to pass with deps="low"
  • Loading branch information...
jGielen authored and jbelien committed Sep 14, 2018
1 parent 72c9834 commit d108775b3d36bcf6dcb421de5cd184f711d22618
@@ -164,6 +164,7 @@ Provider | Package | Features | Stats
[IpInfoDB](https://github.com/geocoder-php/ip-info-db-provider) | `geocoder-php/ip-info-db-provider` | IPv4 <br> [Website](http://ipinfodb.com/) | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/ip-info-db-provider/v/stable)](https://packagist.org/packages/geocoder-php/ip-info-db-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/ip-info-db-provider/downloads)](https://packagist.org/packages/geocoder-php/ip-info-db-provider)
[MaxMind](https://github.com/geocoder-php/maxmind-provider) | `geocoder-php/maxmind-provider` | IPv4, IPv6 <br> [Website](https://www.maxmind.com/) | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/maxmind-provider/v/stable)](https://packagist.org/packages/geocoder-php/maxmind-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/maxmind-provider/downloads)](https://packagist.org/packages/geocoder-php/maxmind-provider)
[MaxMind Binary](https://github.com/geocoder-php/maxmind-binary-provider) | `geocoder-php/maxmind-binary-provider` | IPv4, IPv6 <br> [Website](https://www.maxmind.com/) | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/maxmind-binary-provider/v/stable)](https://packagist.org/packages/geocoder-php/maxmind-binary-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/maxmind-binary-provider/downloads)](https://packagist.org/packages/geocoder-php/maxmind-binary-provider)
[ipstack](https://github.com/geocoder-php/ipstack-provider) | `geocoder-php/ipstack-provider` | IPv4, IPv6 <br> [Website](https://ipstack.com/) | [![Latest Stable Version](https://poser.pugx.org/geocoder-php/ipstack-provider/v/stable)](https://packagist.org/packages/geocoder-php/ipstack-provider) <br>[![Total Downloads](https://poser.pugx.org/geocoder-php/ipstack-provider/downloads)](https://packagist.org/packages/geocoder-php/ipstack-provider)
#### Other packages
@@ -475,6 +476,7 @@ following lines and add your own API keys:
<!-- <server name="OPENCAGE_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="PICKPOINT_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="LOCATIONIQ_API_KEY" value="YOUR_API_KEY" /> -->
<!-- <server name="IPSTACK_API_KEY" value="YOUR_API_KEY" /> -->
</php>
```
@@ -34,6 +34,7 @@
<server name="ALGOLIA_APP_ID" value="YOUR_APP_ID" />
<server name="ALGOLIA_API_KEY" value="YOUR_API_KEY" />
<server name="MAPBOX_GEOCODING_KEY" value="YOUR_GEOCODING_KEY" />
<server name="IPSTACK_API_KEY" value="YOUR_API_KEY" />
<!--<server name="MAXMIND_API_KEY" value="YOUR_API_KEY" />-->
</php>
@@ -0,0 +1,4 @@
.gitattributes export-ignore
.travis.yml export-ignore
phpunit.xml.dist export-ignore
Tests/ export-ignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
phpunit.xml
@@ -0,0 +1,16 @@
language: php
sudo: false
php: 7.0
install:
- composer update --prefer-stable --prefer-dist
script:
- composer test-ci
after_success:
- wget https://scrutinizer-ci.com/ocular.phar
- php ocular.phar code-coverage:upload --format=php-clover build/coverage.xml
@@ -0,0 +1,7 @@
# Change Log
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
## 4.0.0
First release of this library.
@@ -0,0 +1,141 @@
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider\Ipstack;
use Geocoder\Collection;
use Geocoder\Exception\InvalidArgument;
use Geocoder\Exception\InvalidCredentials;
use Geocoder\Exception\QuotaExceeded;
use Geocoder\Exception\UnsupportedOperation;
use Geocoder\Model\Address;
use Geocoder\Model\AddressCollection;
use Geocoder\Query\GeocodeQuery;
use Geocoder\Query\ReverseQuery;
use Geocoder\Http\Provider\AbstractHttpProvider;
use Geocoder\Provider\Provider;
use Http\Client\HttpClient;
/**
* @author Jonas Gielen <gielenjonas@gmail.com>
*/
final class Ipstack extends AbstractHttpProvider implements Provider
{
/**
* @var string
*/
const GEOCODE_ENDPOINT_URL = 'http://api.ipstack.com/%s?access_key=%s';
/**
* @var string
*/
private $apiKey;
/**
* @param HttpClient $client an HTTP adapter
* @param string $apiKey an API key
*/
public function __construct(HttpClient $client, string $apiKey)
{
if (empty($apiKey)) {
throw new InvalidCredentials('No API key provided.');
}
$this->apiKey = $apiKey;
parent::__construct($client);
}
/**
* {@inheritdoc}
*/
public function geocodeQuery(GeocodeQuery $query): Collection
{
$address = $query->getText();
// This API doesn't handle IPs
if (!filter_var($address, FILTER_VALIDATE_IP)) {
throw new UnsupportedOperation('The Ipstack provider does not support street addresses.');
}
if (in_array($address, ['127.0.0.1', '::1'])) {
return new AddressCollection([$this->getLocationForLocalhost()]);
}
$url = sprintf(sprintf(self::GEOCODE_ENDPOINT_URL, $address, $this->apiKey));
if (null !== $query->getLocale()) {
$url = sprintf('%s&language=%s', $url, $query->getLocale());
}
$body = $this->getUrlContents($url);
$data = json_decode($body, true);
// https://ipstack.com/documentation#errors
if (isset($data['error'])) {
switch ($data['error']['code']) {
case 301:
throw new InvalidArgument(
'Invalid request (a required parameter is missing).'
);
case 303:
throw new InvalidArgument(
'Bulk requests are not supported on your plan. Please upgrade your subscription.'
);
case 104:
throw new QuotaExceeded(
'The maximum allowed amount of monthly API requests has been reached.'
);
case 101:
throw new InvalidCredentials(
'No API Key was specified or an invalid API Key was specified.'
);
}
}
if (null === $data['latitude']
&& null === $data['longitude']
&& null === $data['city']
&& null === $data['zip']
&& null === $data['country_name']
&& null === $data['country_code']) {
return new AddressCollection([]);
}
$locations[] = Address::createFromArray([
'providedBy' => $this->getName(),
'latitude' => $data['latitude'] ?: null,
'longitude' => $data['longitude'] ?: null,
'locality' => $data['city'] ?: null,
'postalCode' => $data['zip'] ?: null,
'country' => $data['country_name'] ?: null,
'countryCode' => $data['country_code'] ?: null,
]);
return new AddressCollection($locations);
}
/**
* {@inheritdoc}
*/
public function reverseQuery(ReverseQuery $query): Collection
{
throw new UnsupportedOperation('The Ipstack provider is not able to do reverse geocoding.');
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'ipstack';
}
}
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2011 — William Durand <william.durand1@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@@ -0,0 +1,27 @@
# Ipstack Geocoder provider
[![Build Status](https://travis-ci.org/geocoder-php/ipstack-provider.svg?branch=master)](http://travis-ci.org/geocoder-php/ipstack-provider)
[![Latest Stable Version](https://poser.pugx.org/geocoder-php/ipstack-provider/v/stable)](https://packagist.org/packages/geocoder-php/ipstack-provider)
[![Total Downloads](https://poser.pugx.org/geocoder-php/ipstack-provider/downloads)](https://packagist.org/packages/geocoder-php/ipstack-provider)
[![Monthly Downloads](https://poser.pugx.org/geocoder-php/ipstack-provider/d/monthly.png)](https://packagist.org/packages/geocoder-php/ipstack-provider)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/geocoder-php/ipstack-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/ipstack-provider)
[![Quality Score](https://img.shields.io/scrutinizer/g/geocoder-php/ipstack-provider.svg?style=flat-square)](https://scrutinizer-ci.com/g/geocoder-php/ipstack-provider)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
This is the Ipstack provider from the PHP Geocoder. This is a **READ ONLY** repository. See the
[main repo](https://github.com/geocoder-php/Geocoder) for information and documentation.
### Install
```bash
composer require geocoder-php/ipstack-provider
```
### Note
The default language-locale is `en`, you can choose between `de`, `es`, `fr`,
`ja`, `pt-br`, `ru` `zh`.
### Contribute
Contributions are very welcome! Send a pull request to the [main repository](https://github.com/geocoder-php/Geocoder) or
report any issues you find on the [issue tracker](https://github.com/geocoder-php/Geocoder/issues).
@@ -0,0 +1 @@
s:417:"{"ip":"2001:0db8:0000:0042:0000:8a2e:0370:7334","type":"ipv6","continent_code":null,"continent_name":null,"country_code":null,"country_name":null,"region_code":null,"region_name":null,"city":null,"zip":null,"latitude":null,"longitude":null,"location":{"geoname_id":null,"capital":null,"languages":null,"country_flag":null,"country_flag_emoji":null,"country_flag_emoji_unicode":null,"calling_code":null,"is_eu":false}}";
@@ -0,0 +1 @@
s:417:"{"ip":"2001:0db8:0000:0042:0000:8a2e:0370:7334","type":"ipv6","continent_code":null,"continent_name":null,"country_code":null,"country_name":null,"region_code":null,"region_name":null,"city":null,"zip":null,"latitude":null,"longitude":null,"location":{"geoname_id":null,"capital":null,"languages":null,"country_flag":null,"country_flag_emoji":null,"country_flag_emoji_unicode":null,"calling_code":null,"is_eu":false}}";
@@ -0,0 +1 @@
s:556:"{"ip":"83.227.123.8","type":"ipv4","continent_code":"EU","continent_name":"Europe","country_code":"SE","country_name":"Sweden","region_code":"T","region_name":"\u00d6rebro","city":"\u00d6rebro","zip":"703 53","latitude":59.2741,"longitude":15.2066,"location":{"geoname_id":2686657,"capital":"Stockholm","languages":[{"code":"sv","name":"Swedish","native":"Svenska"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/se.svg","country_flag_emoji":"\ud83c\uddf8\ud83c\uddea","country_flag_emoji_unicode":"U+1F1F8 U+1F1EA","calling_code":"46","is_eu":true}}";
@@ -0,0 +1 @@
s:562:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"Am\u00e9rique du Nord","country_code":"US","country_name":"\u00c9tats-Unis","region_code":null,"region_name":null,"city":null,"zip":null,"latitude":37.751,"longitude":-97.822,"location":{"geoname_id":null,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false}}";
@@ -0,0 +1 @@
s:556:"{"ip":"83.227.123.8","type":"ipv4","continent_code":"EU","continent_name":"Europe","country_code":"SE","country_name":"Sweden","region_code":"T","region_name":"\u00d6rebro","city":"\u00d6rebro","zip":"703 53","latitude":59.2741,"longitude":15.2066,"location":{"geoname_id":2686657,"capital":"Stockholm","languages":[{"code":"sv","name":"Swedish","native":"Svenska"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/se.svg","country_flag_emoji":"\ud83c\uddf8\ud83c\uddea","country_flag_emoji_unicode":"U+1F1F8 U+1F1EA","calling_code":"46","is_eu":true}}";
@@ -0,0 +1 @@
s:552:"{"ip":"74.200.247.59","type":"ipv4","continent_code":"NA","continent_name":"North America","country_code":"US","country_name":"United States","region_code":null,"region_name":null,"city":null,"zip":null,"latitude":37.751,"longitude":-97.822,"location":{"geoname_id":null,"capital":"Washington D.C.","languages":[{"code":"en","name":"English","native":"English"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/us.svg","country_flag_emoji":"\ud83c\uddfa\ud83c\uddf8","country_flag_emoji_unicode":"U+1F1FA U+1F1F8","calling_code":"1","is_eu":false}}";
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
/*
* This file is part of the Geocoder package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Geocoder\Provider\Ipstack\Tests;
use Geocoder\IntegrationTest\ProviderIntegrationTest;
use Geocoder\Provider\Ipstack\Ipstack;
use Http\Client\HttpClient;
/**
* @author Jonas Gielen <gielenjonas@gmail.com>
*/
class IntegrationTest extends ProviderIntegrationTest
{
protected $testAddress = false;
protected $testReverse = false;
protected function createProvider(HttpClient $httpClient)
{
return new Ipstack($httpClient, $this->getApiKey());
}
protected function getCacheDir()
{
return __DIR__.'/.cached_responses';
}
protected function getApiKey()
{
return $_SERVER['IPSTACK_API_KEY'];
}
}
Oops, something went wrong.

0 comments on commit d108775

Please sign in to comment.