blib/lib/Weather/Underground/Forecast.pm

filter: Statementstmt 47.9% 35 / 73 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 52.6% 10 / 19 Podpod 100% 4 / 4 totaltotal 49.0% 49 / 100 CC 3 SCAR 10.8
CC
3
Cov
45%
_build_raw_data
12.0
__ANON__
2.0
temperatures
2.0
CRAP 3.0
↑ Summary
1 1 ▶use strictures 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Weather::Underground::Forecast;
3 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $Weather::Underground::Forecast::VERSION = '0.07';
5 }
6 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use XML::Validate::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Data::Dumper::Concise;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 Name
16  
17 Weather::Underground::Forecast - Simple API to Weather Underground Forecast Data
18  
19 =head1 Synopsis
20  
21 Get the weather forecast:
22
23 my $forecast = Weather::Underground::Forecast->new(
24 location => $location,
25 temperature_units => 'fahrenheit', # or 'celsius'
26 );
27
28 Where the $location can be:
29 * 'city,state' Example: location => 'Bloomington,IN'
30 * zip_code Example: location => 11030
31 * 'latitude,longitude' Example: location => '21.3069444,-157.8583333'
32
33 my ($highs, $lows) = $forecast->temperatures;
34  
35 NOTE: I<location> is the only required parameter to C<new()>
36  
37  
38 =cut
39  
40 1 has 'location' => (
41 is => 'rw',
42 isa => 'Str',
43 required => 1,
44 writer => 'set_location',
45 );
46 1 has 'temperature_units' => (
47 is => 'ro',
48 isa => 'Str',
49 'default' => 'fahrenheit',
50 );
51 1 has 'data' => (
52 is => 'rw',
53 isa => 'ArrayRef[HashRef]',
54 lazy_build => 1,
55 );
56 1 has 'raw_data' => (
57 is => 'rw',
58 isa => 'Str',
59 lazy_build => 1,
60 );
61 1 has 'source_URL' => (
62 is => 'ro',
63 isa => 'Any',
64 'default' => 'http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=',
65 );
66  
67 # When the location changes, we want to clear the data to insure a new data fetch will happen.
68 # We need this since data is lazily built, and we used a distinct name for the writer
69 # so we only clear data when we set the location anytime after initial object construction.
70 0 ▶after 'set_location' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 my $self = shift;
72 0 $self->clear_data;
73 1 };
74  
75 =head1 Methods
76  
77 =head2 temperatures
78  
79 Get the high and low temperatures for the number of days specified.
80  
81 Returns: Array of two ArrayRefs being the high and low temperatures
82 Example: my ($highs, $lows) = $wunder->temperatures;
83  
84 =cut
85  
86 0 ▶sub temperatures {
Subroutine temperatures: not called CC=1 SCAR=6.9
Pod documented
87 0 my $self = shift;
88 0 return ( $self->highs, $self->lows );
89 }
90  
91 =head2 highs
92  
93 Get an ArrayRef[Int] of the forecasted high temperatures.
94  
95 =cut
96  
97 0 ▶sub highs {
Subroutine highs: not called CC=1 SCAR=6.9
Pod documented
98 0 my $self = shift;
99  
100 0 my $key1 = 'high';
101 0 my $key2 = $self->temperature_units;
102 0 return $self->_get_forecast_data_by_two_keys( $key1, $key2 );
103 }
104  
105 =head2 lows
106  
107 Get an ArrayRef[Int] of the forecasted low temperatures.
108  
109 =cut
110  
111 0 ▶sub lows {
Subroutine lows: not called CC=1 SCAR=6.9
Pod documented
112 0 my $self = shift;
113  
114 0 my $key1 = 'low';
115 0 my $key2 = $self->temperature_units;
116 0 return $self->_get_forecast_data_by_two_keys( $key1, $key2 );
117 }
118  
119 =head2 precipitation
120  
121 Get an ArrayRef[Int] of the forecasted chance of precipitation.
122  
123 Example: my $chance_of_precip = $wunder->precipitation;
124  
125 =cut
126  
127 0 ▶sub precipitation {
Subroutine precipitation: not called CC=1 SCAR=6.9
Pod documented
128 0 my $self = shift;
129  
130 0 return $self->_get_forecast_data_by_one_key('pop');
131 }
132  
133 # =head2 _get_forecast_data_by_one_key
134  
135 # Get the values for a single forecast metric that is
136 # only one key deep. An examples is: 'pop' (prob. of precip.)
137  
138 # NOTE: One can dump the data attribute to see
139 # the exact data structure and keys available.
140  
141 # =cut
142  
143 0 ▶sub _get_forecast_data_by_one_key {
Subroutine _get_forecast_data_by_one_key: not called CC=1 SCAR=6.9
144 0 my ( $self, $key ) = @_;
145  
146 0 return [ map { $_->{$key} } @{ $self->data } ];
147 }
148  
149 # =head2 _get_forecast_data_by_two_keys
150  
151 # Like the one_key method above but for values that are
152 # two keys deep in the data structure.
153  
154 # =cut
155  
156 0 ▶sub _get_forecast_data_by_two_keys {
Subroutine _get_forecast_data_by_two_keys: not called CC=1 SCAR=6.9
157 0 my ( $self, $key1, $key2 ) = @_;
158  
159 0 return [ map { $_->{$key1}->{$key2} } @{ $self->data } ];
160 }
161  
162 3 ▶sub _query_URL {
Subroutine _query_URL: called CC=1 SCAR=0.0
163 3 my $self = shift;
164 3 return $self->source_URL . $self->location;
165 }
166  
167 # Builders
168  
169 0 ▶sub _build_data {
Subroutine _build_data: not called CC=1 SCAR=6.9
170 0 my $self = shift;
171  
172 0 my $xml = XML::Simple->new;
173 0 my $data_ref = $xml->XMLin( $self->raw_data );
174 0 my $forecasts = $data_ref->{simpleforecast}->{forecastday};
175  
176 0 return $forecasts;
177 }
178  
179 0 ▶sub _build_raw_data {
Subroutine _build_raw_data: not called CC=3 SCAR=24.8
180 0 my $self = shift;
181  
182 0 my $content = get( $self->_query_URL );
183 0 ▶ die "Couldn't get URL: ", $self->_query_URL unless defined $content;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 0 0
184  
185 0 my $xml_validator = new XML::Validate::LibXML;
186 0 ▶ if ( !$xml_validator->validate($content) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xml_validator->validate($content)) 0 0 0
187 0 my $intro = "Document is invalid\n";
188 0 my $message = $xml_validator->last_error()->{message};
189 0 my $line = $xml_validator->last_error()->{line};
190 0 my $column = $xml_validator->last_error()->{column};
191 0 die "Error: $intro $message at line $line, column $column";
192 }
193  
194 # return and set attribute to raw xml when we make it this far.
195 0 return $content;
196 }
197  
198 1 __PACKAGE__->meta->make_immutable;
199 1 1
200  
201 __END__
↑ Summary