Joker/Response.pm

filter: Statementstmt 24.1% 14 / 58 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 22.9% 20 / 87 CC 11 SCAR 45.1
CC
11
Cov
18%
parse
132.0
new
2.0
BEGIN
1.0
CRAP 91.1
↑ Summary Joker.pm »
1 package WWW::Domain::Registry::Joker::Response;
2  
3 # $CNsys: lib/WWW/Domain/Registry/Joker/Response.pm 2375 2008-10-17 10:25:08Z roam $
4  
5 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use WWW::Domain::Registry::Joker::Loggish;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our @ISA = qw(WWW::Domain::Registry::Joker::Loggish);
12  
13 2 our $VERSION = '0.04';
14  
15 =head1 NAME
16  
17 WWW::Domain::Registry::Joker::Response - parse a DMAPI response
18  
19 =head1 SYNOPSIS
20  
21 use WWW::Domain::Registry::Joker::Response;
22  
23 $r = new WWW::Domain::Registry::Joker::Response();
24 $r->parse($resp);
25 print "$r->{Proc-Id}: $r->{'Status-Code'} $r->{Status-Text}\n";
26  
27 =head1 DESCRIPTION
28  
29 The C<WWW::Domain::Registry::Joker::Response> class is a helper parser
30 for the HTTP responses returned by the Joker.com DMAPI. It examines
31 a response object, extracts the status and error flags, codes, and
32 descriptive messages, and makes them available as Perl object members.
33  
34 =head1 METHODS
35  
36 The C<WWW::Domain::Registry::Joker::Response> class defines the
37 following methods:
38  
39 =over 4
40  
41 =item new ()
42  
43 Initialize a C<WWW::Domain::Registry::Joker::Response> object.
44 No user-serviceable parameters inside.
45  
46 =cut
47  
48 sub new($ %)
49 {
50 0 ▶ my ($proto, %param) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
51 0 my $self;
52  
53 0 $self = WWW::Domain::Registry::Joker::Loggish::new($proto,
54 'code' => 1337,
55 'msg' => '(no status text)',
56 'status' => '(no status line)',
57 'success' => 0,
58 %param,
59 );
60 0 return $self;
61 }
62  
63 =item parse ( RESPONSE )
64  
65 Parse a C<HTTP::Response> from the DMAPI and store the result code,
66 message, error, etc. into the respective fields of the object.
67 In addition to the C<code>, C<msg>, C<status>, and C<success> members
68 described above, the C<parse()> method may also set the C<Version>,
69 C<Proc-Id>, C<Status-Text>, C<Status-Code>, C<Error>, and any other
70 result description members as listed in the DMAPI specification.
71  
72 =cut
73  
74 sub parse($ $)
75 {
76 0 ▶ my ($self, $resp) = @_;
Subroutine parse: not called CC=11 SCAR=48.8
Pod documented
77 0 my ($in_data, $var, $val);
78 0 my (@r, @data);
79  
80 0 ▶ die("No response object passed to ".ref($self)."->parse()\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $resp 0 0 0
81 unless defined($resp);
82 0 $self->debug("parsing a response - '".ref($resp)."'");
83 0 ▶ if (index(ref($resp), '::') == -1 || !$resp->isa('HTTP::Response')) {
Branch: 1 branch
BranchTrueFalseTotal
if (index(ref $resp, "::") == -1 or not $resp->isa("HTTP::Response")) 0 0 0
Condition0%0/3
index(ref $resp, "::") == -1 or not $resp->isa("HTTP::Response")
l!l&&r!l&&!r
000
MC/DC0%0/2
index(ref $resp, "::") == -1 or not $resp->isa("HTTP::Response")
index(ref $resp, "::") == -1 not $resp->isa("HTTP::Response")
Truth table0%0/3
A or not(B)
A= index(ref $resp, "::") == -1B= not $resp->isa("HTTP::Response")
ABresult
1X1
011
000
84 0 die("Not a HTTP response object in ".ref($self)."->parse()\n");
85 }
86  
87 0 $self->{'success'} = $resp->is_success();
88 0 $self->{'status'} = $resp->status_line();
89 0 undef $self->{'Status-Code'};
90 0 undef $self->{'Status-Text'};
91 0 @r = split("\n", $resp->content());
92 0 $self->debug(scalar(@r)." content lines");
93 0 foreach (@r) {
94 0 s/[\r\n]+$//;
95 0 ▶ if ($in_data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_data) 0 0 0
96 0 $self->debug("- data line $_");
97 0 push @data, $_;
98 0 next;
99 }
100 0 $self->debug("- line $_");
101 0 ▶ if ($_ eq '') {
Branch: 2 branches
BranchTrueFalseTotal
if ($_ eq '') { } 0 0 0
elsif (not /^([\w-]+):\s+(.*)$/) { } 0 0 0
102 0 $self->debug("- - end of header");
103 0 $in_data = 1;
104 0 next;
105 } elsif (!/^([\w-]+):\s+(.*)$/) {
106 0 $self->debug("- - bad format!");
107 0 next;
108 }
109 0 ($var, $val) = ($1, $2);
110 0 ▶ if (defined($self->{$var})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{$var}) { } 0 0 0
111 0 $self->{$var} .= " / $val";
112 } else {
113 0 $self->{$var} = $val;
114 }
115 }
116 0 $self->{'data'} = [ @data ];
117 0 ▶ if (defined($self->{'Status-Code'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Status-Code'}) { } 0 0 0
118 0 $self->{'code'} = $self->{'Status-Code'};
119 } else {
120 0 $self->{'code'} = 1337;
121 }
122 0 ▶ if (defined($self->{'Status-Text'})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'Status-Text'}) { } 0 0 0
123 0 $self->{'msg'} = $self->{'Status-Text'};
124 } else {
125 0 $self->{'msg'} = '(no status text)';
126 }
127 $self->debug("=== DMAPI response: is_success $self->{success} ".
128 "status line '$self->{status}', status code: $self->{code}, ".
129 "status text: '$self->{msg}', ".
130 0 "data lines ".scalar(@{$self->{'data'}})."\n");
131 0 return 1;
132 }
133  
134 =back
135  
136 =head1 EXAMPLES
137  
138 Create an object and parse an HTTP response:
139  
140 $r = new WWW::Domain::Registry::Joker::Response();
141 eval {
142 $r->parse($resp);
143 };
144 if ($@) {
145 print STDERR "Could not parse the DMAPI response: $@\n";
146 } elsif (!$r->{'success'}) {
147 print STDERR "DMAPI error: code $r->{code}, text $r->{msg}\n";
148 print STDERR "DMAPI error message: $r->{Error}\n"
149 if $r->{'Error'};
150 } else {
151 print "Successful DMAPI request: $r->{code} $r->{msg}\n";
152 print "Tracking process ID: $r->{Proc-ID}\n" if $r->{'Proc-ID'};
153 }
154  
155 =head1 ERRORS
156  
157 The C<parse()> method will die on invalid input:
158  
159 =over 4
160  
161 =item *
162  
163 no response parameter passed in;
164  
165 =item *
166  
167 the response parameter was not an C<HTTP::Response> or compatible object.
168  
169 =back
170  
171 If the response object is a valid DMAPI response, its C<success>, C<code>,
172 C<msg>, C<Error>, and other attributes are exposed as members of
173 the C<WWW::Domain::Registry::Joker::Response> object as shown above.
174  
175 =head1 SEE ALSO
176  
177 L<WWW::Domain::Registry::Joker>, L<HTTP::Response>
178  
179 I<https://joker.com/faq/category/39/22-dmapi.html> - the Joker.com DMAPI
180 documentation
181  
182 =head1 BUGS
183  
184 None known so far ;)
185  
186 =head1 HISTORY
187  
188 The C<WWW::Domain::Registry::Joker::Response> class was written by
189 Peter Pentchev in 2007.
190  
191 =head1 AUTHOR
192  
193 Peter Pentchev, E<lt>roam@ringlet.netE<gt>
194  
195 =head1 COPYRIGHT AND LICENSE
196  
197 Copyright (C) 2007 by Peter Pentchev
198  
199 This library is free software; you can redistribute it and/or modify
200 it under the same terms as Perl itself, either Perl version 5.8.8 or,
201 at your option, any later version of Perl 5 you may have available.
202  
203 =cut
204  
205 2 1;
↑ Summary Joker.pm »