blib/lib/Dancer/Plugin/RPC/XML.pm

filter: Statementstmt 68.1% 45 / 66 Branchbran 43.7% 7 / 16 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 76.9% 10 / 13 Podpod n/a totaltotal 65.0% 65 / 100 CC 13 SCAR 25.3
CC
13
Cov
64%
__ANON__
31.0
xmlrpc
9.5
__ANON__
2.0
CRAP 12.5
↑ Summary
1 package Dancer::Plugin::RPC::XML;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Dancer ':syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Dancer::Exception ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Dancer::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use RPC::XML;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use RPC::XML::ParserFactory;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $VERSION = '0.06';
13  
14 3 register 'xmlrpc' => \&xmlrpc;
15 3 register 'xmlrpc_fault' => \&xmlrpc_fault;
16  
17 3 ▶hook before => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
18 3 ▶ if (request->is_post) {
Branch: 1 branch
BranchTrueFalseTotal
if (request()->is_post) 2 1 3
19 2 content_type('text/xml');
20 }
21 3 };
22  
23 1 ▶sub xmlrpc {
Subroutine xmlrpc: called CC=5 SCAR=22.5
24 1 my ($pattern, @rest) = @_;
25
26 1 my $code;
27 1 for my $e (@rest) {
28 1 ▶ $code = $e if ref($e) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $e eq "CODE" 1 0 1
29 }
30  
31 2 ▶ my $rpcxml_route = sub {
Subroutine __ANON__: called CC=8 SCAR=34.3
32 2 ▶ if ( not request->is_post ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (request()->is_post) 0 2 2
33 0 ▶ pass and return 0;
Branch: 1 branch
BranchTrueFalseTotal
pass() and return 0 0 0 0
34 }
35
36 # disable layout
37 2 my $layout = setting('layout');
38 2 setting('layout' => undef);
39
40 # parse the request body
41 2 my $xml = request->body;
42
43 2 ▶ return RPC::XML::response->new(
Branch: 1 branch
BranchTrueFalseTotal
if not $xml or $xml =~ /^\s?$/ 1 1 2
Condition66%2/3
not $xml or $xml =~ /^\s?$/
l!l&&r!l&&!r
101
MC/DC50%1/2
not $xml or $xml =~ /^\s?$/
not $xml $xml =~ /^\s?$/
Truth table66%2/3
not(A) or B
A= not $xmlB= $xml =~ /^\s?$/
ABresult
1X1
011
000
44 RPC::XML::fault->new(-1, "XML parse failure - empty"))->as_string if ( !$xml || $xml =~ /^\s?$/ );
45
46 1 my $reqobj = RPC::XML::ParserFactory->new()->parse( $xml );
47
48 0 ▶ if ( not ref $reqobj ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $reqobj) 0 0 0
49 0 return RPC::XML::response->new(
50 RPC::XML::fault->new(-2, "XML parse failure: $reqobj"))->as_string;
51 }
52
53 0 my @data = @{$reqobj->args};
54 0 my $name = $reqobj->name;
55
56 0 my @values = ();
57 0 for my $v (@data) { push @values, $v->value; };
58
59 # stuff data into params
60 0 request->_set_route_params( { 'method' => $name, 'data' => \@values } );
61
62 # call the code
63 0 ▶ my $response = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
64 0 $code->();
65 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
66 0 my $e = $_;
67 0 setting('layout' => $layout);
68 0 die $e;
69 0 };
70  
71 # re-enable layout
72 0 setting('layout' => $layout);
73  
74 # wrap the response in xml with RPC::XML
75 0 ▶ if ( ref $response ne 'RPC::XML::response' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $response ne 'RPC::XML::response') { } 0 0 0
76 0 return RPC::XML::response->new( $response )->as_string;
77 }
78 else {
79 0 return $response->as_string;
80 }
81 1 };
82  
83 # rebuild the @rest array with the compiled route handler
84 1 my @compiled_rest;
85 1 for my $e (@rest) {
86 1 ▶ if (ref($e) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $e eq 'CODE') { } 1 0 1
87 1 push @compiled_rest, {}, $rpcxml_route;
88 }
89 else {
90 0 push @compiled_rest, {}, $e;
91 }
92 }
93
94 1 any ['post'] => $pattern, @compiled_rest;
95 #any ['get', 'post'] => $pattern, @compiled_rest;
96 }
97  
98 0 ▶sub xmlrpc_fault {
Subroutine xmlrpc_fault: not called CC=1 SCAR=6.9
99 0 return RPC::XML::response->new(RPC::XML::fault->new( @_ ));
100 };
101  
102 3 register_plugin;
103 3 1; # End of Dancer::Plugin::RPC::XML
104  
105 =head1 NAME
106  
107 Dancer::Plugin::RPC::XML - A plugin for Dancer to wrap XML-RPC calls
108  
109 =head1 VERSION
110  
111 Version 0.06
112  
113 =head1 SYNOPSIS
114  
115 Quick summary of what the module does.
116  
117 # in your app.pl
118 use Dancer::Plugin::RPC::XML;
119  
120 xmlrpc '/foo/bar' => sub {
121 # methodname
122 my $method = params->{method};
123 # listref of data
124 my $data = params->{data};
125  
126 return xmlrpc_fault(100,"Undefined method") unless $method =~ /something_known/;
127  
128 my $response;
129
130 $response->{name} = "John Smith";
131  
132 return $response;
133 };
134
135 =head1 REGISTERED METHODS
136  
137 =head2 xmlrpc
138
139 Route handler for xmlrpc routes. Unwraps requests and re-wraps responses in xml using
140 the RPC::XML module.
141  
142 =head2 xmlrpc_fault( $faultCode, $faultString )
143  
144 Returns xmlrpc fault xml
145  
146 =head1 AUTHOR
147  
148 Jesper Dalberg, C<< <jdalberg at gmail.com> >>
149  
150 =head1 BUGS
151  
152 Please report any bugs or feature requests to C<bug-dancer-plugin-rpc-xml at rt.cpan.org>, or through
153 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dancer-Plugin-RPC-XML>. I will be notified, and then you'll
154 automatically be notified of progress on your bug as I make changes.
155  
156 =head1 SUPPORT
157  
158 You can find documentation for this module with the perldoc command.
159  
160 perldoc Dancer::Plugin::RPC::XML
161  
162 You can also look for information at:
163  
164 =over 4
165  
166 =item * RT: CPAN's request tracker
167  
168 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dancer-Plugin-RPC-XML>
169  
170 =item * AnnoCPAN: Annotated CPAN documentation
171  
172 L<http://annocpan.org/dist/Dancer-Plugin-RPC-XML>
173  
174 =item * CPAN Ratings
175  
176 L<http://cpanratings.perl.org/d/Dancer-Plugin-RPC-XML>
177  
178 =item * Search CPAN
179  
180 L<http://search.cpan.org/dist/Dancer-Plugin-RPC-XML/>
181  
182 =back
183  
184 =head1 ACKNOWLEDGEMENTS
185  
186 =over
187  
188 =item * Thanks to Randy J Ray (RJRAY) for the wonderful RPC::XML module
189
190 =item * Thanks to the Dancer project for creating an alternative to CGI!
191  
192 =back
193  
194 =head1 COPYRIGHT & LICENSE
195  
196 Copyright 2012 Jesper Dalberg, all rights reserved.
197  
198 This program is free software; you can redistribute it and/or modify it
199 under the same terms as Perl itself.
200  
201 =cut
↑ Summary