| 1 | package Weather::WeatherKit; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Crypt::JWT qw(encode_jwt); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | Weather::WeatherKit - Apple WeatherKit REST API client | ||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | Version 0.1 | ||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | our $VERSION = '0.1'; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | use Weather::WeatherKit; | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | my $wk = Weather::WeatherKit->new( | ||||||||||||||||||||||||||||||||||||||||||
| 27 | team_id => $apple_team_id, # Apple Developer Team Id | ||||||||||||||||||||||||||||||||||||||||||
| 28 | service_id => $weatherkit_service_id, # WeatherKit Service Id | ||||||||||||||||||||||||||||||||||||||||||
| 29 | key_id => $key_id, # WeatherKit developer key ID | ||||||||||||||||||||||||||||||||||||||||||
| 30 | key => $private_key # Encrypted private key (PEM) | ||||||||||||||||||||||||||||||||||||||||||
| 31 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | my $report = $wk->get( | ||||||||||||||||||||||||||||||||||||||||||
| 34 | lat => $lat, # Latitude | ||||||||||||||||||||||||||||||||||||||||||
| 35 | lon => $lon, # Longitude | ||||||||||||||||||||||||||||||||||||||||||
| 36 | dataSets => $datasets # e.g. currentWeather (comma-separated list) | ||||||||||||||||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | Weather::WeatherKit provides basic access to the Apple WeatherKit REST API (v1). | ||||||||||||||||||||||||||||||||||||||||||
| 42 | WeatherKit replaces the Dark Sky API and requires an Apple developer subscription. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | Pease see the L<official API documentation|https://developer.apple.com/documentation/weatherkitrestapi> | ||||||||||||||||||||||||||||||||||||||||||
| 45 | for datasets and usage options as well as the L<required attribution|https://developer.apple.com/weatherkit/get-started/#attribution-requirements>. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | It was made to serve the apps L<Xasteria|https://astro.ecuadors.net/xasteria/> and | ||||||||||||||||||||||||||||||||||||||||||
| 48 | L<Polar Scope Align|https://astro.ecuadors.net/polar-scope-align/>, but if your service | ||||||||||||||||||||||||||||||||||||||||||
| 49 | requires some extra functionality, feel free to contact the author about it. | ||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =head1 CONSTRUCTOR METHOD | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | =head2 C<new> | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | my $wk = Weather::WeatherKit->new( | ||||||||||||||||||||||||||||||||||||||||||
| 56 | team_id => "MLU84X58U4", | ||||||||||||||||||||||||||||||||||||||||||
| 57 | service_id => "com.domain.myweatherapp", | ||||||||||||||||||||||||||||||||||||||||||
| 58 | key_id => $key_id, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | key => $private_key?, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | key_file => $private_key_pem?, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | language => $lang_code?, | ||||||||||||||||||||||||||||||||||||||||||
| 62 | timeout => $timeout_sec?, | ||||||||||||||||||||||||||||||||||||||||||
| 63 | expiration => $expire_secs?, | ||||||||||||||||||||||||||||||||||||||||||
| 64 | ua => $lwp_ua?, | ||||||||||||||||||||||||||||||||||||||||||
| 65 | curl => $use_curl? | ||||||||||||||||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | Required parameters: | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | =item * C<team_id> : Your 10-character Apple developer Team Id - it can be located | ||||||||||||||||||||||||||||||||||||||||||
| 73 | on the Apple developer portal. | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =item * C<service_id> : The WeatherKit Service Identifier created on the Apple | ||||||||||||||||||||||||||||||||||||||||||
| 76 | developer portal. Usually a reverse-domain type string is used for this. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =item * C<key_id> : The ID of the WeatherKit key created on the Apple developer portal. | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =item * C<key_file> : The encrypted WeatherKit private key file that you created on | ||||||||||||||||||||||||||||||||||||||||||
| 81 | the Apple developer portal. On the portal you download a PKCS8 format file (.p8), | ||||||||||||||||||||||||||||||||||||||||||
| 82 | which you first need to convert to the PEM format. On a Mac you can convert it simply: | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | openssl pkcs8 -nocrypt -in AuthKey_<key_id>.p8 -out AuthKey_<key_id>.pem | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | =item * C<key> : Instead of the C<.pem> file, you can pass its contents directly | ||||||||||||||||||||||||||||||||||||||||||
| 87 | as a string. | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | Optional parameters: | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | =item * C<language> : Language code. Default: C<en_US>. | ||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =item * C<timeout> : Timeout for requests in secs. Default: C<30>. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | =item * C<ua> : Pass your own L<LWP::UserAgent> to customise the agent string etc. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =item * C<curl> : If true, fall back to using the C<curl> command line program. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | This is useful if you have issues adding http support to L<LWP::UserAgent>, which | ||||||||||||||||||||||||||||||||||||||||||
| 103 | is the default method for the WeatherKit requests. | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | =item * C<expiration> : Token expiration time in seconds. Tokens are cached until | ||||||||||||||||||||||||||||||||||||||||||
| 106 | there are less than 10 minutes left to expiration. Default: C<7200>. | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =head2 C<get> | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | my $report = $wk->get( | ||||||||||||||||||||||||||||||||||||||||||
| 115 | lat => $lat, | ||||||||||||||||||||||||||||||||||||||||||
| 116 | lon => $lon, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | dataSets => $datasets | ||||||||||||||||||||||||||||||||||||||||||
| 118 | %args? | ||||||||||||||||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | my %report = $wk->get( ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||
| 123 | Fetches datasets (weather report, forecast, alert...) for the requested location. | ||||||||||||||||||||||||||||||||||||||||||
| 124 | Returns a string containing the JSON data, except in array context, in which case, | ||||||||||||||||||||||||||||||||||||||||||
| 125 | as a convenience, it will use L<JSON> to decode it directly to a Perl hash. | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | Requires L<LWP::UserAgent>, unless the C<curl> option was set. | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | If the request is not successful, it will C<die> throwing the C<< HTTP::Response->status_line >>. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | =item * C<lat> : Latitude (-90 to 90). | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | =item * C<lon> : Longitude (-18 to 180). | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =item * C<dataSets> : A comma-separated string of the dataset(s) you request. Example | ||||||||||||||||||||||||||||||||||||||||||
| 138 | supported data sets: C<currentWeather, forecastDaily, forecastHourly, forecastNextHour, weatherAlerts>. | ||||||||||||||||||||||||||||||||||||||||||
| 139 | Some data sets might not be available for all locations. Will return empty results | ||||||||||||||||||||||||||||||||||||||||||
| 140 | if parameter is missing. | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =item * C<%args> : See the official API documentation for the supported weather API | ||||||||||||||||||||||||||||||||||||||||||
| 143 | query parameters which you can pass as key/value pairs. | ||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | =head2 C<get_response> | ||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | my $response = $wk->get_response( | ||||||||||||||||||||||||||||||||||||||||||
| 150 | lat => $lat, | ||||||||||||||||||||||||||||||||||||||||||
| 151 | lon => $lon, | ||||||||||||||||||||||||||||||||||||||||||
| 152 | dataSets => $datasets | ||||||||||||||||||||||||||||||||||||||||||
| 153 | %args? | ||||||||||||||||||||||||||||||||||||||||||
| 154 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | Same as C<get> except it returns the full L<HTTP::Response> from the API (so you | ||||||||||||||||||||||||||||||||||||||||||
| 157 | can handle bad requests yourself). | ||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||
| 159 | =head1 CONVENIENCE METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =head2 C<jwt> | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | my $jwt = $wk->jwt( | ||||||||||||||||||||||||||||||||||||||||||
| 164 | iat => $iat?, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | exp => $exp? | ||||||||||||||||||||||||||||||||||||||||||
| 166 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||
| 168 | Returns the JSON Web Token string in case you need it. Will return a cached one | ||||||||||||||||||||||||||||||||||||||||||
| 169 | if it has more than 10 minutes until expiration and you don't explicitly pass an | ||||||||||||||||||||||||||||||||||||||||||
| 170 | C<exp> argument. | ||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||
| 172 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | =item * C<iat> : Specify the token creation timestamp. Default is C<time()>. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||
| 176 | =item * C<exp> : Specify the token expiration timestamp. Passing this parameter | ||||||||||||||||||||||||||||||||||||||||||
| 177 | will force the creation of a new token. Default is C<time()+7200> (or what you | ||||||||||||||||||||||||||||||||||||||||||
| 178 | specified in the constructor). | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 185 | 10 | ▶ | my $class = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 10 | my $self = {}; | |||||||||||||||||||||||||||||||||||||||||
| 188 | 10 | bless($self, $class); | |||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 10 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | 10 | ▶ | croak("10 digit team_id expected.") unless $args{team_id} && length($args{team_id}) == 10; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$args{"team_id"} and length $args{"team_id"} == 10
MC/DC100%2/2
$args{"team_id"} and length $args{"team_id"} == 10
$args{"team_id"}
length $args{"team_id"} == 10
Truth table100%3/3
A and B
A= $args{"team_id"}B= length $args{"team_id"} == 10
| |||||||||||||||||||||||||||||||||||||||||||
| 193 | 8 | $self->{team_id} = $args{team_id}; | |||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | ($self->{$_} = $args{$_} || croak("$_ required.")) | ||||||||||||||||||||||||||||||||||||||||||
| 196 | 8 | ▶ | foreach qw/service_id key_id/; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{$_} || croak("$_ required.")
MC/DC0%0/2
$args{$_} || croak("$_ required.")
$args{$_}
croak("$_ required.")
Truth table33%1/3
A || B
A= $args{$_}B= croak("$_ required.")
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 6 | ▶ | unless ($args{key}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 3 | ▶ | croak("key or key_file required.") unless $args{key_file}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 2 | ▶ | open my $fh, '<', $args{key_file} or die "Can't open file $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | $args{key} = do { local $/; <$fh> }; | |||||||||||||||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||||||||||||||||
| 203 | 4 | $self->{key} = \$args{key}; | |||||||||||||||||||||||||||||||||||||||||
| 204 | 4 | ▶ | $self->{language} = $args{language} || "en_US"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'language'} || 'en_US'
MC/DC100%1/1
$args{'language'} || 'en_US'
$args{'language'}
Truth table100%2/2
A
A= $args{'language'}
| |||||||||||||||||||||||||||||||||||||||||||
| 205 | 4 | ▶ | $self->{timeout} = $args{timeout} || 30; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'timeout'} || 30
MC/DC100%1/1
$args{'timeout'} || 30
$args{'timeout'}
Truth table100%2/2
A
A= $args{'timeout'}
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 4 | ▶ | $self->{expiration} = $args{expiration} || 7200; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args{'expiration'} || 7200
MC/DC100%1/1
$args{'expiration'} || 7200
$args{'expiration'}
Truth table100%2/2
A
A= $args{'expiration'}
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | 4 | $self->{ua} = $args{ua}; | |||||||||||||||||||||||||||||||||||||||||
| 208 | 4 | $self->{curl} = $args{curl}; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 4 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | sub get { | ||||||||||||||||||||||||||||||||||||||||||
| 214 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | 8 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||
| 217 | 8 | my $resp = $self->get_response(%args); | |||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 3 | ▶ | return _output($resp, wantarray) if $self->{curl}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 3 | ▶ | if ($resp->is_success) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 2 | return _output($resp->decoded_content, wantarray); | |||||||||||||||||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||||||||||||||||
| 224 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | die $resp->status_line; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | sub get_response { | ||||||||||||||||||||||||||||||||||||||||||
| 230 | 8 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_response: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 231 | 8 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 232 | 8 | ▶ | $args{language} ||= $self->{language}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{"language"} ||= $self->{"language"}
MC/DC0%0/2
$args{"language"} ||= $self->{"language"}
$args{"language"}
$self->{"language"}
Truth table66%2/3
A ||= B
A= $args{"language"}B= $self->{"language"}
| |||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | croak("lat between -90 and 90 expected") | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 8 | ▶ | unless defined $args{lat} && abs($args{lat}) <= 90; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"lat"} and abs $args{"lat"} <= 90
MC/DC100%2/2
defined $args{"lat"} and abs $args{"lat"} <= 90
defined $args{"lat"}
abs $args{"lat"} <= 90
Truth table100%3/3
A and B
A= defined $args{"lat"}B= abs $args{"lat"} <= 90
| |||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | croak("lon between -180 and 180 expected") | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 6 | ▶ | unless defined $args{lon} && abs($args{lon}) <= 180; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args{"lon"} and abs $args{"lon"} <= 180
MC/DC100%2/2
defined $args{"lon"} and abs $args{"lon"} <= 180
defined $args{"lon"}
abs $args{"lon"} <= 180
Truth table100%3/3
A and B
A= defined $args{"lon"}B= abs $args{"lon"} <= 180
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 4 | my $url = _weather_url(%args); | |||||||||||||||||||||||||||||||||||||||||
| 241 | 4 | my $jwt = $self->jwt; | |||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 3 | ▶ | unless ($self->{curl} || $self->{ua}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"curl"} or $self->{"ua"}
MC/DC50%1/2
$self->{"curl"} or $self->{"ua"}
$self->{"curl"}
$self->{"ua"}
Truth table66%2/3
A or B
A= $self->{"curl"}B= $self->{"ua"}
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | 2 | require LWP::UserAgent; | |||||||||||||||||||||||||||||||||||||||||
| 245 | $self->{ua} = LWP::UserAgent->new( | ||||||||||||||||||||||||||||||||||||||||||
| 246 | agent => "libwww-perl Weather::WeatherKit/$VERSION", | ||||||||||||||||||||||||||||||||||||||||||
| 247 | timeout => $self->{timeout} | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 2 | ); | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | return _fetch($self->{ua}, $url, $jwt); | |||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | sub jwt { | ||||||||||||||||||||||||||||||||||||||||||
| 255 | 9 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
jwt: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 9 | my %args = @_; | |||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | # Return cached one | ||||||||||||||||||||||||||||||||||||||||||
| 259 | return $self->{jwt} | ||||||||||||||||||||||||||||||||||||||||||
| 260 | 9 | ▶ | if !$args{exp} && $self->{jwt_exp} && $self->{jwt_exp} >= time() + 600; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $args{"exp"} and $self->{"jwt_exp"} and $self->{"jwt_exp"} >= time + 600
not $args{"exp"} and $self->{"jwt_exp"}
MC/DC66%2/3
not $args{"exp"} and $self->{"jwt_exp"} and $self->{"jwt_exp"} >= time + 600
not $args{"exp"}
$self->{"jwt_exp"}
$self->{"jwt_exp"} >= time + 600
Truth table75%3/4
not(A) and B and C
A= not $args{"exp"}B= $self->{"jwt_exp"}C= $self->{"jwt_exp"} >= time + 600
| |||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 5 | ▶ | $args{iat} ||= time(); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{"iat"} ||= time
MC/DC0%0/2
$args{"iat"} ||= time
$args{"iat"}
time
Truth table66%2/3
A ||= B
A= $args{"iat"}B= time
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 5 | ▶ | $self->{jwt_exp} = $args{exp} || (time() + $self->{expiration}); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$args{'exp'} || time + $self->{'expiration'}
MC/DC0%0/2
$args{'exp'} || time + $self->{'expiration'}
$args{'exp'}
time + $self->{'expiration'}
Truth table66%2/3
A || B
A= $args{'exp'}B= time + $self->{'expiration'}
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||||||||||
| 265 | my $data = { | ||||||||||||||||||||||||||||||||||||||||||
| 266 | iss => $self->{team_id}, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | sub => $self->{service_id}, | ||||||||||||||||||||||||||||||||||||||||||
| 268 | exp => $self->{jwt_exp}, | ||||||||||||||||||||||||||||||||||||||||||
| 269 | iat => $args{iat} | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 5 | }; | |||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | $self->{jwt} = encode_jwt( | ||||||||||||||||||||||||||||||||||||||||||
| 273 | payload => $data, | ||||||||||||||||||||||||||||||||||||||||||
| 274 | alg => 'ES256', | ||||||||||||||||||||||||||||||||||||||||||
| 275 | key => $self->{key}, | ||||||||||||||||||||||||||||||||||||||||||
| 276 | extra_headers => { | ||||||||||||||||||||||||||||||||||||||||||
| 277 | kid => $self->{key_id}, | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 5 | id => "$self->{team_id}.$self->{service_id}", | |||||||||||||||||||||||||||||||||||||||||
| 279 | typ => "JWT" | ||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 4 | return $self->{jwt}; | |||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | sub _fetch { | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 3 | ▶ | my ($ua, $url, $jwt) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_fetch: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | return | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 3 | ▶ | `curl "$url" -A "Curl Weather::WeatherKit/$VERSION" -s -H 'Authorization: Bearer $jwt'` | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | unless $ua; | ||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||
| 293 | 3 | return $ua->get($url, Authorization => "Bearer $jwt"); | |||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||
| 296 | sub _weather_url { | ||||||||||||||||||||||||||||||||||||||||||
| 297 | 6 | ▶ | my %args = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_weather_url: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 298 | 6 | my $url = | |||||||||||||||||||||||||||||||||||||||||
| 299 | "https://weatherkit.apple.com/api/v1/weather/{language}/{lat}/{lon}"; | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | 6 | $url =~ s/{$_}/delete $args{$_}/e foreach qw/language lat lon/; | |||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 6 | my $params = join("&", map {"$_=$args{$_}"} keys %args); | |||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | 6 | ▶ | $url .= "?$params" if $params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||
| 307 | 6 | return $url; | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | sub _output { | ||||||||||||||||||||||||||||||||||||||||||
| 311 | 2 | ▶ | my $str = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_output: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | 2 | my $json = shift; | |||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | 2 | ▶ | return $str unless $json; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 1 | require JSON; | |||||||||||||||||||||||||||||||||||||||||
| 317 | 1 | return %{JSON::decode_json($str)}; | |||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | Dimitrios Kechagias, C<< <dkechag at cpan.org> >> | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||
| 326 | Please report any bugs or feature requests either on L<GitHub|https://github.com/dkechag/Weather-WeatherKit> (preferred), or on RT (via the email | ||||||||||||||||||||||||||||||||||||||||||
| 327 | C<bug-weather-weatherkit at rt.cpan.org> or L<web interface|https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Weather-WeatherKit>). | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | I will be notified, and then you'll automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =head1 GIT | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | L<https://github.com/dkechag/Weather-WeatherKit> | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | This software is copyright (c) 2023 by Dimitrios Kechagias. | ||||||||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||||||||
| 339 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||
| 340 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||
| 342 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||