blib/lib/Ham/Resources/HamQTH.pm

filter: Statementstmt 28.3% 45 / 159 Branchbran 7.1% 4 / 56 Conditioncond 2.2% 1 / 44 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 41.6% 10 / 24 Podpod 33.3% 4 / 12 totaltotal 20.1% 64 / 317 CC 46 SCAR 39.1
CC
46
Cov
19%
_get_content
156.0
get_list
72.0
_clean_response
56.0
CRAP 50.1
↑ Summary HamQTH.c »
1 package Ham::Resources::HamQTH;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 our $VERSION = '0.06';
12  
13 1 my $qth_url = "http://www.hamqth.com";
14 1 my $site_name = 'HamQTH XML Database service';
15 1 my $default_timeout = 10;
16 1 my $default_strip_html = 1;
17  
18  
19 sub new
20 1 ▶{
Subroutine new: called CC=4 SCAR=14.1
Pod documented
21 1 my $class = shift;
22 1 my %args = @_;
23 1 my $self = {};
24 1 bless $self, $class;
25  
26 1 $self->_set_agent;
27 1 $self->set_timeout($args{timeout});
28 1 ▶ $self->set_callsign($args{callsign}) if $args{callsign};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"callsign"} 0 1 1
29 1 ▶ $self->set_username($args{username}) if $args{username};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"username"} 0 1 1
30 1 ▶ $self->set_password($args{password}) if $args{password};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"password"} 0 1 1
31 1 $self->set_strip_html($args{strip_html_bio});
32 1 return $self;
33 }
34  
35 sub login
36 0 ▶{
Subroutine login: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my $self = shift;
38 0 my $url = "$qth_url/xml.php?u=".$self->{_username}."&p=".$self->{_password};
39 0 my $login = $self->_get_content($url);
40 }
41  
42 sub set_callsign
43 0 ▶{
Subroutine set_callsign: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my $self = shift;
45 0 my $callsign = shift;
46 0 $callsign =~ tr/a-z/A-Z/;
47 0 $self->{_callsign} = $callsign;
48 }
49  
50 sub set_username
51 0 ▶{
Subroutine set_username: not called CC=1 SCAR=6.9
Pod undocumented
52 0 my $self = shift;
53 0 my $username = shift;
54 0 $self->{_username} = $username;
55 }
56  
57 sub set_password
58 0 ▶{
Subroutine set_password: not called CC=1 SCAR=6.9
Pod undocumented
59 0 my $self = shift;
60 0 my $password = shift;
61 0 $self->{_password} = $password;
62 }
63  
64 sub set_strip_html
65 1 ▶{
Subroutine set_strip_html: called CC=2 SCAR=7.2
Pod undocumented
66 1 my $self = shift;
67 1 my $strip_html_bio = shift;
68
69 1 ▶ if (!$strip_html_bio)
Branch: 1 branch
BranchTrueFalseTotal
if (not $strip_html_bio) { } 1 0 1
70 {
71 1 $strip_html_bio = 0;
72 } else {
73 0 $strip_html_bio = $default_strip_html;
74 }
75
76 1 $self->{_strip_html} = $strip_html_bio;
77 }
78 sub set_key
79 0 ▶{
Subroutine set_key: not called CC=1 SCAR=6.9
Pod undocumented
80 0 my $self = shift;
81 0 my $key = shift;
82 0 $self->{_key} = $key;
83 }
84  
85 sub set_timeout
86 1 ▶{
Subroutine set_timeout: called CC=2 SCAR=7.6
Pod undocumented
87 1 my $self = shift;
88 1 ▶ my $timeout = shift || $default_timeout;
Condition33%1/3
shift() || $default_timeout
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $default_timeout
shift() $default_timeout
Truth table33%1/3
A || B
A= shift()B= $default_timeout
ABresult
1X1
011
000
89 1 $self->{_timeout} = $timeout;
90 }
91  
92 sub get_session
93 0 ▶{
Subroutine get_session: not called CC=1 SCAR=6.9
Pod undocumented
94 0 my $self = shift;
95 0 return $self->{session_id};
96 }
97  
98 sub get_list
99 0 ▶{
Subroutine get_list: not called CC=8 SCAR=42.8
Pod documented
100 0 my $self = shift;
101 0 my @tags;
102
103 0 ▶ if ($self->{error})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error'}) { } 0 0 0
104 {
105 0 push(@tags, $self->{error});
106 }
107 else
108 {
109 0 foreach my $tag (sort keys %{$self})
110 {
111 0 ▶ push(@tags, $tag) if ($tag !~ m/^_.+/i && $tag !~ m/@.+/i && $tag ne "HamQTH" && $tag ne "search" && $tag ne "session");
Branch: 1 branch
BranchTrueFalseTotal
if not $tag =~ /^_.+/i || $tag =~ /\@.+/i and $tag ne "HamQTH" and $tag ne "search" and $tag ne "session" 0 0 0
Condition0%0/12
not $tag =~ /^_.+/i || $tag =~ /\@.+/i and $tag ne "HamQTH" and $tag ne "search" and $tag ne "session"
!ll&&!rl&&r
000
not $tag =~ /^_.+/i || $tag =~ /\@.+/i and $tag ne "HamQTH" and $tag ne "search"
!ll&&!rl&&r
000
not $tag =~ /^_.+/i || $tag =~ /\@.+/i and $tag ne "HamQTH"
!ll&&!rl&&r
000
$tag =~ /^_.+/i || $tag =~ /\@.+/i
l!l&&r!l&&!r
000
MC/DC0%0/5
not $tag =~ /^_.+/i || $tag =~ /\@.+/i and $tag ne "HamQTH" and $tag ne "search" and $tag ne "session"
$tag =~ /^_.+/i $tag =~ /\@.+/i $tag ne "HamQTH" $tag ne "search" $tag ne "session"
Truth table0%0/6
not(A || B) and C and D and E
A= $tag =~ /^_.+/iB= $tag =~ /\@.+/iC= $tag ne "HamQTH"D= $tag ne "search"E= $tag ne "session"
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
112 }
113 }
114 0 return \@tags;
115 }
116  
117 sub get_bio
118 0 ▶{
Subroutine get_bio: not called CC=5 SCAR=34.0
Pod documented
119 0 my $self = shift;
120 0 my $result = {};
121
122 0 ▶ if (!$self->{_callsign})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_callsign"}) 0 0 0
123 {
124 0 $self->{error} = "Ops!! ... Without a callsign I can not search anything";
125 0 &_clean_response($self);
126 }
127  
128 0 &_check_session_id($self);
129
130 0 ▶ if (!$self->{_session_id})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_session_id"}) 0 0 0
131 {
132 0 $self->login;
133 }
134
135 0 ▶ if ($self->{error})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error'}) { } 0 0 0
136 {
137 0 $result->{error} = $self->{error};
138 }
139 else
140 {
141 0 my $url = "$qth_url/xml.php?id=".$self->{_session_id}."&callsign=".$self->{_callsign}."&prg=".$self->{_agent};
142 0 my $bio = $self->_get_content($url);
143 0 $url = "$qth_url/xml_bio.php?id=".$self->{_session_id}."&callsign=".$self->{_callsign}."&prg=".$self->{_agent}."&strip_html=".$self->{_strip_html};
144 0 $bio = $self->_get_content($url);
145
146 0 ▶ if (!$bio->{_session_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bio->{"_session_id"}) 0 0 0
147 0 $self->{error} = $bio->{error};
148 0 return undef;
149 }
150 0 $result = &_clean_response($bio);
151 }
152 0 return $result;
153 }
154  
155 sub get_dxcc
156 0 ▶{
Subroutine get_dxcc: not called CC=6 SCAR=37.4
Pod documented
157 0 my $self = shift;
158 0 my $result = {};
159
160 0 ▶ if (!$self->{_callsign})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_callsign"}) 0 0 0
161 {
162 0 $self->{error} = "Ops!! ... Without a callsign I can not search anything";
163 0 &_clean_response($self);
164 }
165  
166 0 &_check_session_id($self);
167
168 0 ▶ if (!$self->{_session_id})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_session_id"}) 0 0 0
169 {
170 0 $self->login;
171 }
172
173 0 ▶ if ($self->{error})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error'}) { } 0 0 0
174 {
175 0 $result->{error} = $self->{error};
176 }
177 else
178 {
179 #my $url = "$qth_url/xml.php?id=".$self->{_session_id}."&callsign=".$self->{_callsign}."&prg=".$self->{_agent};
180 #my $bio = $self->_get_content($url);
181 0 my $url = "$qth_url/dxcc.php?id=".$self->{_session_id}."&callsign=".$self->{_callsign}."&prg=".$self->{_agent};
182 0 my $bio = $self->_get_content($url);
183
184 0 ▶ if (!$bio->{_session_id}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bio->{"_session_id"}) 0 0 0
185 0 $self->{error} = $bio->{error};
186 0 return undef;
187 }
188
189 0 ▶ if ($self->{adif} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"adif"} == 0) 0 0 0
190 {
191 0 $self->{error} = "Not a valid DXCC";
192 0 &_clean_response($self);
193 }
194
195 0 $result = &_clean_response($bio);
196 }
197 0 return $result;
198 }
199  
200  
201 # -----------------------
202 # PRIVATE SUBS
203 # -----------------------
204  
205 sub _set_agent
206 1 ▶{
Subroutine _set_agent: called CC=1 SCAR=0.0
207 1 my $self = shift;
208 1 $self->{_agent} = "Ham-Resources-HamQTH-$VERSION";
209 }
210  
211 sub _get_content
212 0 ▶{
Subroutine _get_content: not called CC=12 SCAR=50.5
213 0 my ($self, $url) = @_;
214 0 my $ua = LWP::UserAgent->new( timeout=>$self->{_timeout} );
215 0 $ua->agent( $self->{_agent} );
216 0 my $request = HTTP::Request->new('GET', $url);
217 0 my $response = $ua->request($request);
218  
219 0 ▶ if (!$response->is_success)
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
220 {
221 0 $self->{error} = "Ops! ... ".$response->{_msg}." - ".HTTP::Status::status_message($response->code);
222 0 return undef;
223 }
224  
225 0 my $content = $response->content;
226 0 $content =~ s/&/&/; # this character will crash the parser
227 0 my $reader = XML::LibXML::Reader->new( encoding => 'UTF-8', string => $content );
228 0 my $tag_name;
229
230 0 ▶ while ($reader->read)
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->read) 0 0 0
231 {
232 0 ▶ $tag_name = $reader->name if ($reader->nodeType == 1);
Branch: 1 branch
BranchTrueFalseTotal
if $reader->nodeType == 1 0 0 0
233 0 ▶ $self->{_session_id} = $reader->value if ($reader->nodeType == 3 && $tag_name eq "session_id");
Branch: 1 branch
BranchTrueFalseTotal
if $reader->nodeType == 3 and $tag_name eq "session_id" 0 0 0
Condition0%0/3
$reader->nodeType == 3 and $tag_name eq "session_id"
!ll&&!rl&&r
000
MC/DC0%0/2
$reader->nodeType == 3 and $tag_name eq "session_id"
$reader->nodeType == 3 $tag_name eq "session_id"
Truth table0%0/3
A and B
A= $reader->nodeType == 3B= $tag_name eq "session_id"
ABresult
0X0
100
111
234 0 ▶ $self->{$tag_name} = $reader->value if ($reader->nodeType == 3 && $tag_name ne "session_id");
Branch: 1 branch
BranchTrueFalseTotal
if $reader->nodeType == 3 and $tag_name ne "session_id" 0 0 0
Condition0%0/3
$reader->nodeType == 3 and $tag_name ne "session_id"
!ll&&!rl&&r
000
MC/DC0%0/2
$reader->nodeType == 3 and $tag_name ne "session_id"
$reader->nodeType == 3 $tag_name ne "session_id"
Truth table0%0/3
A and B
A= $reader->nodeType == 3B= $tag_name ne "session_id"
ABresult
0X0
100
111
235 }
236 0 ▶ if (!$self->{error} || $self->{error} ne 'Callsign not found' || $self->{error} eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"error"} or $self->{"error"} ne "Callsign not found" or $self->{"error"} eq "") 0 0 0
Condition0%0/6
not $self->{"error"} or $self->{"error"} ne "Callsign not found" or $self->{"error"} eq ""
l!l&&r!l&&!r
000
not $self->{"error"} or $self->{"error"} ne "Callsign not found"
l!l&&r!l&&!r
000
MC/DC0%0/3
not $self->{"error"} or $self->{"error"} ne "Callsign not found" or $self->{"error"} eq ""
not $self->{"error"} $self->{"error"} ne "Callsign not found" $self->{"error"} eq ""
Truth table0%0/4
not(A) or B or C
A= not $self->{"error"}B= $self->{"error"} ne "Callsign not found"C= $self->{"error"} eq ""
ABCresult
1XX1
01X1
0011
0000
237  
238 0 ▶ $self->{link} = $qth_url."/".$self->{_callsign} if length($self->{_callsign}) > 3;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"_callsign"} > 3 0 0 0
239 }
240 0 &_save_session_id($self); # save SESSION ID
241 0 return $self;
242 }
243  
244 sub _clean_response
245 0 ▶{
Subroutine _clean_response: not called CC=7 SCAR=40.3
246 0 my $self = shift;
247 0 my $result = {};
248 0 foreach (sort keys %{$self})
249 {
250 0 ▶ if ($_ !~ m/^_.+/i && $_ !~ m/@.+/i && $_ ne "HamQTH" && $_ ne "search" && $_ ne "session" )
Branch: 1 branch
BranchTrueFalseTotal
if (not $_ =~ /^_.+/i || $_ =~ /\@.+/i and $_ ne "HamQTH" and $_ ne "search" and $_ ne "session") 0 0 0
Condition0%0/12
not $_ =~ /^_.+/i || $_ =~ /\@.+/i and $_ ne "HamQTH" and $_ ne "search" and $_ ne "session"
!ll&&!rl&&r
000
not $_ =~ /^_.+/i || $_ =~ /\@.+/i and $_ ne "HamQTH" and $_ ne "search"
!ll&&!rl&&r
000
not $_ =~ /^_.+/i || $_ =~ /\@.+/i and $_ ne "HamQTH"
!ll&&!rl&&r
000
$_ =~ /^_.+/i || $_ =~ /\@.+/i
l!l&&r!l&&!r
000
MC/DC0%0/5
not $_ =~ /^_.+/i || $_ =~ /\@.+/i and $_ ne "HamQTH" and $_ ne "search" and $_ ne "session"
$_ =~ /^_.+/i $_ =~ /\@.+/i $_ ne "HamQTH" $_ ne "search" $_ ne "session"
Truth table0%0/6
not(A || B) and C and D and E
A= $_ =~ /^_.+/iB= $_ =~ /\@.+/iC= $_ ne "HamQTH"D= $_ ne "search"E= $_ ne "session"
ABCDEresult
1XXXX0
01XXX0
000XX0
0010X0
001100
001111
251 {
252 0 $result->{$_} = $self->{$_};
253 }
254 }
255 0 return $result;
256 }
257  
258 sub _check_session_id
259 0 ▶{
Subroutine _check_session_id: not called CC=4 SCAR=30.0
260 0 my $self = shift;
261
262 0 ▶ if (-e "hamqth_session.id") {
Branch: 1 branch
BranchTrueFalseTotal
if (-e 'hamqth_session.id') { } 0 0 0
263 0 ▶ open my $record_load, '<', 'hamqth_session.id' or $self->{error} = "Cannot open filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $record_load, "<", "hamqth_session.id" or $self->{"error"} = "Cannot open filename: $!" 0 0 0
264 0 ▶ if (not $self->{error}) { return; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"error"}) 0 0 0
265 0 ($self->{_session_id},$self->{_timestamp}) = split("-", $record_load);
266 0 close $record_load;
267 0 &_check_timestamp($self);
268 }
269 else
270 {
271 0 $self->login;
272 }
273 }
274  
275 sub _check_timestamp
276 0 ▶{
Subroutine _check_timestamp: not called CC=4 SCAR=30.0
277 0 my $self = shift;
278 0 my $time_actual_epoch = time();
279 0 ▶ my $timestamp_epoch = $self->{_timestamp} || 0;
Condition0%0/2
$self->{'_timestamp'} || 0
l!l
00
MC/DC0%0/1
$self->{'_timestamp'} || 0
$self->{'_timestamp'}
Truth table0%0/2
A
A= $self->{'_timestamp'}
Aresult
11
00
280 0 my $timestamp_epoch_plus_1h = $timestamp_epoch + (1*60*60);
281  
282 0 ▶ if ($time_actual_epoch > $timestamp_epoch_plus_1h or $time_actual_epoch < $timestamp_epoch)
Branch: 1 branch
BranchTrueFalseTotal
if ($time_actual_epoch > $timestamp_epoch_plus_1h or $time_actual_epoch < $timestamp_epoch) 0 0 0
Condition0%0/3
$time_actual_epoch > $timestamp_epoch_plus_1h or $time_actual_epoch < $timestamp_epoch
l!l&&r!l&&!r
000
MC/DC0%0/2
$time_actual_epoch > $timestamp_epoch_plus_1h or $time_actual_epoch < $timestamp_epoch
$time_actual_epoch > $timestamp_epoch_plus_1h $time_actual_epoch < $timestamp_epoch
Truth table0%0/3
A or B
A= $time_actual_epoch > $timestamp_epoch_plus_1hB= $time_actual_epoch < $timestamp_epoch
ABresult
1X1
011
000
283 {
284 0 $self->login;
285 }
286 }
287
288 sub _save_session_id
289 0 ▶{
Subroutine _save_session_id: not called CC=2 SCAR=17.9
290 0 my $self = shift;
291  
292 0 ▶ open my $record_session, '>', 'hamqth_session.id' or $self->{error} = "Cannot open filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $record_session, ">", "hamqth_session.id" or $self->{"error"} = "Cannot open filename: $!" 0 0 0
293 0 my $my_session = $self->{_session_id}."-".time();
294 0 print $record_session $my_session;
295 0 close $record_session;
296 0 return $self;
297 }
298  
299  
300 1 1;
301 __END__
↑ Summary HamQTH.c »