Skip to content

Commit

Permalink
Merge pull request #428 from bolt/feature/make-weather-widget-work
Browse files Browse the repository at this point in the history
Make Weather widget work
  • Loading branch information
bobdenotter committed May 16, 2019
2 parents 04a971d + 3f0b3d0 commit 47caf4c
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 12 deletions.
68 changes: 68 additions & 0 deletions src/Widget/WeatherWidget.php
Expand Up @@ -6,6 +6,8 @@

use Bolt\Widget\Injector\AdditionalTarget;
use Bolt\Widget\Injector\RequestZone;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class WeatherWidget extends BaseWidget implements TwigAware
{
Expand All @@ -14,4 +16,70 @@ class WeatherWidget extends BaseWidget implements TwigAware
protected $priority = 200;
protected $template = '@bolt/widgets/weather.twig';
protected $zone = RequestZone::BACKEND;

/** @var string Open API key, don't use more than once per second */
public const KEY = '0acbdeea56dfafe244ac87707c5fdcb2';

public function run(array $params = []): string
{
$ip = $this->getIP();

$location = $this->getLocation($ip);

$weather = $this->getWeather($location);

$context = [
'location' => $location,
'weather' => $weather,
];

return parent::run($context);
}

private function getIP(): string
{
try {
$client = new Client(['base_uri' => 'http://checkip.dyndns.com/']);
$dnsResponse = $client->request('GET', '/')->getBody()->getContents();
} catch (RequestException $e) {
$dnsResponse = 'Just assume we are at 127.0.0.1';
}

preg_match('/(\d{1,3}\.){3}\d{1,3}/', $dnsResponse, $matches);

return $matches[0];
}

private function getLocation(string $ip): array
{
try {
$client = new Client(['base_uri' => "http://ipinfo.io/{$ip}"]);
$details = json_decode($client->request('GET', '/')->getBody()->getContents(), true);
} catch (RequestException $e) {
$details = [];
}

return $details;
}

private function getWeather(array $location): array
{
[$lat, $lon] = explode(',', $location['loc']);

$url = sprintf(
'https://api.openweathermap.org/data/2.5/weather?lat=%s&lon=%s&appid=%s&units=metric',
$lat,
$lon,
$this::KEY
);

try {
$client = new Client();
$details = json_decode($client->request('GET', $url)->getBody()->getContents(), true);
} catch (RequestException $e) {
$details = [];
}

return $details;
}
}
13 changes: 7 additions & 6 deletions templates/widgets/weather.twig
@@ -1,16 +1,17 @@
<div class="card mb-4">
<div class="image card-img-top"
style="background-image: url(https://source.unsplash.com/400x240/?rain,sun,snow,storm,weather,sunny); height: 200px; padding-top: 60px;">
<center style='text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.4); color: #FFF;'>
<span>Sunny, with occasional clouds</span><br>
<span style="font-size: 200%;"><i class="fas fa-sun"></i> 18 &deg; C</span><br>
<span>Den Haag, Netherlands</span>
style="background-image: url(https://source.unsplash.com/400x240/?{{ location.city|url_encode }},{{ location.region|url_encode }},{{ location.country|url_encode }}); height: 200px; padding-top: 60px;">
<center style='text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.6); color: #FFF;'>
<span style="font-size: 125%;">{{ weather.weather|first['description']|capitalize }} in {{ weather.name }}, {{ location.country }}</span><br>
<span style="font-size: 200%;">
<img src='http://openweathermap.org/img/w/{{ weather.weather|first['icon'] }}.png'>
{{ weather.main.temp|number_format(1) }} &deg; C</span><br>
</center>
</div>
<div class="card-body">
<a>
<i class="fa fa-socks"></i>
This is a random widget
Current weather, by OpenWeatherMap
</a>
</div>
</div>
20 changes: 20 additions & 0 deletions tests/php/Widget/DummyWidget.php
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Bolt\Tests\Widget;

use Bolt\Widget\BaseWidget;
use Bolt\Widget\Injector\AdditionalTarget;
use Bolt\Widget\Injector\RequestZone;
use Bolt\Widget\TwigAware;

class DummyWidget extends BaseWidget implements TwigAware
{
protected $name = 'Dummy Widget';
protected $target = AdditionalTarget::WIDGET_BACK_DASHBOARD_ASIDE_TOP;
protected $priority = 200;
protected $template = '@bolt/widgets/weather.twig';
protected $zone = RequestZone::BACKEND;

}
12 changes: 6 additions & 6 deletions tests/php/Widget/WidgetsTest.php
Expand Up @@ -83,21 +83,21 @@ public function testRenderWidgetWithExtraParameters(): void
$requestStack = new RequestStack();
$requestStack->push(Request::createFromGlobals());

$loader = new ArrayLoader(['weather.twig' => '[Hello, {{ foo }}!]']);
$loader = new ArrayLoader(['dummy.twig' => '[Hello, {{ foo }}!]']);
$twig = new Environment($loader);

$cache = new Psr6Cache(new TraceableAdapter(new FilesystemAdapter()));

$widgets = new Widgets($requestStack, $queueProcessor, $twig, $cache);

$weatherWidget = new WeatherWidget();
$weatherWidget->setTemplate('weather.twig');
$widget = new DummyWidget();
$widget->setTemplate('dummy.twig');

$widgets->registerWidget($weatherWidget);
$widgets->registerWidget($widget);

$this->assertSameHtml(
'<div id="widget-weather-widget" name="Weather Widget">[Hello, Bar!]</div>',
$widgets->renderWidgetByName('Weather Widget', ['foo' => 'Bar'])
'<div id="widget-dummy-widget" name="Dummy Widget">[Hello, Bar!]</div>',
$widgets->renderWidgetByName('Dummy Widget', ['foo' => 'Bar'])
);
}

Expand Down

0 comments on commit 47caf4c

Please sign in to comment.