| 1 | package Weather::WeatherGov; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | our $VERSION = '0.04'; | ||
| 5 | |||
| 6 | use HTTP::Tiny; | ||
| 7 | use URI; | ||
| 8 | use JSON 'decode_json'; | ||
| 9 | |||
| 10 | use Moo 2; | ||
| 11 | use feature 'signatures'; | ||
| 12 | no warnings 'experimental::signatures'; | ||
| 13 | |||
| 14 | our $base_uri = URI->new( | ||
| 15 | $ENV{PERL_WEATHER_WEATHERGOV_URI} | ||
| 16 | #|| 'https://forecast-v3.weather.gov/' | ||
| 17 | || 'https://api.weather.gov/points/' | ||
| 18 | ); | ||
| 19 | |||
| 20 | has 'ua' => ( | ||
| 21 | is => 'lazy', | ||
| 22 | default => sub ($self) { | ||
| 23 | HTTP::Tiny->new( | ||
| 24 | agent => "Weather::WeatherGov/$VERSION", | ||
| 25 | ) | ||
| 26 | }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | has 'base_uri' => ( | ||
| 30 | is => 'ro', | ||
| 31 | default => sub { URI->new( $base_uri )}, | ||
| 32 | ); | ||
| 33 | |||
| 34 | sub forecast( $self, %options ) { | ||
| 35 | my $entry = $self->base_uri . sprintf '%s,%s', | ||
| 36 | $options{latitude}, | ||
| 37 | $options{longitude}; | ||
| 38 | # We should cache this request for office and grid position | ||
| 39 | # maybe store these in the same SQLite database(schema) as Weather::MOSMIX?! | ||
| 40 | |||
| 41 | # also, we need a cache and rate-limiter here so we can rate-limit | ||
| 42 | # at least by IP address and maybe also globally how much we hit weather.gov | ||
| 43 | my $loc = $self->json_request($entry); | ||
| 44 | $self->json_request( | ||
| 45 | $loc->{properties}->{forecastHourly}, | ||
| 46 | ); | ||
| 47 | |||
| 48 | } | ||
| 49 | |||
| 50 | sub json_request( $self, $uri ) { | ||
| 51 | my $response = $self->ua->request(GET => $uri, { | ||
| 52 | accept => 'JSON-LD', | ||
| 53 | } ); | ||
| 54 | decode_json( $response->{content} ); | ||
| 55 | } | ||
| 56 | |||
| 57 | 1; | ||
| 58 | |||
| 59 | =head1 SEE ALSO | ||
| 60 | |||
| 61 | L<https://www.weather.gov/documentation/services-web-api> | ||
| 62 | |||
| 63 | L<https://forecast-v3.weather.gov/documentation> | ||
| 64 | |||
| 65 | =cut |