Skip to content

Commit

Permalink
readme update; new helper function package addition; new API method a…
Browse files Browse the repository at this point in the history
…dded to make sure the API endpoint is OK
benjaminhansen committed Jun 21, 2024
1 parent 5a84880 commit f742dbc
Showing 4 changed files with 21 additions and 15 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
"guzzlehttp/guzzle": "^7.8",
"nesbot/carbon": "^3.2",
"phpfastcache/phpfastcache": "^9.2",
"illuminate/collections": "^11.6"
"illuminate/collections": "^11.6",
"laravel/helpers": "^1.7"
}
}
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -41,6 +41,13 @@ $api->useCache();
// $api->setCacheLifetime(3600); // seconds that the cached data should persist


/*
** You can make sure that the API's status returns OK before allowing any
** requests to be made. An exception will be thrown if the API status is not OK.
*/
$api->ensureApiIsOk();


/*
** We can set a timezone for the entire API callstack.
** All datetime objects will be converted to this timestamp.
13 changes: 12 additions & 1 deletion src/Api.php
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
use NWS\Features\ForecastOffice;
use NWS\Features\ObservationStation;
use NWS\Exceptions\InvalidRequestException;
use NWS\Exceptions\ApiNotOkException;
use DateTimeZone;

class Api
@@ -114,7 +115,7 @@ public function get($url): object|bool
}

// key-ify the request URL to use as the unique ID in our cache
$key = urlencode($url);
$key = str_slug($url);

// if there is a value in the cache for the given URL, return the cached data
if($this->cache->has($key)) {
@@ -151,6 +152,16 @@ public function ok(): bool
return strtolower($this->status()) == "ok";
}

public function ensureApiIsOk(): self
{
if(!$this->ok()) {
$status = $this->status();
throw new ApiNotOkException("NWS API is not OK: {$status}");
}

return $this;
}

public function getLocation(float $lat = null, float $lon = null, string $observation_station = null, string $forecast_office = null): Point|ObservationStation|ForecastOffice
{
if($lat && $lon) {
13 changes: 0 additions & 13 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
<?php

function env($key, $default = null) {
$env = isset($_ENV[$key]) ? $_ENV[$key] : $default;

$env = match($env) {
'true' => true,
'false' => false,
'null' => null,
default => $env
};

return $env;
}

function stripos_array($haystack, $needles) {
foreach($needles as $needle) {
if(($res = stripos($haystack, $needle)) !== false) {

0 comments on commit f742dbc

Please sign in to comment.