-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from vannut/feature-extract-fetch-to-action
Fetch data in Action and CP updates
- Loading branch information
Showing
9 changed files
with
201 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
@extends('statamic::layout') | ||
@section('title', 'Weather settings') | ||
@section('wrapper_class', 'max-w-md ml-0') | ||
|
||
@section('content') | ||
|
||
<form | ||
title="Fetch current Weather" | ||
method="post" | ||
action="{{ cp_route('weather.data.fetchWeather') }}" | ||
> | ||
<button class="btn-primary" type="submit">Fetch/update Weather data</button> | ||
@csrf | ||
</form> | ||
|
||
<h1 class="mt-4">Current Data</h1> | ||
@if( ! $json ) | ||
<p><em>No data stored yet</em></p> | ||
@else | ||
<div name="textarea"> | ||
<textarea | ||
id="field_textarea" | ||
class="input-text" | ||
style="width:100%; overflow-wrap: break-word; height: 450px;" | ||
>{{ $json }}</textarea> | ||
</div> | ||
@endif | ||
|
||
|
||
|
||
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Vannut\StatamicWeather\Actions; | ||
|
||
use Storage; | ||
use Illuminate\Support\Collection; | ||
|
||
class FetchAndStoreAction { | ||
|
||
public function __construct( | ||
Collection $config | ||
) { | ||
$this->config = $config; | ||
} | ||
|
||
public function execute(): bool | ||
{ | ||
|
||
$content = $this->talkToOpenweathermap(); | ||
|
||
// Do nothing when we don't get anything back | ||
if ($content === false) { | ||
return false; | ||
} | ||
|
||
// Decode the json object, drop out when not a valid object | ||
$jsonObj = json_decode($content); | ||
if ($jsonObj === null && json_last_error() !== JSON_ERROR_NONE) { | ||
return false; | ||
} | ||
|
||
// add the fetch time | ||
$jsonObj = array_merge(["fetched_at" => now()->format('U')], (array) $jsonObj); | ||
|
||
// Store the JSON to be used by the tags and endpoints | ||
Storage::put('weather-forecast.json', json_encode($jsonObj)); | ||
|
||
return true; | ||
|
||
} | ||
|
||
|
||
private function talkToOpenweathermap(): string | ||
{ | ||
$endpoint = $this->config->get('api_url') | ||
.'onecall?lat='.$this->config->get('lat') | ||
.'&lon='.$this->config->get('lon') | ||
.'&exclude=minutely,hourly,alerts' | ||
.'&units='.$this->config->get('units', 'metric') | ||
.'&appid='.$this->config->get('api_secret_key') | ||
.'&lang='.$this->config->get('lang','en'); | ||
|
||
|
||
$headers = [ | ||
'Content-Type:application/json', | ||
]; | ||
$ch = curl_init($endpoint); | ||
|
||
|
||
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers); | ||
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); | ||
|
||
$result = curl_exec($ch); | ||
curl_close($ch); | ||
|
||
return $result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters