Com/Cached.pm

filter: Statementstmt 24.1% 34 / 141 Branchbran 6.2% 3 / 48 Conditioncond 5.0% 1 / 20 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 57.1% 8 / 14 Podpod 100% 3 / 3 totaltotal 20.6% 49 / 237 CC 35 SCAR 52.1
CC
35
Cov
18%
get_weather
380.0
search
132.0
_older_than
12.0
CRAP 182.3
↑ Summary Com/Base.pm »
1 package Weather::Com::Cached;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Storable qw(lock_store lock_retrieve);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Weather::Com::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use base "Weather::Com::Base";
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our $VERSION = sprintf "%d.%03d", q$Revision: 1.7 $ =~ /(\d+)/g;
12  
13 #------------------------------------------------------------------------
14 # Constructor
15 #------------------------------------------------------------------------
16 2 ▶sub new {
Subroutine new: called CC=5 SCAR=18.2
Pod documented
17 2 my $proto = shift;
18 2 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
19  
20 # getting the parameters from @_
21 2 my %parameters = ();
22 2 ▶ if ( ref( $_[0] ) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 2 2
23 0 %parameters = %{ $_[0] };
24 } else {
25 2 %parameters = @_;
26 }
27  
28 # creating the SUPER instance
29 2 my $self = $class->SUPER::new( \%parameters );
30  
31 # where to put the cache files?
32 2 ▶ if ( $parameters{cache} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parameters{'cache'}) { } 0 2 2
33 0 $self->{PATH} = $parameters{cache};
34 } else {
35 2 $self->{PATH} = ".";
36 }
37  
38 # check if cache is writable
39 2 ▶ unless ( -w $self->{PATH} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-w $self->{"PATH"}) 0 2 2
40 die ref($self)
41 . ": path to cache not writable: "
42 0 . $self->{PATH} . "!\n";
43 }
44  
45 # parameter cache
46 2 $self->{PARAMS} = undef;
47  
48 2 bless( $self, $class );
49  
50 2 return $self;
51 }
52  
53 #------------------------------------------------------------------------
54 # searching for location codes
55 # Weather::Com::Cached will not search on the web for location codes
56 # that are cached in "locations.dat"
57 #------------------------------------------------------------------------
58 0 ▶sub search {
Subroutine search: not called CC=11 SCAR=48.8
Pod documented
59 0 my $self = shift;
60 0 my $place = shift;
61 0 my $locations = undef;
62  
63 # set error and die if no place provided
64 0 ▶ unless ($place) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($place) 0 0 0
65 0 die ref($self), ": ERROR Please provide a location to search for!\n";
66 }
67  
68 # 1st, look for locations in cache
69 0 my $locations_cached = undef;
70 0 my $loccachefile = $self->{PATH} . "/locations.dat";
71 0 ▶ if ( -f $loccachefile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $loccachefile) 0 0 0
72 0 $locations_cached = lock_retrieve($loccachefile);
73 0 ▶ if ( $locations_cached->{ lc($place) } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$locations_cached{lc $place}) { } 0 0 0
74 0 $self->_debug("Found locations in location cache.");
75 0 $locations = $locations_cached->{ lc($place) };
76 } else {
77 0 $self->_debug("No direct match found in cache.");
78 }
79 }
80  
81 # 2nd, if nothing has been found, search the Web and store data
82 0 ▶ unless ( keys %{$locations} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (keys %{$locations;}) 0 0 0
83 0 $locations = $self->SUPER::search($place);
84 0 ▶ if ($locations) {
Branch: 2 branches
BranchTrueFalseTotal
if ($locations) { } 0 0 0
elsif ($locations_cached) { } 0 0 0
85 0 $self->_debug("Found locations on the web.");
86 0 $self->_debug("Writing locations to cache.");
87  
88 # first save the direct search result
89 0 $locations_cached->{ lc($place) } = $locations;
90  
91 # then store for each result the name => key hash
92 0 foreach my $location ( keys %{$locations} ) {
93 0 my $name = $locations->{$location};
94 0 $locations_cached->{ lc($name) } = { $location => $name };
95 }
96  
97 # then store in cache file
98 0 ▶ unless ( lock_store( $locations_cached, $loccachefile ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (lock_store($locations_cached, $loccachefile)) 0 0 0
99 0 die ref($self),
100 ": ERROR I/O problem while storing locations cachefile!";
101 }
102 } elsif ($locations_cached) {
103  
104 # if neither the cache nor the weather.com server did return
105 # an exact match, try a regexp search over the cache.
106 0 $self->_debug("No direct match in cache or on the web.");
107 0 $self->_debug("Trying regexp search.");
108 0 $locations = {};
109 0 foreach my $location ( keys %{$locations_cached} ) {
110 0 ▶ if ( $location =~ /$place/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($location =~ /$place/i) 0 0 0
111 0 $self->_debug(
112 "MATCH: '$place' matches location '$location'");
113 0 %{$locations} = (
114 0 %{$locations},
115 %{
116 0 $locations_cached->{ lc($location) }
117 }
118 );
119 }
120 }
121 }
122  
123 }
124  
125 0 return $locations;
126 }
127  
128 #------------------------------------------------------------------------
129 # getting data from weather.com
130 #------------------------------------------------------------------------
131 0 ▶sub get_weather {
Subroutine get_weather: not called CC=19 SCAR=59.4
Pod documented
132 0 my $self = shift;
133 0 my $locid = shift;
134  
135 0 $self->_debug("Trying to get data for $locid");
136  
137 0 ▶ unless ($locid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($locid) 0 0 0
138 0 die ref($self), ": Please provide a location id!\n";
139 }
140  
141 # try to load an existing cache file
142 0 my $cachefile = $self->{PATH} . "/" . $self->{UNITS} . "_$locid.dat";
143 0 my $weathercache = {};
144 0 ▶ if ( -f $cachefile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $cachefile) { } 0 0 0
145 0 $weathercache = lock_retrieve($cachefile);
146 } else {
147 0 $self->_debug("No cache file found.");
148 }
149  
150 # find out which data is wanted by the modules user and
151 # which parts of that are in the cache or must be
152 # loaded from the web
153 0 ▶ if ($weathercache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weathercache) 0 0 0
154  
155 0 $self->_debug("Cache file found.");
156  
157 # save parameters to be able to reset them at the end of
158 # this method...
159 0 $self->_store_params();
160  
161 # load uncached or old requested data
162 0 ▶ if ( $self->{PARAMS}->{CC} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PARAMS"}{"CC"}) 0 0 0
163 0 ▶ if ( $weathercache->{cc}
Branch: 1 branch
BranchTrueFalseTotal
if ($weathercache->{"cc"} and not $self->_older_than(30, $weathercache->{"cc"}{"cached"})) 0 0 0
Condition0%0/3
$weathercache->{"cc"} and not $self->_older_than(30, $weathercache->{"cc"}{"cached"})
!ll&&!rl&&r
000
MC/DC0%0/2
$weathercache->{"cc"} and not $self->_older_than(30, $weathercache->{"cc"}{"cached"})
$weathercache->{"cc"} not $self->_older_than(30, $weathercache->{"cc"}{"cached"})
Truth table0%0/3
A and not(B)
A= $weathercache->{"cc"}B= not $self->_older_than(30, $weathercache->{"cc"}{"cached"})
ABresult
0X0
100
111
164 && !$self->_older_than( 30, $weathercache->{cc}->{cached} ) )
165 {
166 0 $self->{CC} = 0;
167 0 $self->_debug("Turning off 'cc' update. Cache is good enough.");
168 }
169 }
170  
171 0 ▶ if ( $self->{PARAMS}->{FORECAST} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PARAMS"}{"FORECAST"}) 0 0 0
172 0 ▶ if ( $weathercache->{dayf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($weathercache->{"dayf"}) 0 0 0
173 0 my $no_forecastdays; # number of days to be forecasted
174 0 ▶ if ( ref( $weathercache->{dayf}->{day} ) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $weathercache->{'dayf'}{'day'} eq 'HASH') { } 0 0 0
175 0 $no_forecastdays = 1;
176 } else {
177 0 $no_forecastdays = $#{ $weathercache->{dayf}->{day} } + 1;
178 }
179  
180 0 ▶ if ( ( $self->{PARAMS}->{FORECAST} == $no_forecastdays )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"PARAMS"}{"FORECAST"} == $no_forecastdays and not $self->_older_than(120, $weathercache->{"dayf"}{"cached"})) 0 0 0
Condition0%0/3
$self->{"PARAMS"}{"FORECAST"} == $no_forecastdays and not $self->_older_than(120, $weathercache->{"dayf"}{"cached"})
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"PARAMS"}{"FORECAST"} == $no_forecastdays and not $self->_older_than(120, $weathercache->{"dayf"}{"cached"})
$self->{"PARAMS"}{"FORECAST"} == $no_forecastdays not $self->_older_than(120, $weathercache->{"dayf"}{"cached"})
Truth table0%0/3
A and not(B)
A= $self->{"PARAMS"}{"FORECAST"} == $no_forecastdaysB= not $self->_older_than(120, $weathercache->{"dayf"}{"cached"})
ABresult
0X0
100
111
181 && !$self->_older_than( 120,
182 $weathercache->{dayf}->{cached} ) )
183 {
184 0 $self->{FORECAST} = 0;
185 0 $self->_debug(
186 "Turning off 'forecast' update. Cache is good enough.");
187 }
188 }
189 }
190 }
191  
192 0 $self->_debug("Params for cache conditions: ".Dumper($self->{PARAMS}));
193  
194 # only update weathercache if a current conditions update or a
195 # forecast update is necessary or the location data is older than
196 # 15 minutes.
197 0 ▶ if ( $self->{CC}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"CC"} or $self->{"FORECAST"} or not $weathercache or $self->_older_than(15, $weathercache->{"loc"}{"cached"})) 0 0 0
Condition0%0/9
$self->{"CC"} or $self->{"FORECAST"} or not $weathercache or $self->_older_than(15, $weathercache->{"loc"}{"cached"})
l!l&&r!l&&!r
000
$self->{"CC"} or $self->{"FORECAST"} or not $weathercache
l!l&&r!l&&!r
000
$self->{"CC"} or $self->{"FORECAST"}
l!l&&r!l&&!r
000
MC/DC0%0/4
$self->{"CC"} or $self->{"FORECAST"} or not $weathercache or $self->_older_than(15, $weathercache->{"loc"}{"cached"})
$self->{"CC"} $self->{"FORECAST"} not $weathercache $self->_older_than(15, $weathercache->{"loc"}{"cached"})
Truth table0%0/5
A or B or not(C) or D
A= $self->{"CC"}B= $self->{"FORECAST"}C= not $weathercacheD= $self->_older_than(15, $weathercache->{"loc"}{"cached"})
ABCDresult
1XXX1
01XX1
001X1
00011
00000
198 or $self->{FORECAST}
199 or !$weathercache
200 or $self->_older_than( 15, $weathercache->{loc}->{cached} ) )
201 {
202 0 $self->_debug("All conditions met. Fetching weather from web.");
203  
204 0 my $weather = $self->SUPER::get_weather($locid);
205 0 foreach ( keys %{$weather} ) {
206 0 $weathercache->{$_} = $weather->{$_};
207 0 ▶ if ( ref( $weather->{$_} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $weather->{$_}) 0 0 0
208 0 $weathercache->{$_}->{cached} = $self->_cache_time();
209 }
210 }
211  
212 # save data to cache file
213 0 ▶ unless (
Branch: 1 branch
BranchTrueFalseTotal
unless (lock_store($weathercache, $self->{"PATH"} . '/' . $self->{"UNITS"} . "_$locid.dat")) 0 0 0
214 lock_store(
215 $weathercache,
216 $self->{PATH} . "/" . $self->{UNITS} . "_$locid.dat"
217 )
218 )
219 {
220 0 die ref($self), ": ERROR I/O problem while storing cachefile!";
221 }
222 }
223  
224 0 $self->_reset_params();
225 0 $self->_debug( Dumper($weathercache) );
226  
227 0 return $weathercache;
228 }
229  
230 #------------------------------------------------------------------------
231 # store and reset search parameters
232 #------------------------------------------------------------------------
233 0 ▶sub _store_params {
Subroutine _store_params: not called CC=1 SCAR=6.9
234 0 my $self = shift;
235 my %params = (
236 'PROXY' => $self->{PROXY},
237 'TIMEOUT' => $self->{TIMEOUT},
238 'DEBUG' => $self->{DEBUG},
239 'PARTNER_ID' => $self->{PARTNER_ID},
240 'LICENSE_KEY' => $self->{LICENSE_KEY},
241 'UNITS' => $self->{UNITS},
242 'CC' => $self->{CC},
243 'FORECAST' => $self->{FORECAST},
244 'LINKS' => $self->{LINKS},
245 0 );
246  
247 0 $self->{PARAMS} = \%params;
248  
249 0 return 1;
250 }
251  
252 0 ▶sub _reset_params {
Subroutine _reset_params: not called CC=1 SCAR=6.9
253 0 my $self = shift;
254  
255 0 $self->{PROXY} = $self->{PARAMS}->{PROXY};
256 0 $self->{TIMEOUT} = $self->{PARAMS}->{TIMEOUT};
257 0 $self->{DEBUG} = $self->{PARAMS}->{DEBUG};
258 0 $self->{PARTNER_ID} = $self->{PARAMS}->{PARTNER_ID};
259 0 $self->{LICENSE_KEY} = $self->{PARAMS}->{LICENSE_KEY};
260 0 $self->{UNITS} = $self->{PARAMS}->{UNITS};
261 0 $self->{CC} = $self->{PARAMS}->{CC};
262 0 $self->{FORECAST} = $self->{PARAMS}->{FORECAST};
263 0 $self->{LINKS} = $self->{PARAMS}->{LINKS};
264  
265 0 return 1;
266 }
267  
268 0 ▶sub _older_than {
Subroutine _older_than: not called CC=3 SCAR=24.8
269 0 my $self = shift;
270 0 my $caching_timeframe = shift;
271 0 ▶ my $cached = shift || 0;
Condition0%0/2
shift() || 0
l!l
00
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
272 0 my $now = $self->_cache_time();
273  
274 0 ▶ if ( $cached < ( $now - $caching_timeframe * 60 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached < $now - $caching_timeframe * 60) { } 0 0 0
275 0 return 1;
276 } else {
277 0 return 0;
278 }
279 }
280  
281 # this method encapsulates the time() method to be able to test
282 # with Test::MockObject and setting a fixed time
283 0 ▶sub _cache_time {
Subroutine _cache_time: not called CC=1 SCAR=6.9
284 0 my $self = shift;
285 0 return time();
286 }
287  
288 3 1;
289  
290 __END__
↑ Summary Com/Base.pm »