YR/LocationForecast.pm

filter: Statementstmt 87.8% 108 / 123 Branchbran 50.0% 6 / 12 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 92.3% 24 / 26 Podpod n/a totaltotal 84.7% 139 / 164 CC 12 SCAR 14.8
CC
12
Cov
84%
_build_now
20.0
_build_datapoints
7.1
_build_days
3.0
CRAP 4.4
« YR/LocationForecast/Day.pm ↑ Summary YR/Model/Clouds.pm »
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.5908,
58 lon => 10.7414,
59 );
60  
61 my $url = $yr->location_forecast->url;
62  
63 my $xml = MyFancyHttpClient->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 1 ▶sub _build_url {
Subroutine _build_url: called CC=1 SCAR=0.0
75 1 my $self = shift;
76  
77 1 my $url = $self->service_url->clone;
78 1 $url->path ( '/weatherapi/locationforecast/2.0/classic' );
79 1 $url->query(
80 lat => $self->lat,
81 lon => $self->lon,
82 altitude => $self->msl,
83 );
84  
85 1 return $url;
86 }
87  
88 =head2 schema_url
89  
90 Returns the URL to YR.no' location forecast service XML schema. This is used
91 internally for validating the XML output from YR.no itself.
92  
93 =cut
94  
95 1 ▶sub _build_schema_url {
Subroutine _build_schema_url: called CC=1 SCAR=0.0
96 1 my $self = shift;
97  
98 1 my $url = $self->service_url->clone;
99 1 $url->path( '/weatherapi/locationforecast/2.0/schema' );
100  
101 1 return $url;
102 }
103  
104 =head2 datapoints
105  
106 Returns an array reference of L<Weather::YR::LocationForecast::DataPoint> instances.
107  
108 =cut
109  
110 1 ▶sub _build_datapoints {
Subroutine _build_datapoints: called CC=7 SCAR=19.6
111 1 my $self = shift;
112  
113 1 my @datapoints = ();
114  
115 1 ▶ if ( my $xml_ref = $self->xml_ref ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xml_ref = $self->xml_ref) { } 1 0 1
116 1 ▶ my $times = $xml_ref->{product}->{time} || [];
Condition50%1/2
$xml_ref->{'product'}{'time'} || []
l!l
10
MC/DC0%0/1
$xml_ref->{'product'}{'time'} || []
$xml_ref->{'product'}{'time'}
Truth table50%1/2
A
A= $xml_ref->{'product'}{'time'}
Aresult
11
00
117 1 my $datapoint = undef;
118  
119 1 foreach my $t ( @{$times} ) {
120 342 my $from = $self->date_to_datetime( $t->{from} );
121 342 my $to = $self->date_to_datetime( $t->{to } );
122  
123 342 ▶ if ( $t->{location}->{temperature} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($t->{'location'}{'temperature'}) { } 84 258 342
elsif (my $p = $t->{'location'}{'precipitation'}) { } 258 0 258
124 84 my $loc = $t->{location};
125  
126 84 ▶ if ( defined $datapoint ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $datapoint) 83 1 84
127 83 push( @datapoints, $datapoint );
128 83 $datapoint = undef;
129 }
130  
131 $datapoint = Weather::YR::LocationForecast::DataPoint->new(
132 from => $from,
133 to => $to,
134 lang => $self->lang,
135 type => $t->{datatype},
136  
137 temperature => Weather::YR::Model::Temperature->new(
138 from => $from,
139 to => $to,
140 lang => $self->lang,
141 celsius => $loc->{temperature}->{value},
142 ),
143  
144 wind_direction => Weather::YR::Model::WindDirection->new(
145 from => $from,
146 to => $to,
147 lang => $self->lang,
148 degrees => $loc->{windDirection}->{deg},
149 name => $loc->{windDirection}->{name},
150 ),
151  
152 wind_speed => Weather::YR::Model::WindSpeed->new(
153 from => $from,
154 to => $to,
155 lang => $self->lang,
156 mps => $loc->{windSpeed}->{mps},
157 beaufort => $loc->{windSpeed}->{beaufort},
158 name => $loc->{windSpeed}->{name},
159 ),
160  
161 humidity => Weather::YR::Model::Humidity->new(
162 from => $from,
163 to => $to,
164 lang => $self->lang,
165 percent => $loc->{humidity}->{value},
166 ),
167  
168 pressure => Weather::YR::Model::Pressure->new(
169 from => $from,
170 to => $to,
171 lang => $self->lang,
172 hPa => $loc->{pressure}->{value},
173 ),
174  
175 cloudiness => Weather::YR::Model::Cloudiness->new(
176 from => $from,
177 to => $to,
178 lang => $self->lang,
179 percent => $loc->{cloudiness}->{percent},
180 ),
181  
182 fog => Weather::YR::Model::Fog->new(
183 from => $from,
184 to => $to,
185 lang => $self->lang,
186 percent => $loc->{fog}->{percent},
187 ),
188  
189 dew_point_temperature => Weather::YR::Model::DewPointTemperature->new(
190 from => $from,
191 to => $to,
192 lang => $self->lang,
193 celsius => $loc->{dewpointTemperature}->{value},
194 ),
195  
196 temperature_probability => Weather::YR::Model::Probability::Temperature->new(
197 from => $from,
198 to => $to,
199 lang => $self->lang,
200 value => $loc->{temperatureProbability}->{value},
201 ),
202  
203 wind_probability => Weather::YR::Model::Probability::Wind->new(
204 from => $from,
205 to => $to,
206 lang => $self->lang,
207 value => $loc->{windProbability}->{value},
208 84 ),
209 );
210 }
211 elsif ( my $p = $t->{location}->{precipitation} ) {
212 my $precipitation = Weather::YR::Model::Precipitation->new(
213 from => $from,
214 to => $to,
215 lang => $self->lang,
216 value => $p->{value},
217 min => $p->{minvalue},
218 max => $p->{maxvalue},
219 symbol => Weather::YR::Model::Precipitation::Symbol->new(
220 from => $from,
221 to => $to,
222 lang => $self->lang,
223 id => $t->{location}->{symbol}->{id},
224 number => $t->{location}->{symbol}->{number},
225 258 ),
226 );
227  
228 258 $datapoint->add_precipitation( $precipitation );
229 }
230 }
231 }
232 else {
233 0 warn "No XML to generate forecast from!";
234 }
235  
236 1 return \@datapoints;
237 }
238  
239 =head2 days
240  
241 Returns an array reference of L<Weather::YR::LocationForecast::Day> instances.
242  
243 =cut
244  
245 1 ▶sub _build_days {
Subroutine _build_days: called CC=3 SCAR=11.0
246 1 my $self = shift;
247  
248 1 my %day_datapoints = ();
249  
250 1 foreach my $datapoint ( @{$self->datapoints} ) {
251 83 push( @{$day_datapoints{$datapoint->{from}->ymd}}, $datapoint );
252 }
253  
254 1 my @days = ();
255  
256 1 foreach my $date ( sort keys %day_datapoints ) {
257 my $day = Weather::YR::LocationForecast::Day->new(
258 date => DateTime::Format::ISO8601->parse_datetime( $date ),
259 10 datapoints => $day_datapoints{ $date },
260 );
261  
262 10 push( @days, $day );
263 }
264  
265 1 return \@days;
266 }
267  
268 =head2 now
269  
270 Returns a L<Weather::YR::LocationForecast::Day> instance, representing the
271 closest forecast in time.
272  
273 =cut
274  
275 0 ▶sub _build_now {
Subroutine _build_now: not called CC=4 SCAR=30.0
276 0 my $self = shift;
277  
278 0 my $now = DateTime->now( time_zone => 'UTC' );
279 0 my $closest_dp = undef;
280  
281 0 foreach my $dp ( @{$self->today->datapoints} ) {
282 0 ▶ unless ( defined $closest_dp ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $closest_dp) 0 0 0
283 0 $closest_dp = $dp;
284 0 next;
285 }
286  
287 0 my $diff_from_now = abs( $dp->from->epoch - $now->epoch );
288  
289 0 ▶ if ( $diff_from_now < ( abs($closest_dp->from->epoch - $now->epoch) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff_from_now < abs $closest_dp->from->epoch - $now->epoch) 0 0 0
290 0 $closest_dp = $dp;
291 }
292 }
293  
294 0 return Weather::YR::LocationForecast::Day->new(
295 date => $closest_dp->from,
296 datapoints => [ $closest_dp ],
297 );
298 }
299  
300 =head2 today
301  
302 Returns a L<Weather::YR::LocationForecast::Day> instance, representing today's
303 weather.
304  
305 =cut
306  
307 1 ▶sub _build_today {
Subroutine _build_today: called CC=1 SCAR=0.0
308 1 my $self = shift;
309  
310 1 return $self->days->[0];
311 }
312  
313 =head2 tomorrow
314  
315 Returns a L<Weather::YR::LocationForecast::Day> instance, representing
316 tomorrow's weather.
317  
318 =cut
319  
320 0 ▶sub _build_tomorrow {
Subroutine _build_tomorrow: not called CC=1 SCAR=6.9
321 0 my $self = shift;
322  
323 0 return $self->days->[1];
324 }
325  
326 3 __PACKAGE__->meta->make_immutable;
327  
328 3 1;
« YR/LocationForecast/Day.pm ↑ Summary YR/Model/Clouds.pm »