Com/Forecast.pm

filter: Statementstmt 28.3% 19 / 67 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 54.5% 6 / 11 Podpod 75.0% 3 / 4 totaltotal 26.4% 28 / 106 CC 11 SCAR 23.8
CC
11
Cov
22%
new
20.0
_build_forecasts
20.0
day
12.0
CRAP 10.8
« Com/Finder.pm ↑ Summary Com/Location.pm »
1 package Weather::Com::Forecast;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Weather::Com::DayForecast;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base "Weather::Com::Cached";
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $VERSION = sprintf "%d.%03d", q$Revision: 1.6 $ =~ /(\d+)/g;
11  
12 #------------------------------------------------------------------------
13 # Constructor
14 #------------------------------------------------------------------------
15 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
16 0 my $proto = shift;
17 0 ▶ my $class = ref($proto) || $proto;
Condition0%0/3
ref $proto || $proto
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table0%0/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
18 0 my %parameters;
19  
20 # parameters provided by new method
21 0 ▶ if ( ref( $_[0] ) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
22 0 %parameters = %{ $_[0] };
23 } else {
24 0 %parameters = @_;
25 }
26  
27 0 ▶ unless ( $parameters{location_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parameters{"location_id"}) 0 0 0
28 0 die "You need to provide a location id!\n";
29 }
30  
31 # set some parameters to sensible values for a
32 # current conditions object
33 0 $parameters{current} = 0;
34 0 $parameters{forecast} = 10;
35 0 $parameters{links} = 0;
36  
37 # creating the SUPER instance
38 0 my $self = $class->SUPER::new( \%parameters );
39 0 $self->{ID} = $parameters{location_id};
40  
41 # getting first weather info
42 0 $self->{WEATHER} = $self->get_weather( $self->{ID} );
43 0 $self->{DAYS} = undef;
44 0 $self->_build_forecasts();
45 0 $self->{LSUP} = time();
46  
47 0 return $self;
48 } # end new()
49  
50 #------------------------------------------------------------------------
51 # refresh weather data
52 #------------------------------------------------------------------------
53 0 ▶sub refresh {
Subroutine refresh: not called CC=2 SCAR=17.9
Pod undocumented
54 0 my $self = shift;
55 0 my $now = time();
56  
57 # only refresh if last update has been more than 15 min ago
58 0 ▶ if ( ( $now - $self->{LSUP} ) > 900 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($now - $self->{"LSUP"} > 900) 0 0 0
59 0 $self->{WEATHER} = $self->get_weather( $self->{ID} );
60 0 $self->_build_forecasts();
61 0 $self->{LSUP} = $now;
62 }
63 0 return 1;
64 }
65  
66 #------------------------------------------------------------------------
67 # access location data
68 #------------------------------------------------------------------------
69 0 ▶sub day {
Subroutine day: not called CC=3 SCAR=24.8
Pod documented
70 0 my $self = shift;
71 0 my $day = shift; # 0 - 9
72  
73 0 ▶ return 0 unless ( ( $day >= 0 ) && ( $day <= 9 ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $day >= 0 and $day <= 9 0 0 0
Condition0%0/3
$day >= 0 and $day <= 9
!ll&&!rl&&r
000
MC/DC0%0/2
$day >= 0 and $day <= 9
$day >= 0 $day <= 9
Truth table0%0/3
A and B
A= $day >= 0B= $day <= 9
ABresult
0X0
100
111
74  
75 0 $self->refresh();
76 0 return $self->{DAYS}->[$day];
77 }
78  
79 0 ▶sub all {
Subroutine all: not called CC=2 SCAR=17.9
Pod documented
80 0 my $self = shift;
81 0 $self->refresh();
82  
83 0 ▶ if ( wantarray() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
84 0 return @{ $self->{DAYS} };
85 } else {
86 0 return $self->{DAYS};
87 }
88 }
89  
90 #------------------------------------------------------------------------
91 # build up forecast hashes
92 #------------------------------------------------------------------------
93 0 ▶sub _build_forecasts {
Subroutine _build_forecasts: not called CC=4 SCAR=30.0
94 0 my $self = shift;
95  
96 # initialize $self->{DAYS} array of new DayForecast objects
97 0 ▶ unless ( $self->{DAYS} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"DAYS"}) 0 0 0
98 0 ▶ for ( my $i = 0 ; $i < 10 ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < 10) 0 0 0
99 0 $self->_debug("Initializing forecast for day $i");
100 0 $self->{DAYS}->[$i] = Weather::Com::DayForecast->new($self->{ARGS});
101 }
102 }
103  
104 # then update the DayForecast objects
105 # but put the timeszone into it...
106 0 foreach my $day ( @{ $self->{WEATHER}->{dayf}->{day} } ) {
107 0 my %args = (%{$day}, (zone => $self->{ARGS}->{zone}));
108 0 $self->{DAYS}->[ $day->{d} ]->update(\%args);
109 }
110  
111 0 return 1;
112 }
113  
114 1 1;
115  
116 __END__
« Com/Finder.pm ↑ Summary Com/Location.pm »