Skip to content

Commit

Permalink
Feature/init (#1)
Browse files Browse the repository at this point in the history
* Changes to actions

* Updated actions

* Prepared for release

* Cleanup tests

* Updated readme

* Updated README

* Updated Readme

* Updated Readme
  • Loading branch information
Henrik B Hansen authored Jul 29, 2020
1 parent caa8997 commit 60a9625
Show file tree
Hide file tree
Showing 9 changed files with 282 additions and 47 deletions.
38 changes: 26 additions & 12 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,33 @@
name: Laravel
name: tests

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
on: [push, pull_request]

jobs:
laravel-tests:

runs-on: ubuntu-latest


strategy:
fail-fast: true
matrix:
php: [7.4]
stability: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

steps:
- uses: actions/checkout@v2
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
- name: Checkout code
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none

- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

- name: Execute tests
run: vendor/bin/phpunit --verbose
113 changes: 112 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

[![Latest Version on Packagist](https://img.shields.io/packagist/v/vemcogroup/laravel-weather.svg?style=flat-square)](https://packagist.org/packages/vemcogroup/laravel-weather)
[![Total Downloads](https://img.shields.io/packagist/dt/vemcogroup/laravel-weather.svg?style=flat-square)](https://packagist.org/packages/vemcogroup/laravel-weather)
![tests](https://github.com/vemcogroup/laravel-weather/workflows/tests/badge.svg)

## Description

Expand Down Expand Up @@ -50,9 +51,119 @@ return [
*/

'provider' => env('WEATHER_PROVIDER'),

/*
|--------------------------------------------------------------------------
| Midday
|--------------------------------------------------------------------------
|
| Here you define what time is midday.
|
*/

'midday' => [
'hour' => '13',
'minute' => '59',
],

];
```

## Usage

TBD
At the moment this package support the folllowing weather services:

| Service | Website | Geocoding | Remarks |
| :--- | :--- | :---: | :--- |
| Dark Sky | https://darksky.net | Manual | Deprecated, not able to aquire api key https://blog.darksky.net |
| Weatherstack | https://weatherstack.com | Auto | For historical data a minimum Standard license is required. For forecast data a minimum Professional license is required. |

For other weather services fill free to create an issue or make a Pull Request.

For `Manual` geocoding you need a Google geocode api key.
Aquire it here https://developers.google.com/maps/documentation/geocoding/start and insert it into your .env file

```php
GOOGLE_MAPS_GEOCODING_API_KEY=
```

### Request

Start by setting up your request

```php
$request = (new Vemcogroup\Weather\Request('1 Infinite Loop, Cupertino, CA 95014, USA'));
```

*Units*
There two available unit types, default is Metric:

Metric (m): `Vemcogroup\Weather\Providers\Provider::WEATHER_UNITS_METRIC`
Fahrenheit (f): `Vemcogroup\Weather\Providers\Provider::WEATHER_UNITS_FAHRENHEIT`

To change the response units you can do the following:

```php
$request->withUnits(Vemcogroup\Weather\Providers\Provider::WEATHER_UNITS_FAHRENHEIT);
```

*Locale*
To change the locale for descriptions, summaries and other texts in the response, do the following:
```php
$request->withLocale('nl');
```
Locale need to be an 2-letter ISO Code of your preferred language.

*Dates*
If you need to select the dates to get weather data for E.g for historical data, set the dates like this:

```php
$request->withDates([$date, ...]);
```
All dates in the array need to `Carbon` objects.

*Options*
If you need to set any extra options based in your selected weather provider you can do the following:

```php
$request->withOption('name', 'value');
```

### Current weather and forecast

To get current weather and forecast response you can do this:

```php
$weather = weather()->getForecast($request);
```

Weather response will always be a `Collection` of responses.
Forecast days depends on weather service provider.

To get current weather data:

```php
$weather->first()->getCurrently(); // DataPoint
```

To get forecast you can take first element of response and get the forecast like this:

```php
$weather->first()->getDaily()->getData(); // array
```
Afterward run through the array with represent each day of the forecast on a `DataPoint` object.

### Historical

To get historical data you can do this:

```php
$weather = weather()->getHistorical($request);
```

Remember to set dates on request.
Response will be a collection with keys representing the dates for historical data.

### Response
To see what response data is available look into source code `/src/Objects`

8 changes: 8 additions & 0 deletions config/weather.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@

'provider' => env('WEATHER_PROVIDER'),

/*
|--------------------------------------------------------------------------
| Midday
|--------------------------------------------------------------------------
|
| Here you define what time is midday.
|
*/
'midday' => [
'hour' => '13',
'minute' => '59',
Expand Down
27 changes: 16 additions & 11 deletions src/Providers/Darksky.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vemcogroup\Weather\Providers;

use Carbon\Carbon;
use Vemcogroup\Weather\Request;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -44,13 +43,25 @@ private function buildRequest($type = self::WEATHER_TYPE_FORECAST): void
$request->lookupGeocode();
$latitude = $request->getLatitude();
$longitude = $request->getLongitude();

// convert units
if ($request->getUnits() === self::WEATHER_UNITS_METRIC) {
$request->withUnits('si');
}

if ($request->getUnits() === self::WEATHER_UNITS_FAHRENHEIT) {
$request->withUnits('us');
}

$options = $request->getHttpQuery();

if ($type === self::WEATHER_TYPE_FORECAST) {
$dateRequest = clone $request;
$url = $this->url . $this->apiKey
. "/$latitude,$longitude"
. ($options ? "?$options" : '');
. "?lang=" . $request->getLocale()
. "&units=" . $request->getUnits()
. ($options ? "&$options" : '');
$dateRequest->setUrl($url);
$requests[] = $dateRequest;
}
Expand All @@ -61,20 +72,14 @@ private function buildRequest($type = self::WEATHER_TYPE_FORECAST): void
$url = $this->url . $this->apiKey
. "/$latitude,$longitude"
. ",$date->timestamp"
. ($options ? "?$options" : '');
. "?lang=" . $request->getLocale()
. "&units=" . $request->getUnits()
. ($options ? "&$options" : '');
$dateRequest->setKey($date->format('Y-m-d H:i'));
$dateRequest->setUrl($url);
$requests[] = $dateRequest;
}
}

/* if (!empty($request['time'])) {
$currentTimezone = config('app.timezone');
//$currentTimezone = user() && user()->user_timezone ? user()->user_timezone : config('app.timezone');
$dateConverted = new \DateTime($request['time'], new \DateTimeZone($request['timezone']));
$dateConverted->setTimezone(new \DateTimeZone($currentTimezone));
$time = $dateConverted->getTimestamp();
}*/
}

if(count($requests)) {
Expand Down
15 changes: 15 additions & 0 deletions src/Providers/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@

abstract class Provider
{
public const WEATHER_UNITS_METRIC = 'm';
public const WEATHER_UNITS_FAHRENHEIT = 'f';

public const WEATHER_ICON_NA = 'na';
public const WEATHER_ICON_CLEAR_DAY = 'clear-day';
public const WEATHER_ICON_CLEAR_NIGHT = 'clear-night';
public const WEATHER_ICON_RAIN = 'rain';
public const WEATHER_ICON_SNOW = 'snow';
public const WEATHER_ICON_SLEET = 'sleet';
public const WEATHER_ICON_WIND = 'wind';
public const WEATHER_ICON_FOG = 'fog';
public const WEATHER_ICON_CLOUDY = 'cloudy';
public const WEATHER_ICON_PARTLY_CLOUDY_DAY = 'partly-cloudy-day';
public const WEATHER_ICON_PARTLY_CLOUDY_NIGHT = 'partly-cloudy-night';

protected const WEATHER_TYPE_FORECAST = 'forecast';
protected const WEATHER_TYPE_HISTORICAL = 'historical';

Expand Down
Loading

0 comments on commit 60a9625

Please sign in to comment.