| 1 | package Weather::YR::LocationForecast; | ||||||||||||||
| 2 | 3 | ▶ | use Moose; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 3 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 3 | extends 'Weather::YR::Base'; | |||||||||||||
| 6 | |||||||||||||||
| 7 | 3 | ▶ | use DateTime; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 3 | ▶ | use DateTime::Format::ISO8601; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 3 | ▶ | use Mojo::URL; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 3 | ▶ | use Weather::YR::LocationForecast::DataPoint; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 3 | ▶ | use Weather::YR::LocationForecast::Day; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 3 | ▶ | use Weather::YR::Model::Cloudiness; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 3 | ▶ | use Weather::YR::Model::DewPointTemperature; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | 3 | ▶ | use Weather::YR::Model::Fog; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 3 | ▶ | use Weather::YR::Model::Humidity; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 3 | ▶ | use Weather::YR::Model::Precipitation::Symbol; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 3 | ▶ | use Weather::YR::Model::Precipitation; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 3 | ▶ | use Weather::YR::Model::Pressure; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 3 | ▶ | use Weather::YR::Model::Probability::Temperature; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | 3 | ▶ | use Weather::YR::Model::Probability::Wind; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 23 | 3 | ▶ | use Weather::YR::Model::Temperature; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 3 | ▶ | use Weather::YR::Model::WindDirection; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 3 | ▶ | use Weather::YR::Model::WindSpeed; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | =head1 NAME | ||||||||||||||
| 28 | |||||||||||||||
| 29 | Weather::YR::LocationForecast - Object-oriented interface to Yr.no's "location | ||||||||||||||
| 30 | forecast" API. | ||||||||||||||
| 31 | |||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||
| 33 | |||||||||||||||
| 34 | Don't use this class directly. Instead, access it from the L<Weather::YR> class. | ||||||||||||||
| 35 | |||||||||||||||
| 36 | =cut | ||||||||||||||
| 37 | |||||||||||||||
| 38 | 3 | has 'status_code' => ( isa => 'Num', is => 'rw', required => 0 ); | |||||||||||||
| 39 | |||||||||||||||
| 40 | 3 | has 'url' => ( isa => 'Mojo::URL', is => 'ro', lazy_build => 1 ); | |||||||||||||
| 41 | 3 | has 'schema_url' => ( isa => 'Mojo::URL', is => 'ro', lazy_build => 1 ); | |||||||||||||
| 42 | 3 | has 'datapoints' => ( isa => 'ArrayRef[Weather::YR::LocationForecast::DataPoint]', is => 'ro', lazy_build => 1 ); | |||||||||||||
| 43 | 3 | has 'days' => ( isa => 'ArrayRef[Weather::YR::LocationForecast::Day]', is => 'ro', lazy_build => 1 ); | |||||||||||||
| 44 | |||||||||||||||
| 45 | 3 | has 'now' => ( isa => 'Weather::YR::LocationForecast::Day', is => 'ro', lazy_build => 1 ); | |||||||||||||
| 46 | 3 | has 'today' => ( isa => 'Weather::YR::LocationForecast::Day', is => 'ro', lazy_build => 1 ); | |||||||||||||
| 47 | 3 | has 'tomorrow' => ( isa => 'Weather::YR::LocationForecast::Day', is => 'ro', lazy_build => 1 ); | |||||||||||||
| 48 | |||||||||||||||
| 49 | =head1 METHODS | ||||||||||||||
| 50 | |||||||||||||||
| 51 | =head2 url | ||||||||||||||
| 52 | |||||||||||||||
| 53 | Returns the URL to YR.no's location forecast service. This is handy if you | ||||||||||||||
| 54 | want to retrieve the XML from YR.no yourself; | ||||||||||||||
| 55 | |||||||||||||||
| 56 | my $yr = Weather::YR->new( | ||||||||||||||
| 57 | lat => 63.590833, | ||||||||||||||
| 58 | lon => 10.741389, | ||||||||||||||
| 59 | ); | ||||||||||||||
| 60 | |||||||||||||||
| 61 | my $url = $yr->location_forecast->url; | ||||||||||||||
| 62 | |||||||||||||||
| 63 | my $xml = My FancyHttpClient->new->get( $url ); | ||||||||||||||
| 64 | |||||||||||||||
| 65 | my $yr = Weather::YR->new( | ||||||||||||||
| 66 | xml => $xml, | ||||||||||||||
| 67 | tz => DateTime::TimeZone->new( name => 'Europe/Oslo' ), | ||||||||||||||
| 68 | ); | ||||||||||||||
| 69 | |||||||||||||||
| 70 | my $forecast = $yr->location_forecast; | ||||||||||||||
| 71 | |||||||||||||||
| 72 | =cut | ||||||||||||||
| 73 | |||||||||||||||
| 74 | sub _build_url { | ||||||||||||||
| 75 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_url: called CC=1 SCAR=0.0
| |||||||||||||||
| 76 | |||||||||||||||
| 77 | 1 | my $url = $self->service_url->clone; | |||||||||||||
| 78 | 1 | $url->path ( '/weatherapi/locationforecast/2.0/classic' ); | |||||||||||||
| 79 | 1 | $url->query( lat => $self->lat, lon => $self->lon, altitude => $self->msl ); | |||||||||||||
| 80 | |||||||||||||||
| 81 | 1 | return $url; | |||||||||||||
| 82 | # return 'https://api.met.no/weatherapi/locationforecast/2.0/classic?lat=' . $self->lat . ';lon=' . $self->lon . ';altitude=' . $self->msl; | ||||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | =head2 schema_url | ||||||||||||||
| 86 | |||||||||||||||
| 87 | Returns the URL to YR.no' location forecast service XML schema. This is used | ||||||||||||||
| 88 | internally for validating the XML output from YR.no itself. | ||||||||||||||
| 89 | |||||||||||||||
| 90 | =cut | ||||||||||||||
| 91 | |||||||||||||||
| 92 | sub _build_schema_url { | ||||||||||||||
| 93 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_schema_url: called CC=1 SCAR=0.0
| |||||||||||||||
| 94 | |||||||||||||||
| 95 | 1 | my $url = $self->service_url->clone; | |||||||||||||
| 96 | 1 | $url->path( '/weatherapi/locationforecast/2.0/schema' ); | |||||||||||||
| 97 | |||||||||||||||
| 98 | 1 | return $url; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | =head2 datapoints | ||||||||||||||
| 102 | |||||||||||||||
| 103 | Returns an array reference of L<Weather::YR::LocationForecast::DataPoint> instances. | ||||||||||||||
| 104 | |||||||||||||||
| 105 | =cut | ||||||||||||||
| 106 | |||||||||||||||
| 107 | sub _build_datapoints { | ||||||||||||||
| 108 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_datapoints: called CC=7 SCAR=19.6
| |||||||||||||||
| 109 | |||||||||||||||
| 110 | 1 | my @datapoints = (); | |||||||||||||
| 111 | |||||||||||||||
| 112 | 1 | ▶ | if ( my $xml_ref = $self->xml_ref ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | # use Data::Dumper; | ||||||||||||||
| 114 | # print STDOUT Dumper( $xml_ref ); | ||||||||||||||
| 115 | # die; | ||||||||||||||
| 116 | # my $times = $xml_ref->{weatherdata}->{product}->{time} || []; | ||||||||||||||
| 117 | 1 | ▶ | my $times = $xml_ref->{product}->{time} || []; | ||||||||||||
|
Condition50%1/2
$xml_ref->{'product'}{'time'} || []
MC/DC0%0/1
$xml_ref->{'product'}{'time'} || []
$xml_ref->{'product'}{'time'}
Truth table50%1/2
A
A= $xml_ref->{'product'}{'time'}
| |||||||||||||||
| 118 | 1 | my $datapoint = undef; | |||||||||||||
| 119 | |||||||||||||||
| 120 | 1 | foreach my $t ( @{$times} ) { | |||||||||||||
| 121 | 342 | my $from = $self->date_to_datetime( $t->{from} ); | |||||||||||||
| 122 | 342 | my $to = $self->date_to_datetime( $t->{to } ); | |||||||||||||
| 123 | |||||||||||||||
| 124 | 342 | ▶ | if ( $t->{location}->{temperature} ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 125 | 84 | my $loc = $t->{location}; | |||||||||||||
| 126 | |||||||||||||||
| 127 | 84 | ▶ | if ( defined $datapoint ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 83 | push( @datapoints, $datapoint ); | |||||||||||||
| 129 | 83 | $datapoint = undef; | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | |||||||||||||||
| 132 | $datapoint = Weather::YR::LocationForecast::DataPoint->new( | ||||||||||||||
| 133 | from => $from, | ||||||||||||||
| 134 | to => $to, | ||||||||||||||
| 135 | lang => $self->lang, | ||||||||||||||
| 136 | type => $t->{datatype}, | ||||||||||||||
| 137 | |||||||||||||||
| 138 | temperature => Weather::YR::Model::Temperature->new( | ||||||||||||||
| 139 | from => $from, | ||||||||||||||
| 140 | to => $to, | ||||||||||||||
| 141 | lang => $self->lang, | ||||||||||||||
| 142 | celsius => $loc->{temperature}->{value}, | ||||||||||||||
| 143 | ), | ||||||||||||||
| 144 | |||||||||||||||
| 145 | wind_direction => Weather::YR::Model::WindDirection->new( | ||||||||||||||
| 146 | from => $from, | ||||||||||||||
| 147 | to => $to, | ||||||||||||||
| 148 | lang => $self->lang, | ||||||||||||||
| 149 | degrees => $loc->{windDirection}->{deg}, | ||||||||||||||
| 150 | name => $loc->{windDirection}->{name}, | ||||||||||||||
| 151 | ), | ||||||||||||||
| 152 | |||||||||||||||
| 153 | wind_speed => Weather::YR::Model::WindSpeed->new( | ||||||||||||||
| 154 | from => $from, | ||||||||||||||
| 155 | to => $to, | ||||||||||||||
| 156 | lang => $self->lang, | ||||||||||||||
| 157 | mps => $loc->{windSpeed}->{mps}, | ||||||||||||||
| 158 | beaufort => $loc->{windSpeed}->{beaufort}, | ||||||||||||||
| 159 | name => $loc->{windSpeed}->{name}, | ||||||||||||||
| 160 | ), | ||||||||||||||
| 161 | |||||||||||||||
| 162 | humidity => Weather::YR::Model::Humidity->new( | ||||||||||||||
| 163 | from => $from, | ||||||||||||||
| 164 | to => $to, | ||||||||||||||
| 165 | lang => $self->lang, | ||||||||||||||
| 166 | percent => $loc->{humidity}->{value}, | ||||||||||||||
| 167 | ), | ||||||||||||||
| 168 | |||||||||||||||
| 169 | pressure => Weather::YR::Model::Pressure->new( | ||||||||||||||
| 170 | from => $from, | ||||||||||||||
| 171 | to => $to, | ||||||||||||||
| 172 | lang => $self->lang, | ||||||||||||||
| 173 | hPa => $loc->{pressure}->{value}, | ||||||||||||||
| 174 | ), | ||||||||||||||
| 175 | |||||||||||||||
| 176 | cloudiness => Weather::YR::Model::Cloudiness->new( | ||||||||||||||
| 177 | from => $from, | ||||||||||||||
| 178 | to => $to, | ||||||||||||||
| 179 | lang => $self->lang, | ||||||||||||||
| 180 | percent => $loc->{cloudiness}->{percent}, | ||||||||||||||
| 181 | ), | ||||||||||||||
| 182 | |||||||||||||||
| 183 | fog => Weather::YR::Model::Fog->new( | ||||||||||||||
| 184 | from => $from, | ||||||||||||||
| 185 | to => $to, | ||||||||||||||
| 186 | lang => $self->lang, | ||||||||||||||
| 187 | percent => $loc->{fog}->{percent}, | ||||||||||||||
| 188 | ), | ||||||||||||||
| 189 | |||||||||||||||
| 190 | dew_point_temperature => Weather::YR::Model::DewPointTemperature->new( | ||||||||||||||
| 191 | from => $from, | ||||||||||||||
| 192 | to => $to, | ||||||||||||||
| 193 | lang => $self->lang, | ||||||||||||||
| 194 | celsius => $loc->{dewpointTemperature}->{value}, | ||||||||||||||
| 195 | ), | ||||||||||||||
| 196 | |||||||||||||||
| 197 | temperature_probability => Weather::YR::Model::Probability::Temperature->new( | ||||||||||||||
| 198 | from => $from, | ||||||||||||||
| 199 | to => $to, | ||||||||||||||
| 200 | lang => $self->lang, | ||||||||||||||
| 201 | value => $loc->{temperatureProbability}->{value}, | ||||||||||||||
| 202 | ), | ||||||||||||||
| 203 | |||||||||||||||
| 204 | wind_probability => Weather::YR::Model::Probability::Wind->new( | ||||||||||||||
| 205 | from => $from, | ||||||||||||||
| 206 | to => $to, | ||||||||||||||
| 207 | lang => $self->lang, | ||||||||||||||
| 208 | value => $loc->{windProbability}->{value}, | ||||||||||||||
| 209 | 84 | ), | |||||||||||||
| 210 | ); | ||||||||||||||
| 211 | } | ||||||||||||||
| 212 | elsif ( my $p = $t->{location}->{precipitation} ) { | ||||||||||||||
| 213 | my $precipitation = Weather::YR::Model::Precipitation->new( | ||||||||||||||
| 214 | from => $from, | ||||||||||||||
| 215 | to => $to, | ||||||||||||||
| 216 | lang => $self->lang, | ||||||||||||||
| 217 | value => $p->{value}, | ||||||||||||||
| 218 | min => $p->{minvalue}, | ||||||||||||||
| 219 | max => $p->{maxvalue}, | ||||||||||||||
| 220 | symbol => Weather::YR::Model::Precipitation::Symbol->new( | ||||||||||||||
| 221 | from => $from, | ||||||||||||||
| 222 | to => $to, | ||||||||||||||
| 223 | lang => $self->lang, | ||||||||||||||
| 224 | id => $t->{location}->{symbol}->{id}, | ||||||||||||||
| 225 | number => $t->{location}->{symbol}->{number}, | ||||||||||||||
| 226 | 258 | ), | |||||||||||||
| 227 | ); | ||||||||||||||
| 228 | |||||||||||||||
| 229 | 258 | $datapoint->add_precipitation( $precipitation ); | |||||||||||||
| 230 | } | ||||||||||||||
| 231 | } | ||||||||||||||
| 232 | } | ||||||||||||||
| 233 | else { | ||||||||||||||
| 234 | 0 | warn "No XML to generate forecast from!"; | |||||||||||||
| 235 | } | ||||||||||||||
| 236 | |||||||||||||||
| 237 | 1 | return \@datapoints; | |||||||||||||
| 238 | } | ||||||||||||||
| 239 | |||||||||||||||
| 240 | =head2 days | ||||||||||||||
| 241 | |||||||||||||||
| 242 | Returns an array reference of L<Weather::YR::LocationForecast::Day> instances. | ||||||||||||||
| 243 | |||||||||||||||
| 244 | =cut | ||||||||||||||
| 245 | |||||||||||||||
| 246 | sub _build_days { | ||||||||||||||
| 247 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_days: called CC=3 SCAR=11.0
| |||||||||||||||
| 248 | |||||||||||||||
| 249 | 1 | my %day_datapoints = (); | |||||||||||||
| 250 | |||||||||||||||
| 251 | 1 | foreach my $datapoint ( @{$self->datapoints} ) { | |||||||||||||
| 252 | 83 | push( @{$day_datapoints{$datapoint->{from}->ymd}}, $datapoint ); | |||||||||||||
| 253 | } | ||||||||||||||
| 254 | |||||||||||||||
| 255 | 1 | my @days = (); | |||||||||||||
| 256 | |||||||||||||||
| 257 | 1 | foreach my $date ( sort keys %day_datapoints ) { | |||||||||||||
| 258 | my $day = Weather::YR::LocationForecast::Day->new( | ||||||||||||||
| 259 | date => DateTime::Format::ISO8601->parse_datetime( $date ), | ||||||||||||||
| 260 | 10 | datapoints => $day_datapoints{ $date }, | |||||||||||||
| 261 | ); | ||||||||||||||
| 262 | |||||||||||||||
| 263 | 10 | push( @days, $day ); | |||||||||||||
| 264 | } | ||||||||||||||
| 265 | |||||||||||||||
| 266 | 1 | return \@days; | |||||||||||||
| 267 | } | ||||||||||||||
| 268 | |||||||||||||||
| 269 | =head2 now | ||||||||||||||
| 270 | |||||||||||||||
| 271 | Returns a L<Weather::YR::LocationForecast::Day> instance, representing the | ||||||||||||||
| 272 | closest forecast in time. | ||||||||||||||
| 273 | |||||||||||||||
| 274 | =cut | ||||||||||||||
| 275 | |||||||||||||||
| 276 | sub _build_now { | ||||||||||||||
| 277 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_now: not called CC=4 SCAR=30.0
| |||||||||||||||
| 278 | |||||||||||||||
| 279 | 0 | my $now = DateTime->now( time_zone => 'UTC' ); | |||||||||||||
| 280 | 0 | my $closest_dp = undef; | |||||||||||||
| 281 | |||||||||||||||
| 282 | 0 | foreach my $dp ( @{$self->today->datapoints} ) { | |||||||||||||
| 283 | 0 | ▶ | unless ( defined $closest_dp ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 284 | 0 | $closest_dp = $dp; | |||||||||||||
| 285 | 0 | next; | |||||||||||||
| 286 | } | ||||||||||||||
| 287 | |||||||||||||||
| 288 | 0 | my $diff_from_now = abs( $dp->from->epoch - $now->epoch ); | |||||||||||||
| 289 | |||||||||||||||
| 290 | 0 | ▶ | if ( $diff_from_now < ( abs($closest_dp->from->epoch - $now->epoch) ) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 291 | 0 | $closest_dp = $dp; | |||||||||||||
| 292 | } | ||||||||||||||
| 293 | } | ||||||||||||||
| 294 | |||||||||||||||
| 295 | 0 | return Weather::YR::LocationForecast::Day->new( | |||||||||||||
| 296 | date => $closest_dp->from, | ||||||||||||||
| 297 | datapoints => [ $closest_dp ], | ||||||||||||||
| 298 | ); | ||||||||||||||
| 299 | } | ||||||||||||||
| 300 | |||||||||||||||
| 301 | =head2 today | ||||||||||||||
| 302 | |||||||||||||||
| 303 | Returns a L<Weather::YR::LocationForecast::Day> instance, representing today's | ||||||||||||||
| 304 | weather. | ||||||||||||||
| 305 | |||||||||||||||
| 306 | =cut | ||||||||||||||
| 307 | |||||||||||||||
| 308 | sub _build_today { | ||||||||||||||
| 309 | 1 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_today: called CC=1 SCAR=0.0
| |||||||||||||||
| 310 | |||||||||||||||
| 311 | 1 | return $self->days->[0]; | |||||||||||||
| 312 | } | ||||||||||||||
| 313 | |||||||||||||||
| 314 | =head2 tomorrow | ||||||||||||||
| 315 | |||||||||||||||
| 316 | Returns a L<Weather::YR::LocationForecast::Day> instance, representing | ||||||||||||||
| 317 | tomorrow's weather. | ||||||||||||||
| 318 | |||||||||||||||
| 319 | =cut | ||||||||||||||
| 320 | |||||||||||||||
| 321 | sub _build_tomorrow { | ||||||||||||||
| 322 | 0 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
_build_tomorrow: not called CC=1 SCAR=6.9
| |||||||||||||||
| 323 | |||||||||||||||
| 324 | 0 | return $self->days->[1]; | |||||||||||||
| 325 | } | ||||||||||||||
| 326 | |||||||||||||||
| 327 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||
| 328 | |||||||||||||||
| 329 | 3 | 1; | |||||||||||||