Ares.pm

filter: Statementstmt 56.0% 37 / 66 Branchbran 8.3% 1 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 75.0% 9 / 12 Podpod 100% 5 / 5 totaltotal 54.7% 52 / 95 SCAR 28.1
CC
8
Cov
49%
_get_page
20.0
get_xml
12.0
get
6.0
CRAP 16.6
↑ Summary Ares/Standard.pm »
1 package WebService::Ares;
2  
3 # Pragmas.
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 # Modules.
8 6 ▶use Class::Utils qw(set_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Error::Pure qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Version.
14 6 our $VERSION = 0.01;
15  
16 # Constructor.
17 sub new {
18 5 ▶ my ($class, @params) = @_;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
19  
20 # Create object.
21 5 my $self = bless {}, $class;
22  
23 # User agent.
24 5 $self->{'agent'} = 'WebService::Ares/'.$VERSION;
25  
26 # Debug.
27 5 $self->{'debug'} = 0;
28  
29 # Params.
30 5 set_params($self, @params);
31  
32 # Commands.
33 3 $self->{'commands'} = {
34 'standard' => {
35 'attr' => [
36 'ic',
37 ],
38 'method' => 'GET',
39 'url' => 'http://wwwinfo.mfcr.cz/cgi-bin/ares'.
40 '/darv_std.cgi',
41 },
42 };
43  
44 # Error string.
45 3 $self->{'error'} = undef;
46  
47 # User agent.
48 3 $self->{'ua'} = LWP::UserAgent->new;
49 3 $self->{'ua'}->agent($self->{'agent'});
50  
51 # Object.
52 3 return $self;
53 }
54  
55 # Get web service commands.
56 sub commands {
57 1 ▶ my $self = shift;
Subroutine commands: called CC=1 SCAR=0.0
Pod documented
58 1 return sort keys %{$self->{'commands'}};
59 }
60  
61 # Get error.
62 sub error {
63 1 ▶ my ($self, $clean) = @_;
Subroutine error: called CC=2 SCAR=7.4
Pod documented
64 1 my $error = $self->{'error'};
65 1 ▶ if ($clean) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean) 0 1 1
66 0 $self->{'error'} = undef;
67 }
68 1 return $error;
69 }
70  
71 # Get data.
72 sub get {
73 0 ▶ my ($self, $command, $def_hr) = @_;
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
74  
75 # Get XML data.
76 0 my $data = $self->get_xml($command, $def_hr);
77  
78 # Parse XML.
79 0 my $data_hr;
80 0 ▶ if ($command eq 'standard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "standard") 0 0 0
81 0 require Ares::Standard;
82 0 $data_hr = Ares::Standard::parse($data);
83 }
84  
85 # Result.
86 0 return $data_hr;
87 }
88  
89 # Get XML file.
90 sub get_xml {
91 0 ▶ my ($self, $command, $def_hr) = @_;
Subroutine get_xml: not called CC=3 SCAR=24.8
Pod documented
92  
93 # Command structure.
94 0 my $c_hr = $self->{'commands'}->{$command};
95  
96 # Create url.
97 0 my $url = $c_hr->{'url'};
98 0 foreach my $key (keys %{$def_hr}) {
99 # TODO Control
100 # TODO Better create.
101 0 ▶ if ($key eq 'ic') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "ic") 0 0 0
102 0 $url .= '?ico='.$def_hr->{$key};
103 }
104 }
105  
106 # Get XML data.
107 0 return $self->_get_page($c_hr->{'method'}, $url);
108 }
109  
110 # Get page.
111 sub _get_page {
112 0 ▶ my ($self, $method, $url) = @_;
Subroutine _get_page: not called CC=4 SCAR=30.0
113  
114 # Debug.
115 0 ▶ if ($self->{'debug'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
116 0 print "Method: $method\n";
117 0 print "URL: $url\n";
118 }
119  
120 # Request.
121 0 my $req;
122 0 ▶ if ($method eq 'GET') {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'GET') { } 0 0 0
123 0 $req = HTTP::Request->new('GET' => $url);
124 } else {
125 0 err "Method '$method' is unimplemenited.";
126 }
127  
128 # Response.
129 0 my $res = $self->{'ua'}->request($req);
130  
131 # Get page.
132 0 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 0 0 0
133 0 return $res->content;
134 } else {
135 0 $self->{'error'} = $res->status_line;
136 0 return;
137 }
138 }
139  
140 6 1;
141  
142 __END__
↑ Summary Ares/Standard.pm »