blib/lib/WWW/Wunderground/API.pm

filter: Statementstmt 69.8% 74 / 106 Branchbran 31.5% 12 / 38 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 83.3% 20 / 24 Podpod 66.6% 4 / 6 totaltotal 59.2% 112 / 189 CC 24 SCAR 22.8
CC
24
Cov
58%
api_call
42.0
_guess_key
20.0
AUTOLOAD
8.4
CRAP 9.8
↑ Summary
1 package WWW::Wunderground::API;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use JSON::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Hash::AsObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 WWW::Wunderground::API - Use Weather Underground's JSON/XML API
13  
14 =head1 VERSION
15  
16 Version 0.06
17  
18 =cut
19  
20 3 our $VERSION = '0.06';
21  
22 3 has location => (is=>'rw', required=>1);
23 1 ▶has api_key => (is=>'ro', default=>sub { $ENV{WUNDERGROUND_API} });
Subroutine __ANON__: called CC=1 SCAR=0.0
24 1 ▶has api_type => (is=>'rw', lazy=>1, default=>sub { $_[0]->api_key ? 'json' : 'xml' });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->api_key ? : 0 1 1
25 1 ▶has cache => (is=>'ro', lazy=>1, default=>sub { new WWW::Wunderground::API::BadCache });
Subroutine __ANON__: called CC=1 SCAR=0.0
26 1 ▶has auto_api => (is=>'ro', default=> sub {0} );
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 ▶has raw => (is=>'rw', default=>sub{''});
Subroutine __ANON__: called CC=1 SCAR=0.0
28 1 ▶has data => (is=>'rw', lazy=>1, default=>sub{ Hash::AsObject->new } );
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30 sub json {
31 0 ▶ my $self = shift;
Subroutine json: not called CC=2 SCAR=17.9
Pod documented
32 0 ▶ return $self->api_type eq 'json' ? $self->raw : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->api_type eq 'json' ? : 0 0 0
33 }
34  
35 sub xml {
36 1 ▶ my $self = shift;
Subroutine xml: called CC=2 SCAR=7.2
Pod documented
37 1 ▶ return $self->api_type eq 'xml' ? $self->raw : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->api_type eq 'xml' ? : 1 0 1
38 }
39  
40  
41 sub update {
42 1 ▶ my $self = shift;
Subroutine update: called CC=4 SCAR=15.5
Pod documented
43 1 ▶ if ($self->api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->api_key) { } 0 1 1
44 0 $self->api_call('conditions');
45 } else {
46 1 my $legacy_url = 'http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query='.$self->location;
47 1 my $xml;
48 1 ▶ unless($xml = $self->cache->get($legacy_url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xml = $self->cache->get($legacy_url)) 1 0 1
49 1 $xml = get($legacy_url);
50 1 $self->cache->set($legacy_url,$xml);
51 }
52 1 ▶ if ($xml) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xml) 0 1 1
53 0 $self->raw($xml);
54 0 $self->data(Hash::AsObject->new({conditions=>XMLin($xml)}));
55 }
56 }
57 }
58  
59 sub _guess_key {
60 0 ▶ my $self = shift;
Subroutine _guess_key: not called CC=4 SCAR=30.0
61 0 my ($struc,$action) = @_;
62  
63 #try to guess result structure key
64 0 ▶ return $action if defined($struc->{$action});
Branch: 1 branch
BranchTrueFalseTotal
if defined $struc->{$action} 0 0 0
65 0 foreach my $key (keys %$struc) {
66 0 ▶ next if $key=~ /(response|features|version|termsofservice)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /(response|features|version|termsofservice)/i 0 0 0
67 0 return $key;
68 }
69 }
70  
71 sub api_call {
72 0 ▶ my $self = shift;
Subroutine api_call: not called CC=6 SCAR=37.4
Pod documented
73 0 my ($action, $location) = @_;
74 0 ▶ $location||=$self->location;
Condition0%0/3
$location ||= $self->location
l!l&&r!l&&!r
000
MC/DC0%0/2
$location ||= $self->location
$location $self->location
Truth table0%0/3
A ||= B
A= $locationB= $self->location
ABresult
1X1
011
000
75 0 ▶ if ($self->api_key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->api_key) { } 0 0 0
76 0 my $base = 'http://api.wunderground.com/api';
77 0 my $url = join('/', $base,$self->api_key,$action,'q',$location).'.'.$self->api_type;
78  
79 0 my $result;
80 0 ▶ unless ($result = $self->cache->get($url)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result = $self->cache->get($url)) 0 0 0
81 0 $result = get($url);
82 0 $self->cache->set($url,$result);
83 }
84  
85 0 $self->raw($result);
86
87 0 ▶ my $struc = $self->api_type eq 'json'
Branch: 1 branch
BranchTrueFalseTotal
$self->api_type eq 'json' ? : 0 0 0
88 ? JSON::Any->jsonToObj($self->raw)
89 : XMLin($self->raw);
90  
91 0 my $action_key = $self->_guess_key($struc,$action);
92  
93 0 ▶ $struc = $struc->{$action_key} if $action_key;
Branch: 1 branch
BranchTrueFalseTotal
if $action_key 0 0 0
94 0 $self->data->{$action} = $struc;
95  
96 0 return new Hash::AsObject($struc);
97 } else {
98 0 warn "Only basic weather conditions are supported using the deprecated keyless interface";
99 0 warn "please visit http://www.wunderground.com/weather/api to obtain your own API key";
100 }
101 }
102  
103  
104 around BUILDARGS => sub {
105 1 ▶ my $orig = shift;
Subroutine __ANON__: called CC=3 SCAR=12.7
106 1 my $class = shift;
107 1 ▶ if (@_ == 1 and !ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) { } 1 0 1
Condition33%1/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
001
MC/DC0%0/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table33%1/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
108 1 return $class->$orig( location=>$_[0] );
109 } else {
110 0 return $class->$orig(@_);
111 }
112 3 };
113  
114 sub AUTOLOAD {
115 1 ▶ my $self = shift;
Subroutine AUTOLOAD: called CC=7 SCAR=21.3
116 1 our $AUTOLOAD;
117 1 my ($key) = $AUTOLOAD =~ /::(\w+)$/;
118 1 my $val = $self->data->$key;
119  
120 1 ▶ unless ($val) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($val) 1 0 1
121 1 ▶ $self->update if ($self->auto_api and !$self->data->conditions);
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_api and not $self->data->conditions 0 1 1
Condition33%1/3
$self->auto_api and not $self->data->conditions
!ll&&!rl&&r
100
MC/DC0%0/2
$self->auto_api and not $self->data->conditions
$self->auto_api not $self->data->conditions
Truth table33%1/3
A and not(B)
A= $self->auto_apiB= not $self->data->conditions
ABresult
0X0
100
111
122 1 ▶ $val = $self->data->conditions->$key if $self->data->conditions;
Branch: 1 branch
BranchTrueFalseTotal
if $self->data->conditions 0 1 1
123 }
124  
125 1 ▶ if (defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 0 1 1
126 0 return $val;
127 } else {
128 1 ▶ return $self->api_call($key) if $self->auto_api;
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto_api 0 1 1
129 1 warn "$key is not defined. Is it a valid key, and is data actually loading?";
130 1 warn "If you're trying to autoload an endpoint, set auto_api to something truthy";
131 1 return undef;
132 }
133 }
134  
135 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
136  
137 3 __PACKAGE__->meta->make_immutable;
138  
139  
140 #The following exists purely as an example for others of what not to do.
141 #Use a Cache::Cache or CLI Cache. Really.
142 package WWW::Wunderground::API::BadCache;
143 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
144  
145 1 ▶has store=>(is=>'rw', lazy=>1, default=>sub{{}});
Subroutine __ANON__: called CC=1 SCAR=0.0
146  
147 sub get {
148 1 ▶ my $self = shift;
Subroutine get: called CC=2 SCAR=7.4
Pod undocumented
149 1 my ($key) = @_;
150 1 ▶ if (exists($self->store->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->store->{$key}) 0 1 1
151 0 return $self->store->{$key};
152 }
153 1 return undef;
154 }
155  
156 sub set {
157 1 ▶ my $self = shift;
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
158 1 my ($key, $val) = @_;
159 1 $self->store->{$key} = $val;
160 1 return $val;
161 }
162  
163  
164 =head1 SYNOPSIS
165  
166 Connects to the Weather Underground JSON/XML service and parses the data
167 into something usable. The entire response is available in Hash::AsObject form, so
168 any data that comes from the server is accessible. Print a Data::Dumper of ->data
169 to see all of the tasty data bits.
170  
171 use WWW::Wunderground::API;
172  
173 #location
174 my $wun = new WWW::Wunderground::API('Fairfax, VA');
175  
176 #or zipcode
177 my $wun = new WWW::Wunderground::API('22030');
178  
179 #or airport identifier
180 my $wun = new WWW::Wunderground::API('KIAD');
181  
182 #using the options
183  
184 my $wun = new WWW::Wunderground::API(
185 location=>'22152',
186 api_key=>'my wunderground api key',
187 auto_api=>1,
188 cache=>Cache::FileCache->new({ namespace=>'wundercache', default_expires_in=>2400 }) #A cache is probably a good idea.
189 );
190  
191
192 #Check the wunderground docs for details, but here are just a few examples
193 print 'The temperature is: '.$wun->conditions->temp_f."\n";
194 print 'The rest of the world calls that: '.$wun->conditions->temp_c."\n";
195 print 'Record high temperature year: '.$wun->almanac->temp_high->recordyear."\n";
196 print "Sunrise at:".$wun->astronomy->sunrise->hour.':'.$wun->astronomy->sunrise->minute."\n";
197 print "Simple forecast:".$wun->forecast->simpleforecast->forecastday->[0]{conditions}."\n";
198 print "Text forecast:".$wun->forecast->txt_forecast->forecastday->[0]{fcttext}."\n";
199 print "Long range forecast:".$wun->forecast10day->txt_forecast->forecastday->[9]{fcttext}."\n";
200 print "Chance of rain three hours from now:".$wun->hourly->[3]{pop}."%\n";
201 print "Nearest airport:".$wun->geolookup->nearby_weather_stations->airport->{station}[0]{icao}."\n";
202  
203 #Conditions is autoloaded into the root of the object
204 print "Temp_f:".$wun->temp_f."\n";
205  
206 =head1 METHODS/ACCESSORS
207  
208 =head2 update()
209  
210 Included for backward compatibility only.
211 Refetches conditions data from the server. It will be removed in a future release.
212 If you specify an api_key then this is equvilent of ->api_call('conditions') and is subject to the same cache
213  
214 =head2 location()
215  
216 Set the location. For example:
217  
218 my $wun = new WWW::Wunderground::API('22030');
219 my $ffx_temp = $wun->conditions->temp_f;
220  
221 $wun->location('KJFK');
222 my $ny_temp = $wun->conditions->temp_f;
223  
224 $wun->location('San Diego, CA');
225 my $socal_temp = $wun->conditions->temp_f;
226  
227 Valid locations can be derived from others by calling the geolookup endpoint, but you probably already know where you are.
228  
229  
230 =head2 auto_api
231  
232 set auto_api to something truthy to have the module automatically make API calls without the use of api_call()
233  
234  
235 =head2 api_call( api_name, <location> )
236  
237 Set api_name to any location-based wunderground api call (almanac,conditions,forecast,history...).
238  
239 Location is optional and defaults to L</"location()">. Can be any valid location (eg 22152,'KIAD','q/CA/SanFrancisco',...)
240  
241 #Almanac data for 90210
242 $wun->api_call('almanac','90210');
243  
244 #If auto_api=>1 the following is equivalent
245 $wun->location(90120);
246 $wun->almanac;
247  
248 #10 day forecast for New York
249 $wun->api_call('forecast10day'','KJFK');
250  
251  
252  
253 =head2 raw()
254  
255 Returns raw text result from the most recent API call. This will be either xml or json depending on api_type.
256 You can also set this to whatever json/xml you'd like, though I can't imagine why you'd want to.
257  
258 =head2 cache()
259  
260 Specify a cache object. Needs only to support get(key) and set (key,value) so L<Cache::Cache> or L<CLI> caches should work.
261  
262 =head2 xml()
263  
264 *Deprecated* - use L</"raw()"> instead.
265  
266 Returns raw xml result from wunderground server where applicable
267  
268  
269 =head2 json()
270  
271 *Deprecated* - use L</"raw()"> instead.
272  
273 Returns raw json result from wunderground server where applicable
274  
275 =head2 data()
276  
277 Contains all weather data from server parsed into convenient L<Hash::AsObject> form;
278  
279 =head2 api_key()
280  
281 Required for JSON api access. Defaults to $ENV{WUNDERGROUND_API}
282  
283 =head2 api_type()
284  
285 Defaults to json. If no api_key is specified it will be set to xml and only basic weather conditions will be available.
286  
287 =head1 AUTHOR
288  
289 John Lifsey, C<< <nebulous at crashed.net> >>
290  
291 =head1 BUGS
292  
293 Please report any bugs or feature requests to C<bug-www-wunderground-api at rt.cpan.org>, or through
294 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Wunderground-API>. I will be notified, and then you'll
295 automatically be notified of progress on your bug as I make changes.
296  
297 =head1 SOURCE
298  
299 Better yet, fork on github and send me a pull request:
300 L<https://github.com/nebulous/WWW-Wunderground-API>
301  
302  
303 =head1 SUPPORT
304  
305 You can find documentation for this module with the perldoc command.
306  
307 perldoc WWW::Wunderground::API
308  
309  
310 You can also look for information at:
311  
312 =over 4
313  
314 =item * RT: CPAN's request tracker (report bugs here)
315  
316 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Wunderground-API>
317  
318 =item * AnnoCPAN: Annotated CPAN documentation
319  
320 L<http://annocpan.org/dist/WWW-Wunderground-API>
321  
322 =item * CPAN Ratings
323  
324 L<http://cpanratings.perl.org/d/WWW-Wunderground-API>
325  
326 =item * Search CPAN
327  
328 L<http://search.cpan.org/dist/WWW-Wunderground-API/>
329  
330 =back
331  
332  
333 =head1 LICENSE AND COPYRIGHT
334  
335 Copyright 2013 John Lifsey.
336  
337 This program is free software; you can redistribute it and/or modify it
338 under the terms of either: the GNU General Public License as published
339 by the Free Software Foundation; or the Artistic License.
340  
341 See http://dev.perl.org/licenses/ for more information.
342  
343  
344 =cut
345  
346 3 __PACKAGE__->meta->make_immutable;
347 3 1; # End of WWW::Wunderground::API
↑ Summary