File Coverage

blib/lib/Ham/APRS/LastPacket.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
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 1     1   24305 use strict;
  1         2  
  1         44  
21 1     1   6 use warnings;
  1         2  
  1         34  
22 1     1   437 use XML::Simple;
  0            
  0            
23             use LWP::UserAgent;
24             use vars qw($VERSION);
25              
26             our $VERSION = '0.03';
27              
28             my $aprs_url = "http://aprsearch.net/xml/1.3/report.cgi?call=";
29             my $site_name = 'aprsearch.net/xml';
30             my $default_timeout = 10;
31              
32             sub new
33             {
34             my $class = shift;
35             my %args = @_;
36             my $self = {};
37             $self->{timeout} = $args{timeout} || $default_timeout;
38             if ($args{suppress_empty}) {
39             $self->{suppress_empty} = $args{suppress_empty};
40             } elsif (defined $args{suppress_empty} and !$args{suppress_empty}) {
41             $self->{suppress_empty} = '';
42             } elsif (exists $args{suppress_empty} and ! defined $args{suppress_empty}) {
43             $self->{suppress_empty} = undef;
44             } else {
45             $self->{suppress_empty} = '';
46             }
47             bless $self, $class;
48             return $self;
49             }
50              
51             sub set_callsign
52             {
53             my $self = shift;
54             my $callsign = shift;
55             if (!$callsign) {
56             $self->{is_error} = 1;
57             $self->{error_message} = "No callsign was provided";
58             return undef;
59             }
60             $callsign =~ tr/a-z/A-Z/;
61             $self->{callsign} = $callsign;
62             }
63              
64             sub get_callsign
65             {
66             my $self = shift;
67             return $self->{callsign};
68             }
69              
70             sub get_data
71             {
72             my $self = shift;
73             if (!$self->{callsign}) {
74             $self->{is_error} = 1;
75             $self->{error_message} = "Can not get data without a callsign";
76             return undef;
77             }
78             return _get_xml($self);
79             }
80              
81             sub is_error { my $self = shift; $self->{is_error} }
82             sub error_message { my $self = shift; $self->{error_message} }
83              
84             # -----------------------
85             # PRIVATE
86             # -----------------------
87              
88             sub _get_xml
89             {
90             my $self = shift;
91             my $ua = LWP::UserAgent->new( timeout=>$self->{timeout} );
92             $ua->agent("Perl Ham-APRS-LastPacket.pm $VERSION");
93             my $request = HTTP::Request->new('GET', $aprs_url.$self->{callsign});
94             my $response = $ua->request($request);
95             if (!$response->is_success) {
96             $self->{is_error} = 1;
97             $self->{error_message} = "Could not contact site: $site_name - ".HTTP::Status::status_message($response->code);
98             return undef;
99             }
100             my $content = $response->content;
101             chomp $content;
102             $content =~ s/(\r|\n)//g;
103              
104             my $xs = XML::Simple->new(
105             SuppressEmpty => $self->{suppress_empty}
106             );
107             my $data = $xs->XMLin($content);
108              
109             if (!$data->{position}) {
110             $self->{is_error} = 1;
111             $self->{error_message} = "$self->{callsign} was not found at $site_name\n";
112             return undef;
113             }
114             return $data;
115             }
116              
117             1;
118             __END__