blib/lib/Ham/APRS/LastPacket.pm

filter: Statementstmt 28.5% 20 / 70 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 41.6% 5 / 12 Podpod 100% 6 / 6 totaltotal 26.5% 31 / 117 CC 11 SCAR 30.5
CC
11
Cov
22%
new
56.0
_get_xml
12.0
set_callsign
6.0
CRAP 21.0
↑ Summary
1 package Ham::APRS::LastPacket;
2  
3 # --------------------------------------------------------------------------
4 # Ham::APRS::LastPacket - A simple interface to retrieve the most recent
5 # packet data for a station from APRS-IS.
6 #
7 # Copyright (c) 2008-2010 Brad McConahay N8QQ.
8 # Cincinnat, Ohio USA
9 #
10 # This module is free software; you can redistribute it and/or
11 # modify it under the terms of the Artistic License 2.0. For
12 # details, see the full text of the license in the file LICENSE.
13 #
14 # This program is distributed in the hope that it will be
15 # useful, but it is provided "as is" and without any express
16 # or implied warranties. For details, see the full text of
17 # the license in the file LICENSE.
18 # --------------------------------------------------------------------------
19  
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 our $VERSION = '0.03';
27  
28 2 my $aprs_url = "http://aprsearch.net/xml/1.3/report.cgi?call=";
29 2 my $site_name = 'aprsearch.net/xml';
30 2 my $default_timeout = 10;
31  
32 sub new
33 0 ▶{
Subroutine new: not called CC=7 SCAR=40.3
Pod documented
34 0 my $class = shift;
35 0 my %args = @_;
36 0 my $self = {};
37 0 ▶ $self->{timeout} = $args{timeout} || $default_timeout;
Condition0%0/3
$args{'timeout'} || $default_timeout
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{'timeout'} || $default_timeout
$args{'timeout'} $default_timeout
Truth table0%0/3
A || B
A= $args{'timeout'}B= $default_timeout
ABresult
1X1
011
000
38 0 ▶ if ($args{suppress_empty}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($args{'suppress_empty'}) { } 0 0 0
elsif (defined $args{'suppress_empty'} and not $args{'suppress_empty'}) { } 0 0 0
elsif (exists $args{'suppress_empty'} and not defined $args{'suppress_empty'}) { } 0 0 0
39 0 $self->{suppress_empty} = $args{suppress_empty};
40 ▶ } elsif (defined $args{suppress_empty} and !$args{suppress_empty}) {
Condition0%0/3
defined $args{"suppress_empty"} and not $args{"suppress_empty"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"suppress_empty"} and not $args{"suppress_empty"}
defined $args{"suppress_empty"} not $args{"suppress_empty"}
Truth table0%0/3
A and not(B)
A= defined $args{"suppress_empty"}B= not $args{"suppress_empty"}
ABresult
0X0
100
111
41 0 $self->{suppress_empty} = '';
42 ▶ } elsif (exists $args{suppress_empty} and ! defined $args{suppress_empty}) {
Condition0%0/3
exists $args{"suppress_empty"} and not defined $args{"suppress_empty"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $args{"suppress_empty"} and not defined $args{"suppress_empty"}
exists $args{"suppress_empty"} not defined $args{"suppress_empty"}
Truth table0%0/3
A and not(B)
A= exists $args{"suppress_empty"}B= not defined $args{"suppress_empty"}
ABresult
0X0
100
111
43 0 $self->{suppress_empty} = undef;
44 } else {
45 0 $self->{suppress_empty} = '';
46 }
47 0 bless $self, $class;
48 0 return $self;
49 }
50  
51 sub set_callsign
52 0 ▶{
Subroutine set_callsign: not called CC=2 SCAR=17.9
Pod documented
53 0 my $self = shift;
54 0 my $callsign = shift;
55 0 ▶ if (!$callsign) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($callsign) 0 0 0
56 0 $self->{is_error} = 1;
57 0 $self->{error_message} = "No callsign was provided";
58 0 return undef;
59 }
60 0 $callsign =~ tr/a-z/A-Z/;
61 0 $self->{callsign} = $callsign;
62 }
63  
64 sub get_callsign
65 0 ▶{
Subroutine get_callsign: not called CC=1 SCAR=6.9
Pod documented
66 0 my $self = shift;
67 0 return $self->{callsign};
68 }
69  
70 sub get_data
71 0 ▶{
Subroutine get_data: not called CC=2 SCAR=17.9
Pod documented
72 0 my $self = shift;
73 0 ▶ if (!$self->{callsign}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"callsign"}) 0 0 0
74 0 $self->{is_error} = 1;
75 0 $self->{error_message} = "Can not get data without a callsign";
76 0 return undef;
77 }
78 0 return _get_xml($self);
79 }
80  
81 0 ▶sub is_error { my $self = shift; $self->{is_error} }
Subroutine is_error: not called CC=1 SCAR=6.9
Pod documented
82 0 ▶sub error_message { my $self = shift; $self->{error_message} }
Subroutine error_message: not called CC=1 SCAR=6.9
Pod documented
83  
84 # -----------------------
85 # PRIVATE
86 # -----------------------
87  
88 sub _get_xml
89 0 ▶{
Subroutine _get_xml: not called CC=3 SCAR=24.8
90 0 my $self = shift;
91 0 my $ua = LWP::UserAgent->new( timeout=>$self->{timeout} );
92 0 $ua->agent("Perl Ham-APRS-LastPacket.pm $VERSION");
93 0 my $request = HTTP::Request->new('GET', $aprs_url.$self->{callsign});
94 0 my $response = $ua->request($request);
95 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
96 0 $self->{is_error} = 1;
97 0 $self->{error_message} = "Could not contact site: $site_name - ".HTTP::Status::status_message($response->code);
98 0 return undef;
99 }
100 0 my $content = $response->content;
101 0 chomp $content;
102 0 $content =~ s/(\r|\n)//g;
103  
104 my $xs = XML::Simple->new(
105 SuppressEmpty => $self->{suppress_empty}
106 0 );
107 0 my $data = $xs->XMLin($content);
108  
109 0 ▶ if (!$data->{position}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($data->{"position"}) 0 0 0
110 0 $self->{is_error} = 1;
111 0 $self->{error_message} = "$self->{callsign} was not found at $site_name\n";
112 0 return undef;
113 }
114 0 return $data;
115 }
116  
117 2 1;
118 __END__
↑ Summary