Skip to content

Commit

Permalink
First working concept
Browse files Browse the repository at this point in the history
  • Loading branch information
tuupola committed Apr 8, 2017
0 parents commit f121530
Show file tree
Hide file tree
Showing 23 changed files with 992 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -0,0 +1,5 @@
/vendor/
/report/
composer.lock
.phplint-cache
.env
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -0,0 +1,6 @@
# Changelog

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 0.1.0 - Unreleased

36 changes: 36 additions & 0 deletions CONTRIBUTING.md
@@ -0,0 +1,36 @@
# Contributing

## Only one feature or change per pull request

Make pull requests only one feature or change at the time. Make pull requests from feature branch. Pull requests should not come from your master branch.

For example you have fixed a bug. You also have optimized some code. Optimization is not related to a bug. These should be submitted as separate pull requests. This way I can easily choose what to include. It is also easier to understand the code changes.

## Write meaningful commit messages

Proper commit message is full sentence. It starts with capital letter but does not end with period. Headlines do not end with period. The GitHub default `Update filename.php` is not enough. When needed include also longer explanation what the commit does.

```
Capitalized, short (50 chars or less) summary
More detailed explanatory text, if necessary. Wrap it to about 72
characters or so. In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body. The blank
line separating the summary from the body is critical (unless you omit
the body entirely); tools like rebase can get confused if you run the
two together.
```

When in doubt see Tim Pope's blogpost [A Note About Git Commit Messages](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html)

## Follow the existing coding standards

When contributing to open source project it is polite to follow the original authors coding standars. They might be different than yours. It is not a holy war. This project uses **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)**

## Running Tests

You can run tests using composer.

``` bash
$ composer test
```
21 changes: 21 additions & 0 deletions LICENSE.md
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2017 Mika Tuupola <tuupola@appelsiini.net>

> 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.
62 changes: 62 additions & 0 deletions README.md
@@ -0,0 +1,62 @@
# Common interface for wifi positioning services

[![Latest Version](https://img.shields.io/packagist/v/tuupola/whereami.svg?style=flat-square)](https://packagist.org/packages/tuupola/whereami)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
[![Build Status](https://img.shields.io/travis/tuupola/whereami/master.svg?style=flat-square)](https://travis-ci.org/tuupola/whereami)
[![HHVM Status](https://img.shields.io/hhvm/tuupola/whereami.svg?style=flat-square)](http://hhvm.h4cc.de/package/tuupola/whereami)
[![Coverage](http://img.shields.io/codecov/c/github/tuupola/whereami.svg?style=flat-square)](https://codecov.io/github/tuupola/whereami)

## Install

Install using [Composer](https://getcomposer.org/).

``` bash
$ composer require tuupola/whereami:dev-master
```

## Usage

```php
require __DIR__ . "/vendor/autoload.php";

$provider = new MozillaProvider("your-api-key-here");
$scanner = new AirportScanner;
$locator = new Whereami($provider, $scanner);

$location = $locator->whereami();

/*
Array
(
[latitude] => 1.355989
[longitude] => 103.992365
[accuracy] => 65
)
*/
```


## Testing

You can run tests either manually...

``` bash
$ composer test
```
... or automatically on every code change. This requires [entr](http://entrproject.org/) to work:

``` bash
$ composer watch
```

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

## Security

If you discover any security related issues, please email tuupola@appelsiini.net instead of using the issue tracker.

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
1 change: 1 addition & 0 deletions codecov.yml
@@ -0,0 +1 @@
comment: false
53 changes: 53 additions & 0 deletions composer.json
@@ -0,0 +1,53 @@
{
"name": "tuupola/whereami",
"description": "Common interface for wifi positioning services",
"keywords": [
"wifi",
"geoposition"
],
"homepage": "https://github.com/tuupola/whereami",
"license": "MIT",
"authors": [
{
"name": "Mika Tuupola",
"email": "tuupola@appelsiini.net",
"homepage": "http://www.appelsiini.net/",
"role": "Developer"
}
],
"require": {
"php": "^5.6 || ^7.0",
"php-http/client-implementation": "^1.0",
"php-http/httplug": "^1.1",
"php-http/discovery": "^1.2",
"php-http/message": "^1.5",
"php-http/client-common": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^2.5",
"overtrue/phplint": "^0.2.0",
"php-http/mock-client": "^1.0",
"php-http/curl-client": "^1.7",
"zendframework/zend-diactoros": "^1.4",
"php-http/logger-plugin": "^1.0",
"monolog/monolog": "^1.22"
},
"autoload": {
"psr-4": {
"Whereami\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Whereami\\": "tests"
}
},
"scripts": {
"test": "composer phplint && composer phpcs && composer phpunit",
"phpcs": "vendor/bin/phpcs --standard=PSR2 --extensions=php --encoding=utf-8 --ignore=*/vendor/* . -p",
"phpunit": "vendor/bin/phpunit --coverage-text --coverage-html ./report/",
"phplint": "vendor/bin/phplint . --exclude=vendor/",
"watch": "find . -name \"*.php\" -not -path \"./vendor/*\" -o -name \"*.json\" -not -path \"./vendor/*\"| entr -c composer test"
}
}
16 changes: 16 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false">
<testsuites>
<testsuite name="all">
<directory suffix="Test.php">tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src/</directory>
</whitelist>
<blacklist>
<directory suffix=".php">vendor/</directory>
</blacklist>
</filter>
</phpunit>
60 changes: 60 additions & 0 deletions src/HttpClientFactory.php
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of whereami package
*
* Copyright (c) 2017 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://github.com/tuupola/whereami
*
*/

namespace Whereami;

use Http\Client\Common\Plugin;
use Http\Client\Common\PluginClient;
use Http\Client\Common\Plugin\ErrorPlugin;
use Http\Client\Common\Plugin\HeaderDefaultsPlugin;
use Http\Client\Common\Plugin\HeaderSetPlugin;
use Http\Client\HttpClient;
use Http\Discovery\HttpClientDiscovery;

class HttpClientFactory
{
private $httpClient;
private $plugins = [];

public function __construct(
HttpClient $httpClient = null,
array $options = []
) {
$this->httpClient = $httpClient ?: HttpClientDiscovery::find();
$this->options = $options;
}

public function create()
{
$plugins = [
//new ErrorPlugin,
new HeaderSetPlugin([
"User-Agent" => "whereami/" . Whereami::VERSION,
"Accept" => "application/json",
"Content-Type" => "application/json; charset=utf-8",
])
];

$plugins = array_merge($plugins, $this->plugins);

return new PluginClient($this->httpClient, $plugins);
}

public function addPlugin(Plugin $plugin)
{
$this->plugins[] = $plugin;
return $this;
}
}
27 changes: 27 additions & 0 deletions src/Provider.php
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of whereami package
*
* Copyright (c) 2017 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://github.com/tuupola/whereami
*
*/

namespace Whereami;

interface Provider
{
/**
* Process the given data and return location
*
* @param array $data
* @return array
*/
public function process(array $data, array $options = []);
}
80 changes: 80 additions & 0 deletions src/Provider/GoogleProvider.php
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of whereami package
*
* Copyright (c) 2017 Mika Tuupola
*
* Licensed under the MIT license:
* http://www.opensource.org/licenses/mit-license.php
*
* Project home:
* https://github.com/tuupola/whereami
*
*/

namespace Whereami\Provider;

use Http\Client\HttpClient;
use Http\Discovery\MessageFactoryDiscovery;
use Http\Message\RequestFactory;
use Whereami\HttpClientFactory;
use Whereami\Provider;

final class GoogleProvider implements Provider
{
private $endpoint = "https://www.googleapis.com/geolocation/v1/geolocate";
private $apikey;
private $httpClient;
private $requestFactory;

public function __construct(
$apikey,
HttpClient $httpClient = null,
RequestFactory $requestFactory = null
) {
$this->apikey = $apikey;
$this->httpClient = $httpClient ?: (new HttpClientFactory)->create();
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find();
}

public function process(array $data, array $options = [])
{
$endpoint = $this->endpoint();
$headers = [];
$body = $this->transform($data);
$request = $this->requestFactory->createRequest("POST", $endpoint, $headers, $body);
$response = $this->httpClient->sendRequest($request);
return $this->parse((string) $response->getBody());
}

private function endpoint()
{
return $this->endpoint .= "?" . http_build_query(["key" => $this->apikey]);
}

private function transform($data = [])
{
$json["wifiAccessPoints"] = array_map(function ($entry) {
return [
"ssid" => $entry["name"],
"macAddress" => $entry["address"],
"signalStrength" => $entry["signal"],
"channel" => $entry["channel"],
];
}, $data);

return json_encode($json, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}

private function parse($json)
{
$data = json_decode($json, true);

return [
"latitude" => $data["location"]["lat"],
"longitude" => $data["location"]["lng"],
"accuracy" => $data["accuracy"],
];
}
}

0 comments on commit f121530

Please sign in to comment.