Skip to content

Commit

Permalink
Code cleanup; updated wind direction method
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminhansen committed Sep 5, 2024
1 parent 6f46066 commit 16457ba
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 65 deletions.
35 changes: 7 additions & 28 deletions src/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

use Phpfastcache\Helper\Psr16Adapter;
use GuzzleHttp\Client as HttpClient;
use BenjaminHansen\NWS\Features\Point;
use BenjaminHansen\NWS\Features\ForecastOffice;
use BenjaminHansen\NWS\Features\ObservationStation;
use BenjaminHansen\NWS\Features\ObservationStations;
use BenjaminHansen\NWS\Features\Glossary;
use BenjaminHansen\NWS\Features\ForecastZone;
use BenjaminHansen\NWS\Exceptions\ApiNotOkException;
use BenjaminHansen\NWS\Exceptions\CacheException;
use BenjaminHansen\NWS\Features\{Point, ForecastOffice, ObservationStation, ObservationStations, Glossary, ForecastZone};
use BenjaminHansen\NWS\Exceptions\{ApiNotOkException, CacheException};
use BenjaminHansen\NWS\Support\Carbon;
use Illuminate\Support\Collection;
use DateInterval;
use DateTimeZone;
use DateInterval, DateTimeZone;

class Api
{
Expand All @@ -31,16 +24,8 @@ class Api

public function __construct(string $domain, string $email, string|DateTimeZone $timezone = null)
{
if($timezone) {
// set the timezone that was passed into the constructor
$this->timezone($timezone);
} else {
// default the api requests to a reasonable timezone
$this->timezone('UTC');
}

// set our user agent for the API requests
$this->userAgent($domain, $email);
$this->timezone($timezone ?? 'UTC') // set the timezone that was provided, or default to a reasonable value
->userAgent($domain, $email); // set our user agent for the API requests

// build up our HTTP client for making requests to the API
$this->client = new HttpClient([
Expand Down Expand Up @@ -239,12 +224,7 @@ public function ok(): bool
public function assertOk(string $message = null): self
{
if(!$this->ok()) {
if($message) {
throw new ApiNotOkException($message);
}

$status = $this->status();
throw new ApiNotOkException("NWS API is not OK: {$status}");
throw new ApiNotOkException($message ?? "NWS API is not OK: {$this->status()}");
}

return $this;
Expand Down Expand Up @@ -304,8 +284,7 @@ public function zone(string $zone_id): ForecastZone
{
$zone_id = strtoupper($zone_id);
$url = "{$this->baseUrl()}/zones?id={$zone_id}";
$data = $this->get($url)->features[0];
return new ForecastZone($data, $this);
return new ForecastZone($this->get($url)->features[0], $this);
}

/*
Expand Down
8 changes: 1 addition & 7 deletions src/Features/Alert.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,7 @@
namespace BenjaminHansen\NWS\Features;

use BenjaminHansen\NWS\Api;
use BenjaminHansen\NWS\Enums\AlertResponse;
use BenjaminHansen\NWS\Enums\AlertUrgency;
use BenjaminHansen\NWS\Enums\AlertCertainty;
use BenjaminHansen\NWS\Enums\AlertSeverity;
use BenjaminHansen\NWS\Enums\AlertCategory;
use BenjaminHansen\NWS\Enums\AlertStatus;
use BenjaminHansen\NWS\Enums\AlertMessageType;
use BenjaminHansen\NWS\Enums\{AlertResponse, AlertUrgency, AlertCertainty, AlertSeverity, AlertCategory, AlertStatus, AlertMessageType};
use BenjaminHansen\NWS\Support\Carbon;
use Illuminate\Support\Collection;

Expand Down
7 changes: 3 additions & 4 deletions src/Features/Forecast.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace BenjaminHansen\NWS\Features;

use BenjaminHansen\NWS\Api;
use BenjaminHansen\NWS\Support\Helpers;
use BenjaminHansen\NWS\Support\Carbon;
use BenjaminHansen\NWS\Support\{Helpers, Carbon};

class Forecast extends BaseFeature
{
Expand All @@ -25,8 +24,8 @@ public function createdAt(): Carbon

public function elevation(string $unit = "FT", int $decimal_places = 0, bool $show_unit = false): int|float|string
{
$value = match($unit) {
"ft", "FT" => round(Helpers::meters_to_feet($this->properties_elevation()->value), $decimal_places),
$value = match(strtoupper($unit)) {
"FT" => round(Helpers::meters_to_feet($this->properties_elevation()->value), $decimal_places),
default => round($this->properties_elevation()->value, $decimal_places)
};

Expand Down
3 changes: 1 addition & 2 deletions src/Features/ForecastGridData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace BenjaminHansen\NWS\Features;

use BenjaminHansen\NWS\Api;
use BenjaminHansen\NWS\Support\Helpers;
use BenjaminHansen\NWS\Support\Carbon;
use BenjaminHansen\NWS\Support\{Helpers, Carbon};
use Illuminate\Support\Collection;

class ForecastGridData extends BaseFeature
Expand Down
3 changes: 1 addition & 2 deletions src/Features/ForecastHourly.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace BenjaminHansen\NWS\Features;

use BenjaminHansen\NWS\Api;
use BenjaminHansen\NWS\Support\Coordinate;
use BenjaminHansen\NWS\Support\Carbon;
use BenjaminHansen\NWS\Support\{Coordinate, Carbon};
use Illuminate\Support\Collection;

class ForecastHourly extends BaseFeature
Expand Down
10 changes: 2 additions & 8 deletions src/Features/ForecastPeriod.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace BenjaminHansen\NWS\Features;

use BenjaminHansen\NWS\Api;
use BenjaminHansen\NWS\Support\Helpers;
use BenjaminHansen\NWS\Support\Carbon;
use BenjaminHansen\NWS\Support\{Helpers, Carbon};

class ForecastPeriod extends BaseFeature
{
Expand Down Expand Up @@ -48,16 +47,11 @@ public function windSpeed()
return $this->data->windSpeed;
}

public function windDirectionDegrees()
public function windDirection()
{
return $this->data->windDirection;
}

public function windDirectionCardinal(): string
{
return Helpers::degrees_to_cardinals($this->data->windDirection);
}

public function shortForecast(): string
{
return $this->data->shortForecast;
Expand Down
3 changes: 1 addition & 2 deletions src/Features/LatestObservations.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace BenjaminHansen\NWS\Features;

use BenjaminHansen\NWS\Api;
use BenjaminHansen\NWS\Support\Helpers;
use BenjaminHansen\NWS\Support\Carbon;
use BenjaminHansen\NWS\Support\{Helpers, Carbon};

class LatestObservations extends BaseFeature
{
Expand Down
7 changes: 2 additions & 5 deletions src/Features/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ public function radarStationId(): string

public function radarStation(): RadarStation
{
$id = $this->radarStationId();
$base_url = $this->api->baseUrl();
$url = "{$base_url}/radar/stations/{$id}";
$url = "{$base_url}/radar/stations/{$this->radarStationId()}";

return new RadarStation($this->api->get($url), $this->api);
}
Expand Down Expand Up @@ -120,9 +119,7 @@ public function latestObservations(int $observation_station_index = 0): LatestOb
public function activeAlerts(): Alerts
{
$base_url = $this->api->baseUrl();
$lat = $this->latitude();
$lon = $this->longitude();
$request_url = "{$base_url}/alerts/active?point={$lat},{$lon}";
$request_url = "{$base_url}/alerts/active?point={$this->latitude()},{$this->longitude()}";

return new Alerts($this->api->get($request_url), $this->api);
}
Expand Down
8 changes: 1 addition & 7 deletions src/Traits/CanGetDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

namespace BenjaminHansen\NWS\Traits;

use BenjaminHansen\NWS\Enums\AlertResponse;
use BenjaminHansen\NWS\Enums\AlertUrgency;
use BenjaminHansen\NWS\Enums\AlertCertainty;
use BenjaminHansen\NWS\Enums\AlertSeverity;
use BenjaminHansen\NWS\Enums\AlertCategory;
use BenjaminHansen\NWS\Enums\AlertStatus;
use BenjaminHansen\NWS\Enums\AlertMessageType;
use BenjaminHansen\NWS\Enums\{AlertResponse, AlertUrgency, AlertCertainty, AlertSeverity, AlertCategory, AlertStatus, AlertMessageType};

trait CanGetDescription
{
Expand Down

0 comments on commit 16457ba

Please sign in to comment.