blib/lib/Ham/Reference/Callook.pm

filter: Statementstmt 26.5% 21 / 79 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 33.3% 5 / 15 Podpod 100% 6 / 6 totaltotal 27.1% 32 / 118 CC 7 SCAR 15.5
CC
7
Cov
23%
listing
12.0
timeout
6.0
type
6.0
CRAP 4.7
↑ Summary
1 package Ham::Reference::Callook;
2  
3 # --------------------------------------------------------------------------
4 # Ham::Reference::Callook - An interface to the Callook.info Database Service
5 #
6 # Copyright (c) 2010-2011 Brad McConahay N8QQ.
7 # Cincinnati, Ohio USA
8 # --------------------------------------------------------------------------
9  
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '0.02';
17  
18 2 my $callook_url = "http://callook.info/";
19 2 my $site_name = 'Callook.info';
20 2 my $default_timeout = 10;
21 2 my $default_type = 'xml';
22  
23 sub new
24 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
25 0 my $class = shift;
26 0 my %args = @_;
27 0 my $self = {};
28 0 bless $self, $class;
29 0 $self->_clear_errors;
30 0 $self->_set_agent;
31 0 $self->timeout($args{timeout});
32 0 $self->type($args{type});
33 0 return($self);
34 }
35  
36 sub listing
37 0 ▶{
Subroutine listing: not called CC=3 SCAR=24.8
Pod documented
38 0 my $self = shift;
39 0 my $callsign = shift;
40 0 $self->_clear_errors;
41 0 $callsign =~ tr/a-z/A-Z/;
42 0 $self->{_callsign} = $callsign;
43 0 my $url = "$callook_url/$self->{_callsign}/$self->{_type}";
44 0 ▶ if ($self->{_type} eq 'text')
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'_type'} eq 'text') { } 0 0 0
elsif ($self->{'_type'} eq 'xml') { } 0 0 0
45 {
46 0 $self->{_data} = $self->_get_http($url);
47 }
48 elsif ($self->{_type} eq 'xml')
49 {
50 0 $self->{_data} = $self->_get_xml($url);
51 }
52 else # unknown type
53 {
54 0 $self->{is_error} = 1;
55 0 $self->{error_message} = "Unknown type: $self->{_type}";
56 0 $self->{_data} = undef;
57 }
58 0 return $self->{_data};
59 }
60  
61 sub timeout
62 0 ▶{
Subroutine timeout: not called CC=2 SCAR=17.9
Pod documented
63 0 my $self = shift;
64 0 ▶ my $timeout = shift || $default_timeout;
Condition0%0/3
shift() || $default_timeout
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $default_timeout
shift() $default_timeout
Truth table0%0/3
A || B
A= shift()B= $default_timeout
ABresult
1X1
011
000
65 0 $self->{_timeout} = $timeout;
66 }
67  
68 sub type
69 0 ▶{
Subroutine type: not called CC=2 SCAR=17.9
Pod documented
70 0 my $self = shift;
71 0 ▶ my $type = shift || $default_type;
Condition0%0/3
shift() || $default_type
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $default_type
shift() $default_type
Truth table0%0/3
A || B
A= shift()B= $default_type
ABresult
1X1
011
000
72 0 $self->{_type} = $type;
73 }
74  
75 0 ▶sub is_error { my $self = shift; $self->{is_error} }
Subroutine is_error: not called CC=1 SCAR=6.9
Pod documented
76 0 ▶sub error_message { my $self = shift; $self->{error_message} }
Subroutine error_message: not called CC=1 SCAR=6.9
Pod documented
77  
78  
79 # -----------------------
80 # PRIVATE
81 # -----------------------
82  
83 sub _set_agent
84 0 ▶{
Subroutine _set_agent: not called CC=1 SCAR=6.9
85 0 my $self = shift;
86 0 $self->{_agent} = "Perl-Module-Ham-Reference-Callook-$VERSION";
87 }
88  
89 sub _get_xml
90 0 ▶{
Subroutine _get_xml: not called CC=2 SCAR=17.9
91 0 my $self = shift;
92 0 my $url = shift;
93 0 my $content = $self->_get_http($url);
94 0 ▶ return undef if $self->{is_error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"is_error"} 0 0 0
95 0 chomp $content;
96 0 $content =~ s/(\r|\n)//g;
97 0 my $xs = XML::Simple->new( SuppressEmpty => 0 );
98 0 my $data = $xs->XMLin($content);
99 0 return $data;
100 }
101  
102 sub _get_http
103 0 ▶{
Subroutine _get_http: not called CC=2 SCAR=17.9
104 0 my $self = shift;
105 0 my $url = shift;
106 0 $self->_clear_errors;
107 0 my $ua = LWP::UserAgent->new( timeout=>$self->{_timeout} );
108 0 $ua->agent( $self->{_agent} );
109 0 my $request = HTTP::Request->new('GET', $url);
110 0 my $response = $ua->request($request);
111 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
112 0 $self->{is_error} = 1;
113 0 $self->{error_message} = "Could not contact $site_name - ".HTTP::Status::status_message($response->code);
114 0 return undef;
115 }
116 0 return $response->content;
117 }
118  
119 sub _clear_errors
120 0 ▶{
Subroutine _clear_errors: not called CC=1 SCAR=6.9
121 0 my $self = shift;
122 0 $self->{is_error} = 0;
123 0 $self->{error_message} = '';
124 }
125  
126  
127 2 1;
128 __END__
↑ Summary