| 1 | package Weather::Meteo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use autodie; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 15 | ▶ | use CHI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 15 | ▶ | use Object::Configure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use Params::Get 0.13; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 15 | ▶ | use Params::Validate::Strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 15 | ▶ | use POSIX (); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 15 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 15 | ▶ | use Return::Set; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 15 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 15 | ▶ | use Sub::Private { mode => 'enforce' }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 15 | ▶ | use Sub::Protected; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 15 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 15 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # Configuration. Everything that was a magic number or string lives here so | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # that there is exactly one place to change it. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # Archive API host (historical data from 1940 onwards) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 15 | Readonly my $DEFAULT_HOST => 'archive-api.open-meteo.com'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # Forecast API host (up to 16 days ahead) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 15 | Readonly my $FORECAST_HOST => 'api.open-meteo.com'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 15 | Readonly my $FIRST_YEAR => 1940; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 15 | Readonly my $EXPIRES_IN => '1 hour'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 15 | Readonly my $MIN_INTERVAL => 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 15 | Readonly my $MAX_RETRIES => 3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 15 | Readonly my $RETRY_DELAY => 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 15 | Readonly my $MAX_RETRY_WAIT => 60; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # Default timezone when neither the caller nor the location object supplies one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 15 | Readonly my $DEFAULT_TZ => 'Europe/London'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 15 | Readonly my $DEFAULT_DAYS => 7; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 15 | Readonly my $MAX_DAYS => 16; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # The fields requested from the API are fixed, so that callers always get the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # same structure back and the cache never mixes shapes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 15 | Readonly my $HOURLY_FIELDS => 'temperature_2m,rain,snowfall,weathercode'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 15 | Readonly my $DAILY_FIELDS => 'weathercode,temperature_2m_max,temperature_2m_min,' | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | . 'rain_sum,snowfall_sum,precipitation_hours,windspeed_10m_max,' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | . 'windgusts_10m_max,sunrise,sunset'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 15 | Readonly my @UNIT_PAIRS => (windspeed_unit => 'mph', precipitation_unit => 'inch'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Atomic groups prevent O(n) backtracking (ReDoS); capturing also untaints. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 15 | Readonly my $COORD_RE => qr/\A(-?(?>\d+)(?:\.(?>\d+))?)\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 15 | Readonly my $DATE_RE => qr/\A(\d{4}-\d{2}-\d{2})\z/; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | # Only transient failures are worth retrying; a 500 or 404 will not fix itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 15 | Readonly my %RETRYABLE_STATUS => map { $_ => 1 } (429, 502, 503, 504); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # Constructor arguments that must be validated (and thereby untainted). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | # Each entry is [ pattern, message key ]; the pattern captures the safe value. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 15 | Readonly my %PARAM_PATTERNS => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | host => [ qr/\A([A-Za-z0-9][A-Za-z0-9.\-]*(?::\d{1,5})?)\z/, 'invalid_host' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | max_retries => [ qr/\A(\d{1,3})\z/, 'invalid_max_retries' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | retry_delay => [ qr/\A(\d{1,4}(?:\.\d{1,3})?)\z/, 'invalid_retry_delay' ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # Default (English) message table. Placeholders are written {name}. A value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # may also be a hashref of plural/gender variants (see i18n()). Callers can | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # override any entry through the 'messages' constructor argument. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 15 | Readonly my %MESSAGES => ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | usage_weather => 'Usage: weather(latitude => $latitude, longitude => $longitude, date => "YYYY-MM-DD")', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | usage_forecast => 'Usage: forecast(latitude => $latitude, longitude => $longitude)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | usage_sunrise => 'Usage: sunrise_sunset(latitude => $latitude, longitude => $longitude)', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | invalid_coordinates => '{class}: Invalid latitude/longitude format ({latitude}, {longitude})', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | invalid_date_format => 'Invalid date format. Expected YYYY-MM-DD', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | not_a_date => "'{date}' is not a valid date", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | days_range => 'days must be between {min} and {max}; defaulting to {default}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | api_error => '{class}: API error: {reason}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | http_error => '{class}: {url} API returned error: {status}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | bad_response => '{class}: UA->get did not return a valid HTTP response', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | json_parse => 'Failed to parse JSON response: {error}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | bad_ua => "'ua' argument must be an object with a get() method", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | ua_undef => 'ua() requires a defined value', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | invalid_host => "Invalid host '{value}': must be a plain hostname", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | invalid_max_retries => "Invalid max_retries '{value}': must be a non-negative integer", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | invalid_retry_delay => "Invalid retry_delay '{value}': must be a non-negative number", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | invalid_messages => "'messages' argument must be a hash reference", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | Weather::Meteo - Interface to L<https://open-meteo.com> for historical and forecast weather data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | Version 0.16 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 15 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | The C<Weather::Meteo> module provides an interface to the Open-Meteo API for retrieving | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | historical weather data from 1940 and weather forecasts up to 16 days ahead. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | It allows users to fetch weather information by specifying latitude, longitude, and a date. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | The module supports object-oriented usage and allows customisation of the HTTP user agent. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | use Weather::Meteo; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | # Historical weather | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | my $weather = $meteo->weather({ latitude => 0.1, longitude => 0.2, date => '2022-12-25' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | # Forecast (default 7 days) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | my $forecast = $meteo->forecast({ latitude => 51.34, longitude => 1.42 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # Sunrise and sunset for a specific date | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | my $times = $meteo->sunrise_sunset({ latitude => 51.34, longitude => 1.42, date => '2025-06-21' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | print "Sunrise: $times->{sunrise}\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # Retry transient API failures up to 5 times, starting with a 2 second delay | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | my $patient = Weather::Meteo->new(max_retries => 5, retry_delay => 2); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | # Reword or translate the warnings and errors | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | my $french = Weather::Meteo->new(messages => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | not_a_date => "'{date}' n'est pas une date valide", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | =item * Caching | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | Identical requests are cached (using L<CHI> or a user-supplied caching object), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | reducing the number of HTTP requests to the API and speeding up repeated queries. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | When a request is made, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | a cache key is constructed from the coordinates, date, and timezone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | If a cached response exists it is returned immediately, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | avoiding unnecessary API calls. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | =item * Rate-Limiting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | A minimum interval between successive API calls can be enforced to ensure that the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | API is not overwhelmed and to comply with any request throttling requirements. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | Rate-limiting is implemented using L<Time::HiRes>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | A minimum interval between API calls can be specified via the C<min_interval> parameter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | in the constructor. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | Before making an API call, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | the module checks how much time has elapsed since the last request and, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | if necessary, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | sleeps for the remaining time. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | # Custom user agent with proxy support | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | my $ua = LWP::UserAgent->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | $ua->env_proxy(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | $meteo = Weather::Meteo->new(ua => $ua); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | # Clone an existing object and override one slot | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | my $clone = $meteo->new(host => 'custom.example.com'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | Creates a new C<Weather::Meteo> instance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | When called on an existing C<Weather::Meteo> object, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | clones that object and merges the supplied parameters. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | =item * C<cache> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | A caching object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | If not provided, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | an in-memory cache is created with a default expiration of one hour. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | =item * C<host> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | The archive API host endpoint. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | Defaults to C<archive-api.open-meteo.com>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | Must be a plain DNS hostname - letters, digits, hyphens, and dots - with an | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | optional port suffix (e.g. C<mock.example.com:8080>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | Values containing C<@>, path segments, or other special characters are rejected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | with a C<croak> to prevent Server-Side Request Forgery (SSRF) via the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | C<WEATHER__METEO__host> environment variable or configuration file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | Falsy values (C<undef>, C<"">, C<0>) fall back to the default silently. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =item * C<logger> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | An optional logger object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | Must respond to C<error()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | When supplied, API errors are reported through this logger in addition to C<Carp::carp>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | =item * C<max_retries> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | Maximum number of times a request is retried after a transient failure: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | HTTP 429 (Too Many Requests), 502, 503 or 504. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | Defaults to C<3>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | Set to C<0> to disable retrying. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | Other errors, including HTTP 500 and 404, are never retried. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | =item * C<messages> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | A hashref of message keys to replacement text, for translating or rewording | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | the module's warnings and errors. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | See L</i18n> for the keys, the placeholder syntax and plural support. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | Defaults to the built-in English messages. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =item * C<min_interval> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | Minimum number of seconds to wait between API requests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | May be fractional, for example C<0.5>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | Defaults to C<0> (no delay). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | Use this option to enforce rate-limiting. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | =item * C<retry_delay> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | Base delay in seconds before the first retry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | The delay doubles on each subsequent retry (C<retry_delay>, C<2 * retry_delay>, ...). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | A numeric C<Retry-After> header from the server takes precedence. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | Waits are capped at 60 seconds. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | Defaults to C<1>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | =item * C<ua> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | An object to use for HTTP requests. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | If not provided, a default C<LWP::UserAgent> is created. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | Must respond to C<get()>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | The class can be configured at runtime using environment variables and configuration files, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | for example, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | setting C<$ENV{'WEATHER__METEO__carp_on_warn'}> causes warnings to use L<Carp>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | For more information about runtime configuration, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | see L<Object::Configure>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | # Minimal -- use all defaults | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | # Custom UA with throttling | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | use LWP::UserAgent::Throttled; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | my $ua = LWP::UserAgent::Throttled->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | $ua->throttle('open-meteo.com' => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | my $meteo = Weather::Meteo->new(ua => $ua, min_interval => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | # Clone the object but change the host for integration testing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | my $test_meteo = $meteo->new(host => 'mock.example.com'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | All parameters are optional. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | They may be supplied as a hashref or a flat key/value list. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | When C<$class> is an existing C<Weather::Meteo> object the call clones it, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | merging any supplied parameters. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | ua => { type => 'object', can => 'get', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | cache => { type => 'object', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | host => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | min_interval => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | max_retries => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | messages => { type => 'hashref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | retry_delay => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | logger => { type => 'object', can => 'error', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | { type => 'object', isa => 'Weather::Meteo' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | Message Type Trigger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | ------------------------------------------------- ----- ----------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 'ua' argument must be an object with a get() croak clone called with an invalid ua arg | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | Invalid host '$host': must be a plain hostname croak host contains @, /, or other chars | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | that are not safe in a DNS label | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | Invalid max_retries '$n': must be a non-negative croak max_retries is not a small integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | integer | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | Invalid retry_delay '$d': must be a non-negative croak retry_delay is not a plain number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | number | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 'messages' argument must be a hash reference croak messages is not a hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 307 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=25.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 307 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 307 | ▶ | my $params = Params::Get::get_params(undef, \@_) || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
Params::Get::get_params(undef, \@_) || {}
MC/DC100%1/1
Params::Get::get_params(undef, \@_) || {}
Params::Get::get_params(undef, \@_)
Truth table100%2/2
A
A= Params::Get::get_params(undef, \@_)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # Weather::Meteo::new() used rather than Weather::Meteo->new() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 307 | ▶ | $class //= __PACKAGE__; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$class //= __PACKAGE__
MC/DC100%1/1
$class //= __PACKAGE__
$class
Truth table100%2/2
A
A= $class
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | # Clone path: merge new params over the existing object's fields | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 307 | ▶ | return $class->_clone($params) if Scalar::Util::blessed($class); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | 300 | $params = Object::Configure::configure($class, $params); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | # A falsy host (undef, "", 0) silently falls back to the default host | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 300 | ▶ | delete $params->{host} unless $params->{host}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | # Validate and untaint host and the retry settings. Host validation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | # prevents SSRF via the WEATHER__METEO__host env var or a config file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 300 | foreach my $name (sort keys %PARAM_PATTERNS) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 899 | ▶ | _untaint_param($class, $params, $name) if defined($params->{$name}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 298 | ▶ | if(defined($params->{messages}) && ref($params->{messages}) ne 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $params->{"messages"} and ref $params->{"messages"} ne "HASH"
MC/DC100%2/2
defined $params->{"messages"} and ref $params->{"messages"} ne "HASH"
defined $params->{"messages"}
ref $params->{"messages"} ne "HASH"
Truth table100%3/3
A and B
A= defined $params->{"messages"}B= ref $params->{"messages"} ne "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 2 | $class->_croak('invalid_messages'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 296 | my $ua = $params->{ua}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 296 | ▶ | if(!defined($ua)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 241 | $ua = LWP::UserAgent->new(agent => __PACKAGE__ . "/$VERSION"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 241 | $ua->default_header(accept_encoding => 'gzip,deflate'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 296 | ▶ | my $cache = $params->{cache} || CHI->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$params->{'cache'} || 'CHI'->new('driver', 'Memory', 'global', 1, 'expires_in', $EXPIRES_IN)
MC/DC0%0/2
$params->{'cache'} || 'CHI'->new('driver', 'Memory', 'global', 1, 'expires_in', $EXPIRES_IN)
$params->{'cache'}
'CHI'->new('driver', 'Memory', 'global', 1, 'expires_in', $EXPIRES_IN)
Truth table66%2/3
A || B
A= $params->{'cache'}B= 'CHI'->new('driver', 'Memory', 'global', 1, 'expires_in', $EXPIRES_IN)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | driver => 'Memory', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | global => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | expires_in => $EXPIRES_IN, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | return bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | min_interval => $params->{min_interval} || $MIN_INTERVAL, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | last_request => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 296 | ▶ | %{$params}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$params->{'host'} || $DEFAULT_HOST
MC/DC0%0/2
$params->{'host'} || $DEFAULT_HOST
$params->{'host'}
$DEFAULT_HOST
Truth table66%2/3
A || B
A= $params->{'host'}B= $DEFAULT_HOST
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | cache => $cache, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 296 | ▶ | host => $params->{host} || $DEFAULT_HOST, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$params->{'min_interval'} || $MIN_INTERVAL
MC/DC50%1/2
$params->{'min_interval'} || $MIN_INTERVAL
$params->{'min_interval'}
$MIN_INTERVAL
Truth table66%2/3
A || B
A= $params->{'min_interval'}B= $MIN_INTERVAL
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | ua => $ua, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | =head2 weather | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | use Geo::Location::Point; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | my $ramsgate = Geo::Location::Point->new({ latitude => 51.34, longitude => 1.42 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | my $weather = $meteo->weather($ramsgate, '2022-12-25'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | # Print snowfall at 1AM on Christmas morning in Ramsgate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | my @snowfall = @{$weather->{'hourly'}->{'snowfall'}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | print 'Snowfall at 1AM: ', $snowfall[1], " cm\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | use DateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | my $dt = DateTime->new(year => 2024, month => 2, day => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | $weather = $meteo->weather({ location => $ramsgate, date => $dt }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | The date argument can be an ISO-8601 formatted string (C<YYYY-MM-DD>), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | or any object that supports C<strftime>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | Takes an optional C<tz> argument containing the time zone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | If not given, the module tries to derive it from the location object; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | set C<TIMEZONEDB_KEY> to your API key from L<https://timezonedb.com> to enable that. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | If all else fails, the module falls back to C<Europe/London>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | Dates before 1940 return C<undef> silently. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | Invalid date strings cause a C<carp> and return C<undef>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | Missing required arguments or non-numeric coordinates cause a C<croak>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | On success returns a hashref with at minimum an C<hourly> key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | The C<daily> key includes C<sunrise> and C<sunset> as ISO-8601 datetime strings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | (e.g. C<2022-12-25T08:09>), as well as temperature, precipitation, and wind fields. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | Returns C<undef> if the API returns an error, if the JSON cannot be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | parsed, or if the response contains no C<hourly> key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | my $weather = $meteo->weather({ latitude => 51.34, longitude => 1.42, date => '2022-12-25' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | if(defined($weather)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | my $max_temp = $weather->{'daily'}->{'temperature_2m_max'}[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | my $sunrise = $weather->{'daily'}->{'sunrise'}[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | my @temps = @{$weather->{'hourly'}->{'temperature_2m'}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | print "Max temp: ${max_temp}C Sunrise: $sunrise\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | print "Temp at noon: $temps[12]C\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | Three call forms are accepted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | # Form 1 and 2 -- hashref or flat list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | latitude => { type => 'scalar' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | longitude => { type => 'scalar' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | date => { type => 'scalar | object' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | tz => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | location => { type => 'object', can => 'latitude', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # Form 3 -- positional: ($location_obj, $date) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | # $location_obj must respond to latitude() and longitude() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | { type => 'hashref', min => 1 } # success -- contains 'hourly' key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | undef # pre-1940 date, bad input, or API error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | Message Type Trigger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | --------------------------------------------------- ----- ---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | Usage: weather(latitude => ...) croak lat, lon, or date is missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | Invalid latitude/longitude format ($lat, $lon) croak coordinate is not numeric | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | '$date' is not a valid date carp date string is not YYYY-MM-DD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | Invalid date format. Expected YYYY-MM-DD croak date starts with a year but is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | exactly YYYY-MM-DD (also if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | strftime() returned something else) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | UA->get did not return a valid HTTP response carp UA returned non-response object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | $url API returned error: $status carp HTTP 4xx/5xx response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | Failed to parse JSON response: $err carp response body is not valid JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | ($err is the exception with control | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | chars stripped and length capped at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | 200 chars to prevent log injection) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | Weather::Meteo: API error: $reason carp API returned {"error":true,...} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | =head3 PSEUDOCODE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | params = _location_params(args) # hashref, flat list or (location, date) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | (lat, lon) = _coordinates(params) # croak if missing or not decimal numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | date = _archive_date(params.date) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # object with strftime => strftime('%F'); other strings must start with a year | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # return undef quietly if year < 1940; croak unless exactly YYYY-MM-DD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | return undef if date is undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | url = _build_url(archive host, '/v1/archive', hourly+daily fields, units) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | return _cached_report(key, url) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | # cache hit => return it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | # else _api_get(url): _fetch_json() with retry on 429/502/503/504; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | # carp and undef on HTTP error, bad JSON, or {"error":true} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | # undef if no 'hourly' key; else cache it and return hashref (Return::Set) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | 210 | ▶ | sub weather { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
weather: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | 210 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | 210 | my $params = $self->_location_params(\@_, 'date', 2, 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | 208 | my ($latitude, $longitude) = @{ $self->_coordinates($params, 'usage_weather', 'date') }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | 176 | ▶ | my $tz = $params->{'tz'} || $DEFAULT_TZ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$params->{'tz'} || $DEFAULT_TZ
MC/DC0%0/2
$params->{'tz'} || $DEFAULT_TZ
$params->{'tz'}
$DEFAULT_TZ
Truth table66%2/3
A || B
A= $params->{'tz'}B= $DEFAULT_TZ
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 176 | my $date = $self->_archive_date($params->{'date'}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | # undef means a date before 1940 or one that is not a date at all | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | 167 | ▶ | return unless defined($date); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | 148 | my $url = _build_url($self->{host}, '/v1/archive', _report_query( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | $latitude, $longitude, $tz, [ start_date => $date, end_date => $date ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | )); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | 148 | return $self->_cached_report("weather:$latitude:$longitude:$date:$tz", $url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | =head2 forecast | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | my $forecast = $meteo->forecast({ latitude => 51.34, longitude => 1.42 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | my @temps = @{$forecast->{'hourly'}->{'temperature_2m'}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | # Request 3 days of forecast | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | $forecast = $meteo->forecast({ latitude => 51.34, longitude => 1.42, days => 3 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | use Geo::Location::Point; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | my $ramsgate = Geo::Location::Point->new({ latitude => 51.34, longitude => 1.42 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | $forecast = $meteo->forecast($ramsgate); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | $forecast = $meteo->forecast($ramsgate, 5); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | Fetches weather forecast data from L<https://api.open-meteo.com>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | Returns up to 16 days of hourly and daily data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | The C<daily> key of the response includes C<sunrise> and C<sunset> ISO-8601 datetime strings. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | Takes an optional C<days> argument (integer 1-16, default 7). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | Anything that is not a whole number from 1 to 16, such as C<0>, C<17>, C<1.5> or C<abc>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | is replaced by 7 with a warning. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | Takes an optional C<tz> argument for the time zone; defaults to C<Europe/London>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | On success returns a hashref containing at minimum the key C<hourly>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | Returns C<undef> if the API returns an error, if the JSON cannot be parsed, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | or if the response contains no C<hourly> key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | my $forecast = $meteo->forecast({ latitude => 51.34, longitude => 1.42, days => 5 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | if(defined($forecast)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | my $daily = $forecast->{'daily'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | my @sunrises = @{$daily->{'sunrise'}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | my @max_temps = @{$daily->{'temperature_2m_max'}}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | for my $i (0 .. $#sunrises) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | print "Day $i: sunrise $sunrises[$i], max $max_temps[$i]C\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 509 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 511 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 513 | Three call forms are accepted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 515 | # Form 1 and 2 -- hashref or flat list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 516 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 517 | latitude => { type => 'scalar' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 518 | longitude => { type => 'scalar' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 519 | days => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 520 | tz => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 521 | location => { type => 'object', can => 'latitude', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 524 | # Form 3 -- positional: ($location_obj) or ($location_obj, $days) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 525 | # $location_obj must respond to latitude() and longitude() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 527 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 529 | { type => 'hashref', min => 1 } # success -- contains 'hourly' key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 530 | undef # bad input or API error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 532 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 534 | Message Type Trigger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 535 | --------------------------------------------------- ----- ---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 536 | Usage: forecast(latitude => ...) croak lat or lon is missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 537 | Invalid latitude/longitude format ($lat, $lon) croak coordinate is not numeric | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 538 | days must be between 1 and 16; defaulting to 7 carp days argument is out of range | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 539 | UA->get did not return a valid HTTP response carp UA returned non-response object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 540 | $url API returned error: $status carp HTTP 4xx/5xx response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 541 | Failed to parse JSON response: $err carp response body is not valid JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 542 | ($err is the exception with control | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 543 | chars stripped and length capped at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 544 | 200 chars to prevent log injection) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 545 | Weather::Meteo: API error: $reason carp API returned {"error":true,...} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 547 | =head3 PSEUDOCODE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 548 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 549 | params = _location_params(args) # hashref, flat list or (location, days) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 550 | (lat, lon) = _coordinates(params) # croak if missing or not decimal numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 551 | days = params.days // 7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 552 | if days is not an integer in 1..16: carp 'days_range'; days = 7 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 553 | url = _build_url(forecast host, '/v1/forecast', forecast_days, hourly+daily, units) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 554 | return _cached_report(key, url) # as for weather() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 556 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 558 | 48 | ▶ | sub forecast { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
forecast: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 559 | 48 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 560 | 48 | my $params = $self->_location_params(\@_, 'days', 1, 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 562 | 48 | my ($latitude, $longitude) = @{ $self->_coordinates($params, 'usage_forecast') }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 563 | 31 | ▶ | my $tz = $params->{'tz'} || $DEFAULT_TZ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$params->{'tz'} || $DEFAULT_TZ
MC/DC0%0/2
$params->{'tz'} || $DEFAULT_TZ
$params->{'tz'}
$DEFAULT_TZ
Truth table33%1/3
A || B
A= $params->{'tz'}B= $DEFAULT_TZ
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 564 | 31 | ▶ | my $days = $params->{'days'} // $DEFAULT_DAYS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$params->{"days"} // $DEFAULT_DAYS
MC/DC100%2/2
$params->{"days"} // $DEFAULT_DAYS
$params->{"days"}
$DEFAULT_DAYS
Truth table100%3/3
A // B
A= $params->{"days"}B= $DEFAULT_DAYS
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 566 | # Capturing untaints; out-of-range values are corrected rather than fatal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 567 | # so that a typo costs the caller a warning, not an exception. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 568 | 31 | ▶ | if($days =~ /\A(\d+)\z/ && $1 >= 1 && $1 <= $MAX_DAYS) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$days =~ /\A(\d+)\z/ and $1 >= 1 and $1 <= $MAX_DAYS
$days =~ /\A(\d+)\z/ and $1 >= 1
MC/DC100%3/3
$days =~ /\A(\d+)\z/ and $1 >= 1 and $1 <= $MAX_DAYS
$days =~ /\A(\d+)\z/
$1 >= 1
$1 <= $MAX_DAYS
Truth table100%4/4
A and B and C
A= $days =~ /\A(\d+)\z/B= $1 >= 1C= $1 <= $MAX_DAYS
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 569 | 23 | $days = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 570 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 571 | 8 | $self->_carp('days_range', { min => 1, max => $MAX_DAYS, default => $DEFAULT_DAYS }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 572 | 8 | $days = $DEFAULT_DAYS; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 573 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 575 | 31 | my $url = _build_url($FORECAST_HOST, '/v1/forecast', _report_query( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 576 | $latitude, $longitude, $tz, [ forecast_days => $days ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 577 | )); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 579 | 31 | return $self->_cached_report("forecast:$latitude:$longitude:$days:$tz", $url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 582 | =head2 sunrise_sunset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 584 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 586 | # Historical date -- uses the archive endpoint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 587 | my $times = $meteo->sunrise_sunset({ latitude => 51.34, longitude => 1.42, date => '2022-12-25' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 588 | print "Sunrise: $times->{sunrise}\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 589 | print "Sunset: $times->{sunset}\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 591 | # Today (no date given -- uses the forecast endpoint) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 592 | $times = $meteo->sunrise_sunset({ latitude => 51.34, longitude => 1.42 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 594 | use Geo::Location::Point; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 595 | my $ramsgate = Geo::Location::Point->new({ latitude => 51.34, longitude => 1.42 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 596 | $times = $meteo->sunrise_sunset($ramsgate, '2022-12-25'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 598 | Returns a hashref with C<sunrise> and C<sunset> ISO-8601 datetime strings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 599 | (e.g. C<2022-12-25T08:09>) for the given location and date. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 601 | If no date is supplied, today is used and the forecast endpoint is queried. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 602 | For historical dates (strictly before today) the archive endpoint is used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 603 | For today and future dates the forecast endpoint (L<https://api.open-meteo.com>) is used. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 605 | Takes an optional C<tz> argument for the time zone; defaults to C<Europe/London>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 607 | Returns C<undef> if the API returns an error or if the response does not contain | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 608 | sunrise/sunset data. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 610 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 612 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 613 | my $times = $meteo->sunrise_sunset({ latitude => 48.8566, longitude => 2.3522 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 615 | if(defined($times)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 616 | print "Paris sunrise today: $times->{sunrise}\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 617 | print "Paris sunset today: $times->{sunset}\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 618 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 620 | # Historical query | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 621 | my $solstice = $meteo->sunrise_sunset({ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 622 | latitude => 51.4779, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 623 | longitude => -0.0015, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 624 | date => '2024-06-21', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 625 | tz => 'Europe/London', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 626 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 627 | print "Greenwich sunrise on summer solstice 2024: $solstice->{sunrise}\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 629 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 630 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 631 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 633 | Three call forms are accepted. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 635 | # Form 1 and 2 -- hashref or flat list | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 636 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 637 | latitude => { type => 'scalar' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 638 | longitude => { type => 'scalar' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 639 | date => { type => ['scalar', 'object'], optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 640 | tz => { type => 'scalar', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 641 | location => { type => 'object', can => 'latitude', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 644 | # Form 3 -- positional: ($location_obj) or ($location_obj, $date) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 645 | # $location_obj must respond to latitude() and longitude() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 647 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 649 | { type => 'hashref' } # { sunrise => STRING, sunset => STRING } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 650 | undef # bad input or API error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 652 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 654 | Message Type Trigger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 655 | --------------------------------------------------- ----- ---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 656 | Usage: sunrise_sunset(latitude => ...) croak lat or lon is missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 657 | Invalid latitude/longitude format ($lat, $lon) croak coordinate is not numeric | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 658 | '$date' is not a valid date carp date string is not YYYY-MM-DD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 659 | UA->get did not return a valid HTTP response carp UA returned non-response object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 660 | $url API returned error: $status carp HTTP 4xx/5xx response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 661 | Failed to parse JSON response: $err carp response body is not valid JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 662 | ($err is the exception with control | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 663 | chars stripped and length capped at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 664 | 200 chars to prevent log injection) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 665 | Weather::Meteo: API error: $reason carp API returned {"error":true,...} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 667 | =head3 PSEUDOCODE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 669 | params = _location_params(args) # hashref, flat list or (location, date) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 670 | (lat, lon) = _coordinates(params) # croak if missing or not decimal numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 671 | date = params.date (strftime('%F') if an object) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 672 | if date is defined and not YYYY-MM-DD: carp 'not_a_date'; return undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 673 | use_forecast = date is undef or date >= today (host-local date) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 674 | return cached {sunrise, sunset} if present | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 675 | url = _build_url(forecast or archive host, daily=sunrise,sunset) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 676 | daily = _api_get(url).daily # carp and undef on any failure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 677 | return undef unless first sunrise and first sunset are both defined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 678 | cache and return { sunrise => ..., sunset => ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 680 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 682 | 22 | ▶ | sub sunrise_sunset { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sunrise_sunset: called CC=14 SCAR=27.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 683 | 22 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 684 | 22 | my $params = $self->_location_params(\@_, 'date', 1, 2); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 686 | 22 | my ($latitude, $longitude) = @{ $self->_coordinates($params, 'usage_sunrise') }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 687 | 19 | ▶ | my $tz = $params->{'tz'} || $DEFAULT_TZ; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$params->{'tz'} || $DEFAULT_TZ
MC/DC0%0/2
$params->{'tz'} || $DEFAULT_TZ
$params->{'tz'}
$DEFAULT_TZ
Truth table33%1/3
A || B
A= $params->{'tz'}B= $DEFAULT_TZ
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 688 | 19 | my $date = $params->{'date'}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 690 | 19 | ▶ | $date = $date->strftime('%F') if Scalar::Util::blessed($date) && $date->can('strftime'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($date) and $date->can("strftime")
MC/DC0%0/2
builtin::blessed($date) and $date->can("strftime")
builtin::blessed($date)
$date->can("strftime")
Truth table33%1/3
A and B
A= builtin::blessed($date)B= $date->can("strftime")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 692 | 19 | ▶ | if(defined($date)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 693 | # Capturing untaints; anything but YYYY-MM-DD is rejected | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 694 | 19 | my ($clean) = ($date =~ $DATE_RE); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 695 | 19 | ▶ | if(!defined($clean)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 696 | 3 | $self->_carp('not_a_date', { date => $date }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 697 | 3 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 698 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 699 | 16 | $date = $clean; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 700 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 702 | # Today and the future are not in the archive yet, so use the forecast | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 703 | # endpoint for them. With no date at all, "today" is the safest default. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 704 | 16 | my $today = POSIX::strftime('%Y-%m-%d', localtime()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 705 | 16 | ▶ | my $use_forecast = !defined($date) || ($date ge $today); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
!defined($date) || $date ge $today
MC/DC0%0/2
!defined($date) || $date ge $today
!defined($date)
$date ge $today
Truth table33%1/3
not(A) || B
A= !defined($date)B= $date ge $today
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 706 | 16 | ▶ | $date //= $today; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$date //= $today
MC/DC0%0/2
$date //= $today
$date
$today
Truth table33%1/3
A //= B
A= $dateB= $today
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 708 | 16 | my $cache_key = "sunrise_sunset:$latitude:$longitude:$date:$tz"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 709 | 16 | my $result = $self->{'cache'}->get($cache_key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 711 | 16 | ▶ | if(!$result) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 712 | my $url = _build_url( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 713 | $use_forecast ? $FORECAST_HOST : $self->{host}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 714 | 13 | ▶ | $use_forecast ? '/v1/forecast' : '/v1/archive', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 715 | [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 716 | latitude => $latitude, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 717 | longitude => $longitude, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 718 | start_date => $date, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 719 | end_date => $date, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 720 | daily => 'sunrise,sunset', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 721 | timezone => $tz, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 722 | ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 723 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 724 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 725 | # Both values must be present or the answer is useless | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 726 | 13 | ▶ | my $daily = ($self->_api_get($url) // {})->{'daily'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_api_get($url) // {}
MC/DC100%1/1
$self->_api_get($url) // {}
$self->_api_get($url)
Truth table100%2/2
A
A= $self->_api_get($url)
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 727 | 13 | my ($sunrise, $sunset) = map { _first_element($daily, $_) } qw(sunrise sunset); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 728 | 13 | ▶ | if(defined($sunrise) && defined($sunset)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $sunrise and defined $sunset
MC/DC50%1/2
defined $sunrise and defined $sunset
defined $sunrise
defined $sunset
Truth table66%2/3
A and B
A= defined $sunriseB= defined $sunset
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 729 | 12 | $result = { sunrise => $sunrise, sunset => $sunset }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 730 | 12 | $self->{'cache'}->set($cache_key, $result); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 732 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 733 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 734 | 16 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 735 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 737 | =head2 ua | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 738 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 739 | Accessor method to get and set the C<UserAgent> object used internally. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 740 | You can call C<env_proxy> for example, to get proxy information from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 741 | environment variables: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 743 | $meteo->ua()->env_proxy(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 745 | You can also replace the user agent entirely: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 747 | use LWP::UserAgent::Throttled; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 749 | my $ua = LWP::UserAgent::Throttled->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 750 | $ua->throttle('open-meteo.com' => 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 751 | $meteo->ua($ua); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 752 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 753 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 754 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 755 | my $meteo = Weather::Meteo->new(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 757 | # Getter: inspect the current UA | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 758 | my $ua = $meteo->ua(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 759 | $ua->env_proxy(1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 761 | # Setter: replace with a throttled UA | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 762 | use LWP::UserAgent::Throttled; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 763 | $meteo->ua(LWP::UserAgent::Throttled->new()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 764 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 765 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 766 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 767 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 769 | When called with no arguments acts as a getter; the input schema is empty. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 770 | When called with an argument the argument must be an object that responds to C<get>: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 772 | { ua => { type => 'object', can => 'get' } } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 774 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 776 | { type => 'object', can => 'get' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 777 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 778 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 780 | Message Type Trigger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 781 | --------------------------------------------------- ----- ---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 782 | ua() requires a defined value croak ua(undef) called | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 783 | must be an object that understands the get method croak ua arg lacks get() method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 785 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 787 | 37 | ▶ | sub ua { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ua: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 788 | 37 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 790 | 37 | ▶ | if(@_) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 791 | 20 | my $params = Params::Validate::Strict::validate_strict({ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 792 | args => Params::Get::get_params('ua', \@_), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 793 | schema => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 794 | ua => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 795 | type => 'object', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 796 | can => 'get' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 797 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 798 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 799 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 800 | # Reject undef explicitly before it silently corrupts $self->{ua} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 801 | 10 | ▶ | $self->_croak('ua_undef') unless defined($params->{ua}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 802 | 9 | $self->{ua} = $params->{ua}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 803 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 805 | 26 | return $self->{ua}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 806 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 808 | =head2 i18n | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 810 | my $text = $meteo->i18n('days_range', { min => 1, max => 16, default => 7 }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 812 | Returns the text of a diagnostic message, with its placeholders filled in. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 813 | Every warning and error raised by this module is produced through this method, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 814 | so each one can be translated or reworded without touching the code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 815 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 816 | Messages are looked up first in the C<messages> hashref given to L</new>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 817 | then in the built-in English table. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 818 | Placeholders are written C<{name}> and are replaced from the arguments hashref. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 819 | A placeholder with no matching argument is left as it is, so a missing value is | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 820 | visible in the output instead of silently vanishing. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 822 | A message may be a hashref of variants instead of a string. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 823 | The variant is chosen by the C<count> argument (C<one> if it is 1, otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 824 | C<other>) and, if a C<gender> argument is also given, the variant named | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 825 | C<one_F<gender>> or C<other_F<gender>> is preferred when it exists. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 826 | Only the English rule for singular and plural is built in; translators for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 827 | languages with more plural forms should put the form they need under C<other> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 828 | or add their own keys and select them with C<count>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 830 | If the arguments hashref has a C<sprintf> arrayref, the message is first passed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 831 | through C<sprintf> with those values, which allows C<%s> style translations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 832 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 833 | Can be called as a class method. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 834 | Confesses (with a stack trace) if the key is unknown, because that is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 835 | programming error and not something a caller can recover from. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 836 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 837 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 838 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 839 | # Built-in English | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 840 | print $meteo->i18n('invalid_host', { value => 'bad host' }), "\n"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 841 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 842 | # German overrides, with a plural | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 843 | my $de = Weather::Meteo->new(messages => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 844 | days_range => 'Tage muessen zwischen {min} und {max} liegen; verwende {default}', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 845 | days_left => { one => 'noch {count} Tag', other => 'noch {count} Tage' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 846 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 847 | print $de->i18n('days_left', { count => 3 }), "\n"; # noch 3 Tage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 848 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 849 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 851 | =head4 Input | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 852 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 853 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 854 | key => { type => 'string' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 855 | args => { type => 'hashref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 856 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 857 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 858 | =head4 Output | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 859 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 860 | { type => 'string', min => 1 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 862 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 864 | Message Type Trigger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 865 | --------------------------------------------------- ------- ---------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 866 | Weather::Meteo: no message defined for key '$key' confess key is in neither table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 867 | Weather::Meteo: no '$category' variant in message confess variant hashref lacks one/other | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 868 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 869 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 870 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 871 | 152 | ▶ | sub i18n { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
i18n: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 872 | 152 | my ($self, $key, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 873 | 152 | ▶ | $args //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args //= {}
MC/DC100%1/1
$args //= {}
$args
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 875 | # Per-object overrides win over the built-in English table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 876 | 152 | ▶ | my $overrides = ref($self) ? $self->{messages} : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 877 | 152 | ▶ | my $format = ($overrides && exists($overrides->{$key})) ? $overrides->{$key} : $MESSAGES{$key}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$overrides and exists $overrides->{$key}
MC/DC100%2/2
$overrides and exists $overrides->{$key}
$overrides
exists $overrides->{$key}
Truth table100%3/3
A and B
A= $overridesB= exists $overrides->{$key}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 879 | # An unknown key is a programming error, so show the caller's stack | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 880 | 152 | ▶ | Carp::confess("Weather::Meteo: no message defined for key '$key'") unless defined($format); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 882 | # A hashref holds plural/gender variants, e.g. { one => ..., other => ..., one_f => ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 883 | 151 | ▶ | $format = _select_variant($format, $args) if ref($format) eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 884 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 885 | # Positional sprintf arguments are optional and applied first, so that a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 886 | # '%' inside a {name} substitution is never mistaken for a conversion. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 887 | 150 | ▶ | if(ref($args->{sprintf}) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 888 | 1 | $format = sprintf($format, @{$args->{sprintf}}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 889 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 891 | # Named placeholders; unknown names are left visible rather than dropped | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 892 | 150 | ▶ | $format =~ s{\{(\w+)\}}{ exists($args->{$1}) ? ($args->{$1} // q{}) : "{$1}" }ge; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$args->{$1} // ""
MC/DC100%1/1
$args->{$1} // ""
$args->{$1}
Truth table100%2/2
A
A= $args->{$1}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 894 | 150 | return $format; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 895 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 896 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 897 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 898 | # _select_variant -- pick a plural/gender variant from a message hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 899 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 900 | # Purpose: Let a translation say "1 day" and "2 days" (or a gendered form) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 901 | # without the calling code knowing the rules. Only the English | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 902 | # rule (1 is singular, everything else plural) is built in; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 903 | # translators for other languages supply the right keys themselves. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 904 | # Entry: $variants -- hashref with some of: one, other, <category>_<gender> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 905 | # $args -- hashref; 'count' picks the category (default: other), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 906 | # 'gender' picks a gendered variant if one exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 907 | # Exit: the chosen format string; confesses if no variant matches | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 908 | # Side effects: none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 909 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 910 | 9 | ▶ | sub _select_variant :Private { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_select_variant: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 911 | 9 | my ($variants, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 913 | 9 | ▶ | my $category = (defined($args->{count}) && $args->{count} == 1) ? 'one' : 'other'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args->{"count"} and $args->{"count"} == 1
MC/DC100%2/2
defined $args->{"count"} and $args->{"count"} == 1
defined $args->{"count"}
$args->{"count"} == 1
Truth table100%3/3
A and B
A= defined $args->{"count"}B= $args->{"count"} == 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 914 | 27 | ▶ | my ($chosen) = grep { defined } ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 915 | (defined($args->{gender}) ? $variants->{"${category}_$args->{gender}"} : undef), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 916 | $variants->{$category}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 917 | $variants->{other}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 918 | 9 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 919 | 9 | ▶ | Carp::confess("Weather::Meteo: no '$category' variant in message") unless defined($chosen); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 920 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 921 | 8 | return $chosen; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 922 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 924 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 925 | # _log_error -- pass a message to the optional logger | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 926 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 927 | # Purpose: Report errors to a caller-supplied logger as well as via Carp. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 928 | # Entry: $self -- object or class name (class names have no logger) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 929 | # $message -- text to log | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 930 | # Exit: $self, so calls can be chained | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 931 | # Side effects: calls $self->{logger}->error() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 932 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 933 | 119 | ▶ | sub _log_error :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_log_error: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 934 | 119 | my ($self, $message) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 935 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 936 | # eval guard: Log::Abstraction::error() is fatal (it dies), but the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 937 | # documented contract is to return undef, so a logger must never be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 938 | # allowed to turn a warning or a handled error into an exception. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 939 | 119 | ▶ | eval { $self->{'logger'}->error($message) } if ref($self) && $self->{'logger'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $self and $self->{"logger"}
MC/DC50%1/2
ref $self and $self->{"logger"}
ref $self
$self->{"logger"}
Truth table66%2/3
A and B
A= ref $selfB= $self->{"logger"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 941 | 119 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 942 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 943 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 944 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 945 | # _croak / _carp -- raise a translated error or warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 946 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 947 | # Purpose: One place that looks up the message, logs it and hands it to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 948 | # Carp, so that every diagnostic is translatable and logged alike. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 949 | # Carp reports the caller's location, not ours, because it skips | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 950 | # frames inside this package. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 951 | # Entry: $self -- object or class name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 952 | # $key -- message key (see i18n()) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 953 | # $args -- optional hashref of placeholder values | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 954 | # Exit: _croak never returns; _carp returns $self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 955 | # Side effects: logs via the logger if there is one; writes to STDERR (_carp) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 956 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 957 | 66 | ▶ | sub _croak :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_croak: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 958 | 66 | my ($self, $key, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 959 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 960 | 66 | ▶ | my $message = $self->i18n($key, { class => (ref($self) || $self), %{$args || {}} }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
ref $self || $self
$args || {}
MC/DC0%0/2
ref $self || $self
ref $self
$self
MC/DC100%1/1
$args || {}
$args
Truth table66%2/3
A || B
A= ref $selfB= $self
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 961 | 66 | $self->_log_error($message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 962 | 66 | Carp::croak($message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 963 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 964 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 965 | 52 | ▶ | sub _carp :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_carp: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 966 | 52 | my ($self, $key, $args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 968 | 52 | ▶ | my $message = $self->i18n($key, { class => (ref($self) || $self), %{$args || {}} }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition60%3/5
ref $self || $self
$args || {}
MC/DC0%0/2
ref $self || $self
ref $self
$self
MC/DC100%1/1
$args || {}
$args
Truth table33%1/3
A || B
A= ref $selfB= $self
Truth table100%2/2
A
A= $args
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 969 | 52 | $self->_log_error($message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 970 | 52 | Carp::carp($message); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 972 | 52 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 973 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 975 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 976 | # _clone -- copy an object, overriding some of its fields | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 977 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 978 | # Purpose: Support $meteo->new(host => ...) for cheap test variants. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 979 | # Entry: $self -- Weather::Meteo object to copy | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 980 | # $params -- hashref of overrides; ua => undef means "keep the old UA" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 981 | # Exit: a new object of the same class | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 982 | # Side effects: croaks if the replacement ua cannot get() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 983 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 984 | 7 | ▶ | sub _clone :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_clone: called CC=5 SCAR=18.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 985 | 7 | my ($self, $params) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 986 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 987 | 7 | ▶ | if(exists($params->{ua})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 988 | 1 | ▶ | if(!defined($params->{ua})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 989 | 0 | delete $params->{ua}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 990 | ▶ | } elsif(!Scalar::Util::blessed($params->{ua}) || !$params->{ua}->can('get')) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
builtin::blessed($params->{'ua'}) && $params->{'ua'}->can('get')
MC/DC0%0/2
builtin::blessed($params->{'ua'}) && $params->{'ua'}->can('get')
builtin::blessed($params->{'ua'})
$params->{'ua'}->can('get')
Truth table33%1/3
A && B
A= builtin::blessed($params->{'ua'})B= $params->{'ua'}->can('get')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 991 | 0 | $self->_croak('bad_ua'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 992 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 993 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 994 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 995 | 7 | return bless { %{$self}, %{$params} }, ref($self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 996 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 997 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 998 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 999 | # _untaint_param -- validate one constructor argument and replace it by its | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1000 | # untainted capture | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1001 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1002 | # Purpose: Stop hostile values (SSRF in host, absurd retry counts) at the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1003 | # door, and satisfy taint mode. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1004 | # Entry: $class -- class name, used to raise the error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1005 | # $params -- hashref of constructor arguments (modified) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1006 | # $name -- key to check; must be in %PARAM_PATTERNS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1007 | # Exit: nothing useful | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1008 | # Side effects: croaks if the value does not match; rewrites $params->{$name} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1009 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1010 | 43 | ▶ | sub _untaint_param :Private { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_untaint_param: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1011 | 43 | my ($class, $params, $name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1012 | 43 | my ($pattern, $key) = @{ $PARAM_PATTERNS{$name} }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1014 | 43 | my ($safe) = ($params->{$name} =~ $pattern); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1015 | 43 | ▶ | $class->_croak($key, { value => $params->{$name} }) unless defined($safe); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1016 | 41 | $params->{$name} = $safe; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1017 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1018 | 41 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1019 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1020 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1021 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1022 | # _location_params -- turn any of the accepted call forms into one hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1023 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1024 | # Purpose: weather(), forecast() and sunrise_sunset() all accept a hashref, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1025 | # a flat list, or (Geo::Location::Point, $second_value). Doing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1026 | # that once keeps the three methods identical in how they read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1027 | # their arguments. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1028 | # Entry: $args -- arrayref of the caller's arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1029 | # $second_key -- name for the optional second positional value | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1030 | # $min_args -- fewest arguments the positional form takes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1031 | # $max_args -- most arguments it takes; more means a different call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1032 | # form (which Params::Get will then reject) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1033 | # Exit: hashref of named parameters | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1034 | # Side effects: none; tz comes from the location only when TIMEZONEDB_KEY is set | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1035 | # because that is when Geo::Location::Point can look it up | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1036 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1037 | 280 | ▶ | sub _location_params :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_location_params: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1038 | 280 | my ($self, $args, $second_key, $min_args, $max_args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1039 | 280 | my $first = $args->[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1040 | 280 | my $params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1041 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1042 | 280 | ▶ | if(@{$args} >= $min_args && @{$args} <= $max_args && Scalar::Util::blessed($first) && $first->can('latitude')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%9/9
@{$args;} >= $min_args and @{$args;} <= $max_args and builtin::blessed($first) and $first->can("latitude")
@{$args;} >= $min_args and @{$args;} <= $max_args and builtin::blessed($first)
@{$args;} >= $min_args and @{$args;} <= $max_args
MC/DC100%4/4
@{$args;} >= $min_args and @{$args;} <= $max_args and builtin::blessed($first) and $first->can("latitude")
@{$args;} >= $min_args
@{$args;} <= $max_args
builtin::blessed($first)
$first->can("latitude")
Truth table100%5/5
A and B and C and D
A= @{$args;} >= $min_argsB= @{$args;} <= $max_argsC= builtin::blessed($first)D= $first->can("latitude")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1043 | 13 | $params = { latitude => $first->latitude(), longitude => $first->longitude() }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1044 | 13 | ▶ | $params->{$second_key} = $args->[1] if defined($args->[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1045 | 13 | ▶ | $params->{tz} = $first->tz() if $first->can('tz') && $ENV{'TIMEZONEDB_KEY'}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$first->can("tz") and $ENV{"TIMEZONEDB_KEY"}
MC/DC100%2/2
$first->can("tz") and $ENV{"TIMEZONEDB_KEY"}
$first->can("tz")
$ENV{"TIMEZONEDB_KEY"}
Truth table100%3/3
A and B
A= $first->can("tz")B= $ENV{"TIMEZONEDB_KEY"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1046 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1047 | 267 | $params = Params::Get::get_params(undef, $args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1048 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1049 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1050 | 278 | return $params; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1051 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1052 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1053 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1054 | # _coordinates -- find, check and clean the latitude and longitude | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1055 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1056 | # Purpose: One implementation of coordinate handling for all three data | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1057 | # methods (it used to be copied into each). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1058 | # Entry: $params -- hashref from _location_params() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1059 | # $usage_key -- message key to croak with if something is missing | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1060 | # @required -- further keys of $params that must be present | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1061 | # Exit: arrayref [ $latitude, $longitude ], validated and untainted | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1062 | # Side effects: croaks on missing arguments or malformed coordinates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1063 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1064 | 278 | ▶ | sub _coordinates :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_coordinates: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1065 | 278 | my ($self, $params, $usage_key, @required) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1066 | 278 | my ($latitude, $longitude, $location) = @{$params}{qw(latitude longitude location)}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1067 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1068 | # A location object can stand in for explicit coordinates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1069 | 278 | ▶ | if(!defined($latitude) && Scalar::Util::blessed($location) && $location->can('latitude')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $latitude and builtin::blessed($location) and $location->can("latitude")
not defined $latitude and builtin::blessed($location)
MC/DC100%3/3
not defined $latitude and builtin::blessed($location) and $location->can("latitude")
not defined $latitude
builtin::blessed($location)
$location->can("latitude")
Truth table100%4/4
not(A) and B and C
A= not defined $latitudeB= builtin::blessed($location)C= $location->can("latitude")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1070 | 2 | ($latitude, $longitude) = ($location->latitude(), $location->longitude()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1071 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1072 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1073 | 278 | ▶ | if(!defined($latitude) || !defined($longitude) || grep { !defined($params->{$_}) } @required) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
not defined $latitude && defined $longitude or grep {not defined $params->{$_};} @required
defined $latitude && defined $longitude
MC/DC100%3/3
not defined $latitude && defined $longitude or grep {not defined $params->{$_};} @required
defined $latitude
defined $longitude
grep {not defined $params->{$_};} @required
Truth table100%4/4
not(A && B) or C
A= defined $latitudeB= defined $longitudeC= grep {not defined $params->{$_};} @required
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1074 | 38 | $self->_croak($usage_key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1075 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1076 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1077 | 240 | my @normalised = map { _normalise_coord($_) } ($latitude, $longitude); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1078 | 240 | ▶ | my @clean = map { $_ =~ $COORD_RE ? $1 : undef } @normalised; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1080 | 240 | ▶ | if(grep { !defined } @clean) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1081 | 14 | $self->_croak('invalid_coordinates', { latitude => $normalised[0], longitude => $normalised[1] }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1082 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1084 | 226 | return \@clean; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1085 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1087 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1088 | # _archive_date -- turn the caller's date into YYYY-MM-DD for the archive | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1089 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1090 | # Purpose: Accept a string or any strftime-capable object, and filter out | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1091 | # dates the archive cannot hold. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1092 | # Entry: $date -- string or object with strftime() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1093 | # Exit: 'YYYY-MM-DD', or undef if the date is before 1940 (silently) or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1094 | # not a date at all (with a warning) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1095 | # Side effects: carps for unparseable strings; croaks if the date looks like | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1096 | # one but is not YYYY-MM-DD | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1097 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1098 | 176 | ▶ | sub _archive_date :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_archive_date: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1099 | 176 | my ($self, $date) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1100 | 176 | my $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1102 | 176 | ▶ | if(Scalar::Util::blessed($date) && $date->can('strftime')) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($date) and $date->can("strftime")
MC/DC50%1/2
builtin::blessed($date) and $date->can("strftime")
builtin::blessed($date)
$date->can("strftime")
Truth table66%2/3
A and B
A= builtin::blessed($date)B= $date->can("strftime")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1103 | 7 | $date = $date->strftime('%F'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1104 | } elsif($date !~ /\A\d{4}-/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1105 | 11 | $self->_carp('not_a_date', { date => $date }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1106 | 11 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1107 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1109 | # The year test comes first so that pre-1940 input is always quietly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1110 | # undef, whether it came as a string or as an object. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1111 | 165 | my ($year) = ($date =~ /\A(\d{4})-/); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1112 | 165 | ▶ | if(!defined($year) || $year >= $FIRST_YEAR) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $year or $year >= $FIRST_YEAR
MC/DC100%2/2
not defined $year or $year >= $FIRST_YEAR
not defined $year
$year >= $FIRST_YEAR
Truth table100%3/3
not(A) or B
A= not defined $yearB= $year >= $FIRST_YEAR
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1113 | 157 | ($result) = ($date =~ $DATE_RE); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1114 | 157 | ▶ | $self->_croak('invalid_date_format') unless defined($result); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1116 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1117 | 156 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1118 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1119 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1120 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1121 | # _report_query -- the query parameters shared by weather() and forecast() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1122 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1123 | # Purpose: Both endpoints return the same hourly/daily structure, so they | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1124 | # must request the same fields in the same units. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1125 | # Entry: $latitude, $longitude, $tz -- already validated | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1126 | # $extra -- arrayref of endpoint-specific key/value pairs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1127 | # Exit: arrayref of key/value pairs, order preserved for stable URLs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1128 | # Side effects: none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1129 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1130 | 179 | ▶ | sub _report_query :Private { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_report_query: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1131 | 179 | my ($latitude, $longitude, $tz, $extra) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1132 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1133 | return [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1134 | latitude => $latitude, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1135 | longitude => $longitude, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1136 | 179 | @{$extra}, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1137 | hourly => $HOURLY_FIELDS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1138 | daily => $DAILY_FIELDS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1139 | timezone => $tz, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1140 | @UNIT_PAIRS, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1141 | ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1142 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1143 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1144 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1145 | # _build_url -- assemble an API URL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1146 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1147 | # Purpose: Build the URL and keep commas readable; URI would otherwise | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1148 | # encode them as %2C, which the API accepts but humans cannot read | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1149 | # in logs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1150 | # Entry: $host -- validated hostname | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1151 | # $path -- endpoint path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1152 | # $pairs -- arrayref of key/value query pairs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1153 | # Exit: URL string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1154 | # Side effects: none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1155 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1156 | 192 | ▶ | sub _build_url :Private { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_url: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1157 | 192 | my ($host, $path, $pairs) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1159 | 192 | my $uri = URI->new("https://$host$path"); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1160 | 192 | $uri->query_form(@{$pairs}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1161 | 192 | (my $url = $uri->as_string()) =~ s/%2C/,/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1163 | 192 | return $url; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1164 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1165 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1166 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1167 | # _first_element -- the first entry of a named array inside a hashref | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1168 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1169 | # Purpose: The API wraps single-day answers in one-element arrays, and a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1170 | # broken response may lack the array altogether. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1171 | # Entry: $hash -- hashref or anything else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1172 | # $key -- name of the array | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1173 | # Exit: the first element, or undef if there is none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1174 | # Side effects: none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1175 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1176 | 26 | ▶ | sub _first_element :Private { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_first_element: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1177 | 26 | my ($hash, $key) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1178 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1179 | 26 | ▶ | return (ref($hash) eq 'HASH' && ref($hash->{$key}) eq 'ARRAY') ? $hash->{$key}[0] : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $hash eq "HASH" and ref $hash->{$key} eq "ARRAY"
MC/DC50%1/2
ref $hash eq "HASH" and ref $hash->{$key} eq "ARRAY"
ref $hash eq "HASH"
ref $hash->{$key} eq "ARRAY"
Truth table66%2/3
A and B
A= ref $hash eq "HASH"B= ref $hash->{$key} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1180 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1182 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1183 | # _normalise_coord -- fix a coordinate that leads with a bare decimal point | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1184 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1185 | # Purpose: Perl and many user inputs write ".5" where "0.5" is required. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1186 | # The coordinate pattern rejects the bare-dot form, so we | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1187 | # normalise before validating. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1188 | # Entry: $coord -- a coordinate string, possibly with a leading "." | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1189 | # Exit: normalised string: ".5" -> "0.5", "-.5" -> "-0.5", others unchanged | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1190 | # Side effects: none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1191 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1192 | 480 | ▶ | sub _normalise_coord :Private { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalise_coord: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1193 | 480 | my $result = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1195 | # Anchored with \z; atomic group on \d+ prevents O(n) backtracking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1196 | 480 | ▶ | if(my ($frac) = $result =~ /\A-\.((?>\d+))\z/) { $result = "-0.$frac" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1197 | 5 | elsif($result =~ /\A\./) { $result = "0$result" } | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1198 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1199 | 480 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1200 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1201 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1202 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1203 | # _enforce_rate_limit -- sleep until enough time has passed since the last call | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1204 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1205 | # Purpose: Prevent hammering the API when min_interval > 0. Uses | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1206 | # high-resolution time: whole-second time() could under- or | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1207 | # over-sleep by nearly a second. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1208 | # Entry: $self -- Weather::Meteo instance with last_request and min_interval | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1209 | # Exit: $self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1210 | # Side effects: may block for up to min_interval seconds via Time::HiRes::sleep | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1211 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1212 | 186 | ▶ | sub _enforce_rate_limit :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_enforce_rate_limit: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1213 | 186 | my $self = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1214 | 186 | my $elapsed = Time::HiRes::time() - $self->{last_request}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1215 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1216 | 186 | ▶ | Time::HiRes::sleep($self->{min_interval} - $elapsed) if $elapsed < $self->{min_interval}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1217 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1218 | 186 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1219 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1220 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1221 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1222 | # _backoff_seconds -- how long to wait before the next retry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1223 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1224 | # Purpose: Exponential backoff, unless the server said how long to wait. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1225 | # Entry: $res -- the failed HTTP::Response | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1226 | # $attempt -- 0 for the first retry, 1 for the second, ... | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1227 | # $base_delay -- seconds before the first retry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1228 | # Exit: seconds to wait, never more than $MAX_RETRY_WAIT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1229 | # Side effects: none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1230 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1231 | 13 | ▶ | sub _backoff_seconds :Private { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_backoff_seconds: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1232 | 13 | my ($res, $attempt, $base_delay) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1233 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1234 | # Only the numeric (delta-seconds) form of Retry-After is honoured; the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1235 | # HTTP-date form would need clock agreement with the server. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1236 | 13 | ▶ | my ($retry_after) = (($res->header('Retry-After') // '') =~ /\A(\d{1,6})\z/); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$res->header("Retry-After") // ""
MC/DC100%1/1
$res->header("Retry-After") // ""
$res->header("Retry-After")
Truth table100%2/2
A
A= $res->header("Retry-After")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1237 | 13 | ▶ | my $wait = $retry_after // ($base_delay * (2 ** $attempt)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$retry_after // $base_delay * 2 ** $attempt
MC/DC100%1/1
$retry_after // $base_delay * 2 ** $attempt
$retry_after
Truth table100%2/2
A
A= $retry_after
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1238 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1239 | 13 | ▶ | return $wait > $MAX_RETRY_WAIT ? $MAX_RETRY_WAIT : $wait; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1240 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1241 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1242 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1243 | # _get_with_retry -- GET a URL, retrying transient failures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1244 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1245 | # Purpose: Ride out rate limiting (429) and brief outages (502-504). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1246 | # Entry: $self -- object; honours max_retries, retry_delay, min_interval | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1247 | # $url -- fully-formed request URL | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1248 | # Exit: the last HTTP::Response (or whatever the UA returned) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1249 | # Side effects: sleeps between attempts; updates last_request | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1250 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1251 | 173 | ▶ | sub _get_with_retry :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_with_retry: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1252 | 173 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1253 | 173 | ▶ | my $max_retries = $self->{max_retries} // $MAX_RETRIES; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"max_retries"} // $MAX_RETRIES
MC/DC100%2/2
$self->{"max_retries"} // $MAX_RETRIES
$self->{"max_retries"}
$MAX_RETRIES
Truth table100%3/3
A // B
A= $self->{"max_retries"}B= $MAX_RETRIES
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1254 | 173 | ▶ | my $base_delay = $self->{retry_delay} // $RETRY_DELAY; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$self->{"retry_delay"} // $RETRY_DELAY
MC/DC100%2/2
$self->{"retry_delay"} // $RETRY_DELAY
$self->{"retry_delay"}
$RETRY_DELAY
Truth table100%3/3
A // B
A= $self->{"retry_delay"}B= $RETRY_DELAY
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1255 | 173 | my $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1256 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1257 | 173 | foreach my $attempt (0 .. $max_retries) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1258 | 186 | $self->_enforce_rate_limit(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1259 | 186 | $res = $self->{ua}->get($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1260 | 186 | $self->{last_request} = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1261 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1262 | 186 | ▶ | last unless _is_retryable($res) && $attempt < $max_retries; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
_is_retryable($res) and $attempt < $max_retries
MC/DC100%2/2
_is_retryable($res) and $attempt < $max_retries
_is_retryable($res)
$attempt < $max_retries
Truth table100%3/3
A and B
A= _is_retryable($res)B= $attempt < $max_retries
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1263 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1264 | 13 | my $wait = _backoff_seconds($res, $attempt, $base_delay); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1265 | 13 | ▶ | Time::HiRes::sleep($wait) if $wait > 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1266 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1267 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1268 | 173 | return $res; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1269 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1270 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1271 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1272 | # _is_retryable -- is this response worth another attempt? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1273 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1274 | # Purpose: Keep the list of transient statuses in one place (%RETRYABLE_STATUS). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1275 | # Entry: $res -- anything the UA returned | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1276 | # Exit: true for an HTTP error response with a transient status | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1277 | # Side effects: none | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1278 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1279 | 186 | ▶ | sub _is_retryable :Private { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_is_retryable: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1280 | 186 | my $res = $_[0]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1281 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1282 | return Scalar::Util::blessed($res) && $res->can('is_error') && $res->is_error() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1283 | 186 | ▶ | && $RETRYABLE_STATUS{ $res->code() }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%9/9
builtin::blessed($res) && $res->can('is_error') && $res->is_error && $RETRYABLE_STATUS{$res->code}
builtin::blessed($res) and $res->can("is_error") and $res->is_error
builtin::blessed($res) and $res->can("is_error")
MC/DC100%4/4
builtin::blessed($res) && $res->can('is_error') && $res->is_error && $RETRYABLE_STATUS{$res->code}
builtin::blessed($res)
$res->can("is_error")
$res->is_error
$RETRYABLE_STATUS{$res->code}
Truth table100%5/5
A and B and C && D
A= builtin::blessed($res)B= $res->can("is_error")C= $res->is_errorD= $RETRYABLE_STATUS{$res->code}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1284 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1286 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1287 | # _fetch_json -- HTTP GET a URL and decode the response body as JSON | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1288 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1289 | # Purpose: Centralise the HTTP dispatch and JSON parsing steps that are | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1290 | # common to all three public data methods, giving one place to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1291 | # maintain error-handling and rate-limiting logic. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1292 | # Entry: $self -- Weather::Meteo instance | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1293 | # $url -- fully-formed request URL string | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1294 | # Exit: decoded JSON value on success; undef on error | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1295 | # Side effects: enforces rate limit (may sleep); updates last_request timestamp; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1296 | # carps on HTTP errors and JSON parse failures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1297 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1298 | 173 | ▶ | sub _fetch_json :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch_json: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1299 | 173 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1300 | 173 | my $res = $self->_get_with_retry($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1301 | 173 | my $decoded; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1303 | 173 | ▶ | if(!(Scalar::Util::blessed($res) && $res->can('is_error'))) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
builtin::blessed($res) && $res->can('is_error')
MC/DC100%2/2
builtin::blessed($res) && $res->can('is_error')
builtin::blessed($res)
$res->can('is_error')
Truth table100%3/3
A && B
A= builtin::blessed($res)B= $res->can('is_error')
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1304 | 7 | $self->_carp('bad_response'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1305 | } elsif($res->is_error()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1306 | 11 | $self->_carp('http_error', { url => $url, status => $res->status_line() }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1307 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1308 | # charset 'none' leaves the bytes alone: JSON is UTF-8 by definition | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1309 | # and the decoder below does that conversion itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1310 | 155 | eval { $decoded = JSON::MaybeXS->new()->utf8()->decode($res->decoded_content(charset => 'none')) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1311 | 155 | ▶ | if($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1312 | # Strip control characters and cap the length so that a hostile | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1313 | # response body cannot inject log lines or flood the log. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1314 | 7 | (my $error = "$@") =~ s/[[:cntrl:]]/ /g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1315 | 7 | ▶ | $error = substr($error, 0, 200) . '...' if length($error) > 200; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1316 | 7 | $self->_carp('json_parse', { error => $error }); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1317 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1319 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1320 | 173 | return $decoded; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1321 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1323 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1324 | # _api_get -- fetch a URL and return the response if the API accepted it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1325 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1326 | # Purpose: Open-Meteo reports some failures as HTTP 200 with | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1327 | # {"error":true,"reason":"..."}; treat those as failures too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1328 | # Entry: $self, $url | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1329 | # Exit: hashref, or undef on any failure | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1330 | # Side effects: as _fetch_json; also carps with the API's reason | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1331 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1332 | 173 | ▶ | sub _api_get :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_api_get: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1333 | 173 | my ($self, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1334 | 173 | my $rc = $self->_fetch_json($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1335 | 173 | my $result = undef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1336 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1337 | 173 | ▶ | if(ref($rc) eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1338 | 146 | ▶ | if($rc->{'error'}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1339 | # Surface the API-provided reason so callers can diagnose failures | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1340 | 5 | ▶ | $self->_carp('api_error', { reason => $rc->{'reason'} // 'unknown' }); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$rc->{"reason"} // "unknown"
MC/DC100%1/1
$rc->{"reason"} // "unknown"
$rc->{"reason"}
Truth table100%2/2
A
A= $rc->{"reason"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1341 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1342 | 141 | $result = $rc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1345 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1346 | 173 | return $result; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1347 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1348 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1349 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1350 | # _cached_report -- fetch an hourly/daily report, using the cache | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1351 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1352 | # Purpose: weather() and forecast() differ only in the URL and cache key. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1353 | # Entry: $self, $cache_key, $url | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1354 | # Exit: hashref with an 'hourly' key, or undef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1355 | # Side effects: reads and writes the cache; makes an HTTP request on a miss | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1356 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1357 | 179 | ▶ | sub _cached_report :Protected { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cached_report: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1358 | 179 | my ($self, $cache_key, $url) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1359 | 179 | my $report = $self->{'cache'}->get($cache_key); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1360 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1361 | 179 | ▶ | if(!$report) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1362 | 160 | my $rc = $self->_api_get($url); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1363 | 160 | ▶ | if(defined($rc) && defined($rc->{'hourly'})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $rc and defined $rc->{"hourly"}
MC/DC100%2/2
defined $rc and defined $rc->{"hourly"}
defined $rc
defined $rc->{"hourly"}
Truth table100%3/3
A and B
A= defined $rcB= defined $rc->{"hourly"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1364 | 125 | $self->{'cache'}->set($cache_key, $rc); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1365 | 125 | $report = $rc; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1366 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1367 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1368 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1369 | 179 | ▶ | return $report ? Return::Set::set_return($report, { type => 'hashref', min => 1 }) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1370 | 15 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1371 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1372 | =head1 LIMITATIONS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1374 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1375 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1376 | =item * Archive data lag | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1377 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1378 | The Open-Meteo archive endpoint has a lag of approximately five days before | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1379 | recent historical data becomes available. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1380 | For dates within the past five days, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1381 | C<weather()> may return C<undef> even when no error occurs. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1382 | Use C<forecast()> or C<sunrise_sunset()> (without a date) to obtain data for | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1383 | today or recent days. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1384 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1385 | =item * Coordinate range | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1386 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1387 | The module normalises coordinates with a bare leading decimal point (e.g. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1388 | C<".5"> to C<"0.5">) but does not validate that latitude is within C<-90..90> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1389 | or longitude within C<-180..180>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1390 | Out-of-range values are passed to the API, which may return an error. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1391 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1392 | =item * No sub-hourly resolution | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1393 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1394 | The hourly data arrays always contain exactly 24 entries per day (one per hour). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1395 | Sub-hourly resolution is not supported by this interface. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1396 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1397 | =item * Per-process rate limiting | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1398 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1399 | The C<min_interval> rate limiter tracks the last request timestamp within a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1400 | single process instance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1401 | Multiple concurrent processes or threads are not coordinated and may collectively | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1402 | exceed the desired request rate. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1403 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1404 | =item * Timezone resolution requires an API key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1405 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1406 | Automatic per-location timezone resolution requires setting the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1407 | C<TIMEZONEDB_KEY> environment variable to a valid key from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1408 | L<https://timezonedb.com>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1409 | Without it the module defaults to C<Europe/London> for all locations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1411 | =item * No list-context support | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1413 | C<weather()> and C<forecast()> enforce scalar/hashref context via | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1414 | L<Return::Set>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1415 | List context is not currently supported. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1416 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1417 | =item * Access control is enforced, but only by convention for subclasses | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1418 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1419 | Helper functions with no need of an object (coordinate and URL handling, retry | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1420 | arithmetic) are L<Sub::Private> and cannot be called, overridden or mocked from | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1421 | outside this file. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1422 | Helper methods that need the object are L<Sub::Protected>: callable from this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1423 | class and its subclasses but not from unrelated code. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1424 | White-box tests therefore have to go through the public methods or subclass | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1425 | C<Weather::Meteo>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1426 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1427 | Both modules switch their checks off when C<$ENV{HARNESS_ACTIVE}> is set, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1428 | which C<prove> and C<make test> do, so under a test harness the helpers can be | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1429 | called from anywhere. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1430 | Set C<$Sub::Private::config{harness_bypass}> and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1431 | C<$Sub::Protected::config{harness_bypass}> to C<0> to test the enforcement | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1432 | itself, as F<t/i18n.t> does. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1433 | Also note that L<Sub::Protected> registers the C<:Protected> attribute in | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1434 | C<UNIVERSAL>, so it is available to every package in the process once this | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1435 | module is loaded. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1436 | The cost of this strictness is that a subclass can reuse the protected methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1437 | but not the private functions, so it cannot change, for example, how URLs are built. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1438 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1439 | =item * Errors are reported as C<undef> plus a warning | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1440 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1441 | Every failure of C<weather()>, C<forecast()> and C<sunrise_sunset()> that is not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1442 | a usage error comes back as C<undef> with a C<carp>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1443 | A caller therefore cannot tell a rate limit from an outage from an unknown | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1444 | place without parsing the warning text. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1445 | This is the documented contract, kept for compatibility, but an exception | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1446 | class (or at least a C<last_error()> accessor) would be a better design. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1447 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1448 | =item * Retries block the caller | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1449 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1450 | Retrying a transient failure sleeps in the calling process, for up to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1451 | C<max_retries> times the (doubling, capped) delay: about 7 seconds with the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1452 | defaults. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1453 | Set C<max_retries> to C<0> for a non-blocking client, or do the retrying | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1454 | yourself. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1455 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1456 | =item * Messages are translatable, but the plural rules are not | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1457 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1458 | L</i18n> understands C<one> and C<other> only, which is right for English, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1459 | German and French but not for languages with several plural forms. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1460 | It also does not translate strings that come from outside the module, such as | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1461 | the text of an HTTP status line or an exception from the JSON decoder. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1462 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1463 | =item * Dates are checked by shape, not by calendar | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1464 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1465 | C<2022-02-31> and C<2022-13-45> have the shape C<YYYY-MM-DD> and are passed to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1466 | the API, which rejects them. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1467 | The module does not check them itself because no calendar module is a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1468 | dependency. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1469 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1470 | =item * "Today" is the host's date, not the location's | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1471 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1472 | When C<sunrise_sunset()> is called without a date, or with today's date, "today" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1473 | is taken from the local clock of the machine running the code, not from the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1474 | C<tz> requested. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1475 | Near midnight and for distant time zones that can be a day out. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1476 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1477 | =item * Cache keys use the text of the coordinates | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1478 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1479 | C<51.34> and C<51.340> are cached separately, and so are C<tz> spellings that | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1480 | the API treats as the same zone. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1481 | The results are correct; only the hit rate suffers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1482 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1483 | =item * Host parameter restricted to plain DNS hostnames | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1484 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1485 | The C<host> constructor parameter (and the C<WEATHER__METEO__host> environment | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1486 | variable) must match C</\A[A-Za-z0-9][A-Za-z0-9.\-]*(:\d{1,5})?\z/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1487 | IP addresses in CIDR notation, URLs with path components, C<@>-style | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1488 | user-info, and other special characters are rejected with a C<croak> to | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1489 | prevent Server-Side Request Forgery. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1490 | If you need to test against a local service on a non-standard port, use a | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1491 | plain C<hostname:port> string (e.g. C<localhost:8080>). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1492 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1493 | =item * Coordinate values limited to decimal numbers | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1494 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1495 | Latitude and longitude must match C</\A-?(?>\d+)(?:\.(?>\d+))?\z/> after | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1496 | leading-decimal normalisation. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1497 | Exponential notation (C<1.5e2>), hex (C<0x1F>), and strings with embedded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1498 | whitespace are rejected. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1499 | Pass a pre-formatted decimal string rather than a Perl numeric expression if | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1500 | your caller might produce non-decimal representations. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1501 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1502 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1503 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1504 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1505 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1506 | Nigel Horne, C<< <njh@nigelhorne.com> >> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1507 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1508 | This library is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1509 | it under the terms of the GNU General Public License, version 2. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1510 | See L</LICENSE AND COPYRIGHT> below. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1511 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1512 | Lots of thanks to the folks at L<https://open-meteo.com>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1513 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1514 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1515 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1516 | Please report any bugs or feature requests to C<bug-weather-meteo at rt.cpan.org>, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1517 | or through the web interface at | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1518 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Weather-Meteo>. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1519 | I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1520 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1521 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1522 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1523 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1524 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1525 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1526 | =item * Open Meteo API: L<https://open-meteo.com/en/docs#api_form> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1527 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1528 | =item * L<Configure an Object at Runtime|Object::Configure> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1529 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1530 | =item * L<Test Dashboard|https://nigelhorne.github.io/Weather-Meteo/coverage/> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1531 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1532 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1533 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1534 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1535 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1536 | This module is provided as-is without any warranty. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1537 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1538 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1539 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1540 | perldoc Weather::Meteo | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1541 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1542 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1543 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1544 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1545 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1546 | =item * MetaCPAN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1547 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1548 | L<https://metacpan.org/release/Weather-Meteo> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1549 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1550 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1551 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1552 | L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Weather-Meteo> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1553 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1554 | =item * CPANTS | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1555 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1556 | L<http://cpants.cpanauthors.org/dist/Weather-Meteo> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1557 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1558 | =item * CPAN Testers' Matrix | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1559 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1560 | L<http://matrix.cpantesters.org/?dist=Weather-Meteo> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1561 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1562 | =item * CPAN Testers Dependencies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1563 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1564 | L<http://deps.cpantesters.org/?module=Weather-Meteo> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1565 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1566 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1567 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1568 | =head1 FORMAL SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1569 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1570 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1571 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1572 | ___ NEW ___________________________________________________ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1573 | | class? : PACKAGE | Weather::Meteo | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1574 | | params? : NAME |--> VALUE | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1575 | |___________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1576 | | result! : Weather::Meteo | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1577 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1578 | | blessed(result!) = 'Weather::Meteo' | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1579 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1580 | | params?.ua? => result!.ua = params?.ua | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1581 | | ~params?.ua => result!.ua : LWP::UserAgent | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1582 | | params?.cache? => result!.cache = params?.cache | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1583 | | ~params?.cache => result!.cache : CHI(Memory, global) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1584 | | params?.host? ^ valid_hostname(params?.host?) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1585 | | => result!.host = params?.host? | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1586 | | params?.host? ^ ~valid_hostname(params?.host?) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1587 | | => croak /Invalid host/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1588 | | valid_hostname(h) ::= h =~ /\A[A-Za-z0-9][A-Za-z0-9.\-]*(:\d{1,5})?\z/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1589 | | ~params?.host? => result!.host = DEFAULT_HOST | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1590 | | params?.max_retries? ^ ~(params?.max_retries? =~ /\A\d{1,3}\z/) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1591 | | => croak /Invalid max_retries/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1592 | | params?.retry_delay? ^ ~(params?.retry_delay? =~ /\A\d{1,4}(\.\d{1,3})?\z/) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1593 | | => croak /Invalid retry_delay/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1594 | | params?.messages? ^ ~hashref(params?.messages?) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1595 | | => croak /'messages' argument must be a hash reference/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1596 | | params?.min_interval? => result!.min_interval = params?.min_interval | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1597 | | ~params?.min_interval => result!.min_interval = 0 | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1598 | | result!.last_request = 0 | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1599 | |___________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1600 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1601 | | PRE: class? is PACKAGE name or blessed Weather::Meteo | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1602 | | POST: blessed(result!) = 'Weather::Meteo' | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1603 | | forall k in params? . result!.k = params?.k | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1604 | |___________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1605 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1606 | =head2 weather | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1607 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1608 | ___ WEATHER _______________________________________________ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1609 | | self? : Weather::Meteo | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1610 | | latitude? : REAL | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1611 | | longitude? : REAL | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1612 | | date? : DATE_STRING | strftime_OBJECT | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1613 | | tz? : STRING (optional, default 'Europe/London')| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1614 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1615 | | result! : HASHREF | undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1616 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1617 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1618 | | PRE (~latitude? v ~longitude? v ~date?) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1619 | | => croak /Usage: weather\(latitude/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1620 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1621 | | PRE lat? or lon? not matching | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1622 | | /\A(-?(?>\d+)(?:\.(?>\d+))?)\z/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1623 | | (after leading-decimal normalisation via _normalise_coord) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1624 | | => croak /Invalid latitude\/longitude format/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1625 | | NOTE: list-context capture untaints lat/lon (perl -T) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1626 | | atomic groups eliminate O(n) backtracking | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1627 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1628 | | PRE date? blessed ^ date?.can('strftime') | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1629 | | => date? := date?.strftime('%F') | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1630 | | PRE date? !~ /\A\d{4}-\d{2}-\d{2}\z/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1631 | | => croak /Invalid date format. Expected YYYY-MM-DD/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1632 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1633 | | PRE year(date?) < 1940 (string or strftime object) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1634 | | => result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1635 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1636 | | POST cache hit for (lat, lon, date, tz) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1637 | | => result! = cached_value | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1638 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1639 | | POST HTTP error response | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1640 | | => carp msg ^ result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1641 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1642 | | POST JSON parse failure | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1643 | | => carp /Failed to parse JSON response/ ^ result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1644 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1645 | | POST response.error = true | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1646 | | => carp /API error: reason/ ^ result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1647 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1648 | | POST ~response.hourly | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1649 | | => result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1650 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1651 | | POST otherwise | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1652 | | => result! = { hourly => HOURLY, daily => DAILY } | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1653 | | cache.set(key, result!) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1654 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1655 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1656 | =head2 forecast | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1657 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1658 | ___ FORECAST ______________________________________________ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1659 | | self? : Weather::Meteo | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1660 | | latitude? : REAL | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1661 | | longitude? : REAL | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1662 | | days? : INTEGER [1..16] (optional, default 7) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1663 | | tz? : STRING (optional, default 'Europe/London')| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1664 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1665 | | result! : HASHREF | undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1666 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1667 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1668 | | PRE (~latitude? v ~longitude?) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1669 | | => croak /Usage: forecast\(latitude/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1670 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1671 | | PRE lat? or lon? not matching | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1672 | | /\A(-?(?>\d+)(?:\.(?>\d+))?)\z/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1673 | | (after leading-decimal normalisation via _normalise_coord) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1674 | | => croak /Invalid latitude\/longitude format/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1675 | | NOTE: list-context capture untaints lat/lon (perl -T) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1676 | | atomic groups eliminate O(n) backtracking | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1677 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1678 | | PRE days? defined ^ (days? < 1 v days? > 16) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1679 | | => carp /days must be between 1 and 16/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1680 | | days? := 7 | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1681 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1682 | | POST cache hit for (lat, lon, days, tz) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1683 | | => result! = cached_value | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1684 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1685 | | POST HTTP error response | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1686 | | => carp msg ^ result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1687 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1688 | | POST JSON parse failure | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1689 | | => carp /Failed to parse JSON response/ ^ result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1690 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1691 | | POST response.error = true | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1692 | | => result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1693 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1694 | | POST ~response.hourly | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1695 | | => result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1696 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1697 | | POST otherwise | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1698 | | => result! = { hourly => HOURLY, daily => DAILY } | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1699 | | cache.set(key, result!) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1700 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1701 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1702 | =head2 sunrise_sunset | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1703 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1704 | ___ SUNRISE_SUNSET ________________________________________ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1705 | | self? : Weather::Meteo | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1706 | | latitude? : REAL | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1707 | | longitude? : REAL | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1708 | | date? : DATE_STRING (optional, default today) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1709 | | tz? : STRING (optional, default 'Europe/London')| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1710 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1711 | | result! : HASHREF | undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1712 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1713 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1714 | | PRE (~latitude? v ~longitude?) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1715 | | => croak /Usage: sunrise_sunset\(latitude/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1716 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1717 | | PRE lat? or lon? not matching | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1718 | | /\A(-?(?>\d+)(?:\.(?>\d+))?)\z/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1719 | | (after leading-decimal normalisation via _normalise_coord) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1720 | | => croak /Invalid latitude\/longitude format/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1721 | | NOTE: list-context capture untaints lat/lon (perl -T) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1722 | | atomic groups eliminate O(n) backtracking | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1723 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1724 | | PRE date? defined ^ date? !~ /\A\d{4}-\d{2}-\d{2}\z/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1725 | | => carp /not a valid date/ ^ result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1726 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1727 | | POST ~date? v date? >= today | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1728 | | => uses forecast endpoint (api.open-meteo.com) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1729 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1730 | | POST date? < today | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1731 | | => uses archive endpoint (archive-api.open-meteo.com) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1732 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1733 | | POST cache hit for (lat, lon, date, tz) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1734 | | => result! = cached_value | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1735 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1736 | | POST HTTP error or JSON failure or ~daily.sunrise | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1737 | | => result! = undef | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1738 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1739 | | POST otherwise | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1740 | | => result! = { sunrise => ISO8601, sunset => ISO8601 } | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1741 | | cache.set(key, result!) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1742 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1743 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1744 | =head2 ua | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1745 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1746 | ___ UA ____________________________________________________ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1747 | | self? : Weather::Meteo | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1748 | | ua? : OBJECT [can 'get'] (optional) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1749 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1750 | | result! : OBJECT [can 'get'] | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1751 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1752 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1753 | | PRE ua? defined ^ ~ua?.can('get') | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1754 | | => croak /must be an object that understands the get method/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1755 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1756 | | POST ua? defined | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1757 | | => self?.ua = ua? ^ result! = ua? | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1758 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1759 | | POST ~ua? | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1760 | | => result! = self?.ua (no state change) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1761 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1762 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1763 | =head2 i18n | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1764 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1765 | ___ I18N __________________________________________________ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1766 | | self? : Weather::Meteo | PACKAGE | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1767 | | key? : STRING | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1768 | | args? : NAME |--> VALUE (optional, default empty) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1769 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1770 | | result! : STRING | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1771 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1772 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1773 | | format == self?.messages(key?) if defined | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1774 | | == BUILTIN(key?) otherwise | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1775 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1776 | | ~defined(format) => confess /no message defined for key/ | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1777 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1778 | | format : HASHREF | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1779 | | => format := variant(format, args?.count, args?.gender)| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1780 | | variant prefers <cat>_<gender>, then <cat>, then | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1781 | | other; cat = 'one' if count = 1 else 'other' | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1782 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1783 | | args?.sprintf : ARRAY | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1784 | | => format := sprintf(format, args?.sprintf) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1785 | | | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1786 | | result! = format with every {n} in dom(args?) replaced | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1787 | | by args?.n (undef becomes the empty string; | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1788 | | names not in dom(args?) are left as {n}) | | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1789 | |____________________________________________________________| | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1790 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1791 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1792 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1793 | Copyright 2023-2026 Nigel Horne. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1794 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1795 | Usage is subject to the GPL2 licence terms. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1796 | If you use it, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1797 | please let me know. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1798 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1799 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1800 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 1801 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||