NDFDgenByDay.pm

filter: Statementstmt 100% 87 / 87 Branchbran 58.3% 14 / 24 Conditioncond 41.9% 13 / 31 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 100% 19 / 19 Podpod 100% 13 / 13 totaltotal 76.2% 148 / 194 CC 24 SCAR 23.4
CC
24
Cov
80%
get_forecast_xml
25.0
BUILD
8.0
set_number_of_days
4.0
CRAP 10.4
« NDFDgen.pm ↑ Summary
1 package Weather::NWS::NDFDgenByDay;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use SOAP::DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 ▶use Class::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =pod
14  
15 =head1 NAME
16  
17 Weather::NWS::NDFDgenByDay - Object interface to the NWS NDFDgenByDay Web Service.
18  
19 =head1 VERSION
20  
21 Version 0.02
22  
23 =cut
24  
25 3 our $VERSION = '0.02';
26  
27 =pod
28  
29 =head1 SYNOPSIS
30  
31 use Weather::NWS::NDFDgenByDay;
32  
33 my $NDFDgenByDay = Weather::NWS::NDFDgenByDay->new();
34  
35 my $NDFDgenByDay = Weather::NWS::NDFDgenByDay->new(
36 'Format' => 'Day',
37 'Latitude' => 42,
38 'Longitude' => -88,
39 );
40
41 my $latitude = 42;
42 $NDFDgenByDay->set_latitude($latitude);
43 $latitude = $NDFDgenByDay->get_latitude();
44
45 my $longitude = -88;
46 $NDFDgenByDay->set_longitude($longitude);
47 $longitude = $NDFDgenByDay->get_longitude();
48  
49 my $format = 'Day';
50 $NDFDgenByDay->set_format($format);
51 $format = $NDFDgenByDay->get_format();
52
53 my $start_date = scalar localtime;
54 $NDFDgenByDay->set_start_date($start_date);
55 $start_date = $NDFDgenByDay->get_start_date();
56
57 my $num_days = 3;
58 $NDFDgenByDay->set_set_number_of_days($num_days);
59 $num_days = $NDFDgenByDay->get_number_of_days();
60
61 my $xml = $NDFDgenByDay->get_forecast_xml();
62
63 my $xml = $NDFDgenByDay->get_forecast_xml(
64 'Format' => 'Day',
65 'Latitude' => 42,
66 'Longitude' => -88,
67 );
68  
69 my @formats = $NDFDgen->get_available_formats();
70  
71 =cut
72  
73 =pod
74  
75 =head1 NDFDgenByDay
76  
77 =cut
78  
79 3 Readonly my $SERVICE =>
80 'http://www.weather.gov/forecasts/xml/DWMLgen/wsdl/ndfdXML.wsdl';
81  
82 3 Readonly my %NAME_TO_ARGUMENT => (
83 'Latitude' => 'latitude',
84 'Longitude' => 'longitude',
85 'Start Date' => 'startDate',
86 'Number of Days' => 'numDays',
87 'Format' => 'format',
88 );
89 3 Readonly my @ARGUMENTS => keys %NAME_TO_ARGUMENT;
90  
91 3 Readonly my %NAME_TO_FORMAT => (
92 'Day' => '24 hourly',
93 'Half-Day' => '12 hourly',
94 );
95 3 Readonly my @FORMATS => keys %NAME_TO_FORMAT;
96  
97 3 Readonly my $DEFAULT_FORMAT => 'Day';
98 3 Readonly my $DEFAULT_START_DATE => ConvertDate( scalar localtime );
99 3 Readonly my $DEFAULT_NUM_DAYS => 1;
100  
101 =pod
102  
103 =head1 METHODS
104  
105 =cut
106  
107 {
108 3 my %forecaster : ATTR;
109 3 my %forecast_xml : ATTR;
110 3 my %default_num_days : ATTR;
111 3 my %default_format : ATTR;
112 3 my %default_start_date : ATTR;
113 3 my %default_latitude : ATTR;
114 3 my %default_longitude : ATTR;
115  
116 =pod
117  
118 =head2 BUILD (new)
119  
120 Constructor for new NDFDgenByDay objects. If called with no parameters, it will
121 return a new object initialized with the 'Day' format, the current date as the
122 start date, and the default number of days equal to one. All other parameters
123 are left unintialized. Values can be provided for 'Latitude', 'Longitude',
124 'Format', 'Start Time', and 'Number of Days'.
125  
126 =cut
127  
128 1 ▶ sub BUILD {
Subroutine BUILD: called CC=6 SCAR=20.8
Pod documented
129 1 my ( $self, $ident, $arg_ref ) = @_;
130  
131 1 my %args = %{$arg_ref};
132  
133 1 ▶ $self->set_latitude( $args{'Latitude'} || undef );
Condition50%1/2
$args{'Latitude'} || undef
l!l
01
MC/DC0%0/1
$args{'Latitude'} || undef
$args{'Latitude'}
Truth table50%1/2
A
A= $args{'Latitude'}
Aresult
11
00
134 1 ▶ $self->set_longitude( $args{'Longitude'} || undef );
Condition50%1/2
$args{'Longitude'} || undef
l!l
01
MC/DC0%0/1
$args{'Longitude'} || undef
$args{'Longitude'}
Truth table50%1/2
A
A= $args{'Longitude'}
Aresult
11
00
135 1 ▶ $self->set_start_date( $args{'Start Date'} || $DEFAULT_START_DATE );
Condition33%1/3
$args{'Start Date'} || $DEFAULT_START_DATE
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'Start Date'} || $DEFAULT_START_DATE
$args{'Start Date'} $DEFAULT_START_DATE
Truth table33%1/3
A || B
A= $args{'Start Date'}B= $DEFAULT_START_DATE
ABresult
1X1
011
000
136 1 ▶ $self->set_number_of_days( $args{'Number of Days'}
Condition33%1/3
$args{'Number of Days'} || $DEFAULT_NUM_DAYS
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'Number of Days'} || $DEFAULT_NUM_DAYS
$args{'Number of Days'} $DEFAULT_NUM_DAYS
Truth table33%1/3
A || B
A= $args{'Number of Days'}B= $DEFAULT_NUM_DAYS
ABresult
1X1
011
000
137 || $DEFAULT_NUM_DAYS );
138 1 ▶ $self->set_format( $args{'Format'} || $DEFAULT_FORMAT );
Condition33%1/3
$args{'Format'} || $DEFAULT_FORMAT
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'Format'} || $DEFAULT_FORMAT
$args{'Format'} $DEFAULT_FORMAT
Truth table33%1/3
A || B
A= $args{'Format'}B= $DEFAULT_FORMAT
ABresult
1X1
011
000
139 }
140  
141 =pod
142  
143 =head2 set_latitude
144  
145 Sets the latitude for the object. This is a decimal value.
146  
147 =cut
148  
149 2 ▶ sub set_latitude {
Subroutine set_latitude: called CC=1 SCAR=0.0
Pod documented
150 2 my ( $self, $new_latitude ) = @_;
151 2 return $default_latitude{ ident $self} = $new_latitude;
152 }
153  
154 =pod
155  
156 =head2 get_latitude
157  
158 Returns the latitude stored in the object.
159  
160 =cut
161  
162 2 ▶ sub get_latitude {
Subroutine get_latitude: called CC=1 SCAR=0.0
Pod documented
163 2 my ($self) = @_;
164 2 return $default_latitude{ ident $self};
165 }
166  
167 =pod
168  
169 =head2 set_longitude
170  
171 Sets the longitude for the object. This is a decimal value.
172  
173 =cut
174  
175 2 ▶ sub set_longitude {
Subroutine set_longitude: called CC=1 SCAR=0.0
Pod documented
176 2 my ( $self, $new_longitude ) = @_;
177 2 return $default_longitude{ ident $self} = $new_longitude;
178 }
179  
180 =pod
181  
182 =head2 get_longitude
183  
184 Returns the longitude stored in the object.
185  
186 =cut
187  
188 2 ▶ sub get_longitude {
Subroutine get_longitude: called CC=1 SCAR=0.0
Pod documented
189 2 my ($self) = @_;
190 2 return $default_longitude{ ident $self};
191 }
192  
193 =pod
194  
195 =head2 set_format
196  
197 Sets the format for the object. This is either 'Day' or 'Half-Day'.
198  
199 =cut
200  
201 4 ▶ sub set_format {
Subroutine set_format: called CC=2 SCAR=6.9
Pod documented
202 4 my ( $self, $new_format ) = @_;
203  
204 die("Invalid format ($new_format)")
205 4 ▶ unless ( grep { /^${new_format}$/ } @FORMATS );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^$new_format$/;} @FORMATS 1 3 4
206  
207 3 return $default_format{ ident $self} = $new_format;
208 }
209  
210 =pod
211  
212 =head2 get_format
213  
214 Returns the format stored in the object.
215  
216 =cut
217  
218 2 ▶ sub get_format {
Subroutine get_format: called CC=1 SCAR=0.0
Pod documented
219 2 my ($self) = @_;
220 2 return $default_format{ ident $self};
221 }
222  
223 =pod
224  
225 =head2 set_start_date
226  
227 Sets the start date for the object.
228  
229 =cut
230  
231 2 ▶ sub set_start_date {
Subroutine set_start_date: called CC=2 SCAR=7.1
Pod documented
232 2 my ( $self, $new_start_date ) = @_;
233  
234 2 ▶ return unless $new_start_date;
Branch: 1 branch
BranchTrueFalseTotal
unless $new_start_date 0 2 2
235  
236 2 return $default_start_date{ ident $self} = ConvertDate($new_start_date);
237 }
238  
239 =pod
240  
241 =head2 get_start_date
242  
243 Gets the start date stored in the object.
244  
245 =cut
246  
247 1 ▶ sub get_start_date {
Subroutine get_start_date: called CC=1 SCAR=0.0
Pod documented
248 1 my ($self) = @_;
249 1 return $default_start_date{ ident $self};
250 }
251  
252 =pod
253  
254 =head2 set_number_of_days
255  
256 Sets the number of days for the object. This is an integer value
257 between 1 and 7.
258  
259 =cut
260  
261 10 ▶ sub set_number_of_days {
Subroutine set_number_of_days: called CC=4 SCAR=13.9
Pod documented
262 10 my ( $self, $new_number_of_days ) = @_;
263  
264 10 ▶ die("Non-numeric number of days ($new_number_of_days)")
Branch: 1 branch
BranchTrueFalseTotal
if $new_number_of_days =~ /\D/ 0 10 10
265 if ( $new_number_of_days =~ /\D/ );
266  
267 10 ▶ die("Only 1-7 days are allowed")
Branch: 1 branch
BranchTrueFalseTotal
if $new_number_of_days < 1 or $new_number_of_days > 7 2 8 10
Condition100%3/3
$new_number_of_days < 1 or $new_number_of_days > 7
l!l&&r!l&&!r
118
MC/DC100%2/2
$new_number_of_days < 1 or $new_number_of_days > 7
$new_number_of_days < 1 $new_number_of_days > 7
Truth table100%3/3
A or B
A= $new_number_of_days < 1B= $new_number_of_days > 7
ABresult
1X1
011
000
268 if ( $new_number_of_days < 1 || $new_number_of_days > 7 );
269  
270 8 return $default_num_days{ ident $self} = $new_number_of_days;
271 }
272  
273 =pod
274  
275 =head2 get_number_of_days
276  
277 Returns the number_of_days stored in the object.
278  
279 =cut
280  
281 7 ▶ sub get_number_of_days {
Subroutine get_number_of_days: called CC=1 SCAR=0.0
Pod documented
282 7 my ($self) = @_;
283 7 return $default_num_days{ ident $self};
284 }
285  
286 =pod
287  
288 =head2 get_available_formats
289  
290 Return a list of all formats available through this service.
291  
292 =cut
293  
294 1 ▶ sub get_available_formats {
Subroutine get_available_formats: called CC=1 SCAR=0.0
Pod documented
295 1 my ($self) = @_;
296 1 return @FORMATS;
297 }
298  
299 =pod
300  
301 =head2 get_forecast_xml
302  
303 Return the NWS NDFD XML as described in
304 L<http://products.weather.gov/PDD/Extensible_Markup_Language.pdf>. The data
305 returned depends on the state of the NDFDgenByDay object at the date of the call
306 to this method. Any parameters can be overridden by being passed in as
307 arguments to this method.
308  
309 =cut
310  
311 1 ▶ sub get_forecast_xml {
Subroutine get_forecast_xml: called CC=14 SCAR=32.2
Pod documented
312 1 my ( $self, %args ) = @_;
313  
314 1 my ($ident) = ident $self;
315  
316 1 my ( $latitude, $longitude, $format, $start_date, $num_days );
317  
318 die("Latitude required")
319 1 ▶ unless $latitude = $args{'Latitude'} || $default_latitude{$ident};
Branch: 1 branch
BranchTrueFalseTotal
unless $latitude = $args{"Latitude"} || $default_latitude{$ident} 0 1 1
Condition33%1/3
$args{'Latitude'} || $default_latitude{$ident}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'Latitude'} || $default_latitude{$ident}
$args{'Latitude'} $default_latitude{$ident}
Truth table33%1/3
A || B
A= $args{'Latitude'}B= $default_latitude{$ident}
ABresult
1X1
011
000
320  
321 die("Longitude required")
322 1 ▶ unless $longitude = $args{'Longitude'} || $default_longitude{$ident};
Branch: 1 branch
BranchTrueFalseTotal
unless $longitude = $args{"Longitude"} || $default_longitude{$ident} 0 1 1
Condition33%1/3
$args{'Longitude'} || $default_longitude{$ident}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'Longitude'} || $default_longitude{$ident}
$args{'Longitude'} $default_longitude{$ident}
Truth table33%1/3
A || B
A= $args{'Longitude'}B= $default_longitude{$ident}
ABresult
1X1
011
000
323  
324 die("Format required")
325 1 ▶ unless $format = $args{'Format'} || $default_format{$ident};
Branch: 1 branch
BranchTrueFalseTotal
unless $format = $args{"Format"} || $default_format{$ident} 0 1 1
Condition33%1/3
$args{'Format'} || $default_format{$ident}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'Format'} || $default_format{$ident}
$args{'Format'} $default_format{$ident}
Truth table33%1/3
A || B
A= $args{'Format'}B= $default_format{$ident}
ABresult
1X1
011
000
326  
327 die("Start date required")
328 unless $start_date = $args{'Start Time'}
329 1 ▶ || $default_start_date{$ident};
Branch: 1 branch
BranchTrueFalseTotal
unless $start_date = $args{"Start Time"} || $default_start_date{$ident} 0 1 1
Condition33%1/3
$args{'Start Time'} || $default_start_date{$ident}
l!l&&r!l&&!r
010
MC/DC0%0/2
$args{'Start Time'} || $default_start_date{$ident}
$args{'Start Time'} $default_start_date{$ident}
Truth table33%1/3
A || B
A= $args{'Start Time'}B= $default_start_date{$ident}
ABresult
1X1
011
000
330  
331 die("Number of days required")
332 unless $num_days = $args{'Number of Days'}
333 1 ▶ || $default_num_days{$ident};
Branch: 1 branch
BranchTrueFalseTotal
unless $num_days = $args{"Number of Days"} || $default_num_days{$ident} 0 1 1
Condition33%1/3
$args{'Number of Days'} || $default_num_days{$ident}
l!l&&r!l&&!r
100
MC/DC0%0/2
$args{'Number of Days'} || $default_num_days{$ident}
$args{'Number of Days'} $default_num_days{$ident}
Truth table33%1/3
A || B
A= $args{'Number of Days'}B= $default_num_days{$ident}
ABresult
1X1
011
000
334  
335 1 my $url =
336 'http://www.weather.gov/forecasts/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?';
337  
338 1 $url .= '&lat=' . $latitude;
339 1 $url .= '&lon=' . $longitude;
340 1 ▶ $url .= '&startDate=' . $start_date if $start_date;
Branch: 1 branch
BranchTrueFalseTotal
if $start_date 1 0 1
341 1 ▶ $url .= '&numDays=' . $num_days if $num_days;
Branch: 1 branch
BranchTrueFalseTotal
if $num_days 1 0 1
342 1 ▶ $url .= '&format=' . $NAME_TO_FORMAT{$format} if $format;
Branch: 1 branch
BranchTrueFalseTotal
if $format 1 0 1
343  
344 1 $forecast_xml{$ident} = get $url;
345  
346 1 return $forecast_xml{$ident};
347 }
348 }
349  
350 =pod
351  
352 =head1 AUTHOR
353  
354 Josh McAdams, C<< <josh dot mcadams at gmail dot com> >>
355  
356 =head1 BUGS
357  
358 Please report any bugs or feature requests to
359 C<bug-weather-nws-ndfdgenbyday at rt.cpan.org>, or through the web interface at
360 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Weather-NWS-NDFDgenByDay>.
361 I will be notified, and then you'll automatically be notified of progress on
362 your bug as I make changes.
363  
364 =head1 SUPPORT
365  
366 You can find documentation for this module with the perldoc command.
367  
368 perldoc Weather::NWS::NDFDgenByDay
369  
370 You can also look for information at:
371  
372 =over 4
373  
374 =item * AnnoCPAN: Annotated CPAN documentation
375  
376 L<http://annocpan.org/dist/Weather-NWS-NDFDgenByDay>
377  
378 =item * CPAN Ratings
379  
380 L<http://cpanratings.perl.org/d/Weather-NWS-NDFDgenByDay>
381  
382 =item * RT: CPAN's request tracker
383  
384 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Weather-NWS-NDFDgenByDay>
385  
386 =item * Search CPAN
387  
388 L<http://search.cpan.org/dist/Weather-NWS-NDFDgenByDay>
389  
390 =back
391  
392 =head1 ACKNOWLEDGEMENTS
393  
394 =head1 COPYRIGHT & LICENSE
395  
396 Copyright 2006 Josh McAdams, all rights reserved.
397  
398 This program is free software; you can redistribute it and/or modify it
399 under the same terms as Perl itself.
400  
401 =cut
402  
403 3 1; # End of Weather::NWS::NDFDgenByDay
« NDFDgen.pm ↑ Summary