Com/Location.pm

filter: Statementstmt 21.8% 31 / 142 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 37.0% 10 / 27 Podpod 93.7% 15 / 16 totaltotal 25.4% 56 / 220 CC 15 SCAR 20.1
CC
15
Cov
18%
new
30.0
refresh
12.0
units
6.0
CRAP 7.5
« Com/Forecast.pm ↑ Summary Com/DayPart.pm »
1 package Weather::Com::Location;
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 warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Weather::Com::Cached;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Weather::Com::Units;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Weather::Com::CurrentConditions;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Weather::Com::Forecast;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use base "Weather::Com::Cached";
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 our $VERSION = sprintf "%d.%03d", q$Revision: 1.10 $ =~ /(\d+)/g;
15  
16 #------------------------------------------------------------------------
17 # Constructor
18 #------------------------------------------------------------------------
19 0 ▶sub new {
Subroutine new: not called CC=5 SCAR=34.0
Pod documented
20 0 my $proto = shift;
21 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
22 0 my %parameters = ();
23  
24 # parameters provided by new method
25 0 ▶ if ( ref( $_[0] ) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 0 0 0
26 0 %parameters = %{ $_[0] };
27 } else {
28 0 %parameters = @_;
29 }
30  
31 0 ▶ unless ( $parameters{location_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parameters{"location_id"}) 0 0 0
32 0 die "You need to provide a location id!\n";
33 }
34  
35 # set some parameters to sensible values for a pure location
36 # object
37 0 $parameters{current} = 0;
38 0 $parameters{forecast} = 0;
39 0 $parameters{links} = 0;
40  
41 # creating the SUPER instance
42 0 my $self = $class->SUPER::new( \%parameters );
43  
44 0 $self->{ID} = $parameters{location_id};
45 0 $self->{NAME} = $parameters{location_name};
46 0 $self->{DEBUG} = $parameters{debug};
47  
48 # the weather data will be initialized when the first call on
49 # data is performed
50 0 $self->{HEAD} = undef;
51 0 $self->{WEATHER} = undef;
52 0 $self->{CONDITIONS} = undef;
53 0 $self->{FORECAST} = undef;
54 0 $self->{LOCALTIME} = undef;
55 0 $self->{SUNRISE} = undef;
56 0 $self->{SUNSET} = undef;
57  
58 # last update will be used to trigger automatic refresh of
59 # location data
60 0 $self->{LSUP} = time();
61  
62 0 bless( $self, $class );
63  
64 # init object, add timezone to ARGS
65 0 ▶ $self->{ARGS}->{lang} = $parameters{language} || 'en';
Condition0%0/2
$parameters{'language'} || 'en'
l!l
00
MC/DC0%0/1
$parameters{'language'} || 'en'
$parameters{'language'}
Truth table0%0/2
A
A= $parameters{'language'}
Aresult
11
00
66  
67 0 return $self;
68 } # end new()
69  
70 #------------------------------------------------------------------------
71 # refresh weather data
72 #------------------------------------------------------------------------
73 # this calls refresh if weather data is not initialized yet
74 0 ▶sub refresh {
Subroutine refresh: not called CC=3 SCAR=24.8
Pod undocumented
75 0 my $self = shift;
76 0 ▶ if ( !$self->{WEATHER} || $self->_update ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"WEATHER"} or $self->_update) 0 0 0
Condition0%0/3
not $self->{"WEATHER"} or $self->_update
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->{"WEATHER"} or $self->_update
not $self->{"WEATHER"} $self->_update
Truth table0%0/3
not(A) or B
A= not $self->{"WEATHER"}B= $self->_update
ABresult
1X1
011
000
77 0 $self->{WEATHER} = $self->get_weather( $self->{ID} );
78 0 $self->_debug("Weather data refreshed!");
79 }
80 0 return 1;
81 }
82  
83 #------------------------------------------------------------------------
84 # access location data
85 #------------------------------------------------------------------------
86 0 ▶sub id {
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
87 0 my $self = shift;
88 0 return $self->{ID};
89 }
90  
91 0 ▶sub name {
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
92 0 my $self = shift;
93 0 return $self->{NAME};
94 }
95  
96 0 ▶sub units {
Subroutine units: not called CC=2 SCAR=17.9
Pod documented
97 0 my $self = shift;
98 0 $self->refresh();
99  
100 0 ▶ unless ( $self->{HEAD} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"HEAD"}) 0 0 0
101 0 $self->{HEAD} = Weather::Com::Units->new();
102 }
103 0 $self->{HEAD}->update( $self->{WEATHER}->{head} );
104 0 return $self->{HEAD};
105 }
106  
107 0 ▶sub timezone {
Subroutine timezone: not called CC=2 SCAR=17.9
Pod documented
108 0 my $self = shift;
109  
110 0 ▶ unless ( $self->{WEATHER} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"WEATHER"}) 0 0 0
111 0 $self->{WEATHER} = $self->get_weather( $self->{ID} );
112 }
113 0 $self->{ARGS}->{zone} = $self->{WEATHER}->{loc}->{zone};
114 0 return $self->{ARGS}->{zone};
115 }
116  
117 0 ▶sub latitude {
Subroutine latitude: not called CC=1 SCAR=6.9
Pod documented
118 0 my $self = shift;
119 0 $self->refresh();
120 0 return $self->{WEATHER}->{loc}->{lat};
121 }
122  
123 0 ▶sub longitude {
Subroutine longitude: not called CC=1 SCAR=6.9
Pod documented
124 0 my $self = shift;
125 0 $self->refresh();
126 0 return $self->{WEATHER}->{loc}->{lon};
127 }
128  
129 # localtime will be calculated because it does not make
130 # any sense to used a cached time as current local time of
131 # some location
132 0 ▶sub localtime {
Subroutine localtime: not called CC=2 SCAR=17.9
Pod documented
133 0 my $self = shift;
134 0 $self->refresh();
135  
136 0 ▶ unless ( $self->{LOCALTIME} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"LOCALTIME"}) 0 0 0
137 0 $self->{LOCALTIME} = Weather::Com::DateTime->new( $self->timezone() );
138 }
139  
140 0 return $self->{LOCALTIME};
141 }
142  
143 0 ▶sub localtime_ampm {
Subroutine localtime_ampm: not called CC=1 SCAR=6.9
Pod documented
144 0 carp("Use of deprecated method 'localtime_ampm()'!");
145 0 carp("Please use 'localtime()->time_ampm()' instead.");
146  
147 0 my $self = shift;
148 0 $self->refresh();
149  
150 0 return $self->localtime()->time_ampm();
151 }
152  
153 0 ▶sub sunrise {
Subroutine sunrise: not called CC=2 SCAR=17.9
Pod documented
154 0 my $self = shift;
155 0 $self->refresh();
156  
157 0 ▶ unless ( $self->{SUNRISE} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"SUNRISE"}) 0 0 0
158 0 $self->{SUNRISE} = Weather::Com::DateTime->new( $self->timezone() );
159 }
160  
161 0 $self->{SUNRISE}->set_time( $self->{WEATHER}->{loc}->{sunr} );
162 0 return $self->{SUNRISE};
163 }
164  
165 0 ▶sub sunrise_ampm {
Subroutine sunrise_ampm: not called CC=1 SCAR=6.9
Pod documented
166 0 carp("Use of deprecated method 'sunrise_ampm()'!");
167 0 carp("Please use 'sunrise()->time_ampm()' instead.");
168  
169 0 my $self = shift;
170 0 $self->refresh();
171  
172 0 return $self->sunrise()->time_ampm();
173 }
174  
175 0 ▶sub sunset {
Subroutine sunset: not called CC=2 SCAR=17.9
Pod documented
176 0 my $self = shift;
177 0 $self->refresh();
178  
179 0 ▶ unless ( $self->{SUNSET} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"SUNSET"}) 0 0 0
180 0 $self->{SUNSET} = Weather::Com::DateTime->new( $self->timezone() );
181 }
182  
183 0 $self->{SUNSET}->set_time( $self->{WEATHER}->{loc}->{suns} );
184 0 return $self->{SUNSET};
185 }
186  
187 0 ▶sub sunset_ampm {
Subroutine sunset_ampm: not called CC=1 SCAR=6.9
Pod documented
188 0 carp("Use of deprecated method 'sunset_ampm()'!");
189 0 carp("Please use 'sunset()->time_ampm()' instead.");
190  
191 0 my $self = shift;
192 0 $self->refresh();
193  
194 0 return $self->sunset()->time_ampm();
195 }
196  
197 0 ▶sub current_conditions {
Subroutine current_conditions: not called CC=2 SCAR=17.9
Pod documented
198 0 my $self = shift;
199 0 $self->refresh();
200  
201 0 ▶ unless ( $self->{CONDITIONS} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"CONDITIONS"}) 0 0 0
202 $self->{CONDITIONS} =
203 0 Weather::Com::CurrentConditions->new( $self->{ARGS} );
204 }
205 0 return $self->{CONDITIONS};
206 }
207  
208 0 ▶sub forecast {
Subroutine forecast: not called CC=2 SCAR=17.9
Pod documented
209 0 my $self = shift;
210 0 $self->refresh();
211  
212 0 ▶ unless ( $self->{FORECAST} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"FORECAST"}) 0 0 0
213 0 $self->{FORECAST} = Weather::Com::Forecast->new( $self->{ARGS} );
214 }
215 0 return $self->{FORECAST};
216 }
217  
218 #------------------------------------------------------------------------
219 # internal methods go here
220 #------------------------------------------------------------------------
221 0 ▶sub _update {
Subroutine _update: not called CC=2 SCAR=17.9
222 0 my $self = shift;
223  
224 # idea for check if now is one or more days after last update:
225 # 1. transform last update to 00:00:00 of last updated date in
226 # local time of location
227 # 2. get 00:00:00 of today in local time of location
228 # If both in epoc are equal, no update is needed, else we'll get
229 # the new location information.
230 0 my @lsup = gmtime( $self->timezone() * 3600 + $self->{LSUP} );
231 0 $lsup[0] = 0;
232 0 $lsup[1] = 0;
233 0 $lsup[2] = 0;
234 0 my $local_epoc_lsup = timegm(@lsup);
235  
236 0 $self->{LSUP} = time();
237 0 my @now = gmtime( time() + ( $self->timezone() * 3600 ) );
238 0 $now[0] = 0;
239 0 $now[1] = 0;
240 0 $now[2] = 0;
241 0 my $local_epoc_now = timegm(@now);
242  
243 0 ▶ if ( $local_epoc_now > $local_epoc_lsup ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($local_epoc_now > $local_epoc_lsup) 0 0 0
244 0 $self->_debug("should refresh location cache...\n");
245 0 return 1;
246 }
247  
248 0 return 0;
249 }
250  
251 1 1;
252  
253 __END__
« Com/Forecast.pm ↑ Summary Com/DayPart.pm »