File Coverage

blib/lib/App/wu.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1 1     1   11915 use strict;
  1         1  
  1         24  
2 1     1   3 use warnings;
  1         1  
  1         31  
3             package App::wu;
4             $App::wu::VERSION = '0.04';
5 1     1   151 use WWW::Wunderground::API 0.06;
  0            
  0            
6             use Cache::FileCache;
7             use Carp;
8             use Try::Tiny;
9              
10             #ABSTRACT: Terminal app that provides an hourly weather forecast using Weather Underground API
11              
12              
13              
14              
15             sub new
16             {
17             croak 'Incorrect number of args passed to constructor' unless @_ == 3;
18             my ($class, $location, $api_key) = @_;
19              
20             my $wu = new WWW::Wunderground::API(
21             location => $location,
22             api_key => $api_key,
23             auto_api => 1,
24             cache => Cache::FileCache->new({
25             namespace => 'wundercache',
26             default_expires_in => 2400 }),
27             );
28              
29             return bless { wu => $wu }, $class;
30             }
31              
32             sub print_hourly
33             {
34             my $self = shift;
35             my $wu = $self->{wu};
36              
37             try {
38             my @hourly_results = @{ $wu->hourly };
39             {
40             # print header
41             binmode STDOUT, ':utf8'; # for degrees symbol
42             printf "%-10s%-4s%-4s%-8s%-20s\n",
43             'Time',
44             "\x{2109}",
45             "\x{2103}",
46             'Rain %',
47             'Conditions';
48              
49             # print hourly
50             for (@hourly_results)
51             {
52             printf "%8s%4i%4i%8i %-30s\n",
53             $_->{FCTTIME}{civil},
54             $_->{temp}{english},
55             $_->{temp}{metric},
56             $_->{pop},
57             $_->{condition};
58             }
59             }
60             } catch
61             { # see if there is an error message to display
62             if (exists $wu->{data}{hourly}{response}{error}{description})
63             {
64             print "$wu->{data}{hourly}{response}{error}{description}\n";
65             }
66             else
67             {
68             print "Error connecting to Wunderground API (is your Internet connection active?)\n";
69             }
70             };
71             }
72              
73             1;
74              
75             __END__