blib/lib/Froody/Dispatch.pm

filter: Statementstmt 73.7% 185 / 251 Branchbran 50.0% 54 / 108 Conditioncond 42.8% 21 / 49 MC/DCmcdc 22.2% 6 / 27 Subroutinesub 86.6% 26 / 30 Podpod 100% 16 / 16 totaltotal 64.0% 308 / 481 CC 84 SCAR 29.7
CC
84
Cov
64%
parse_cli
110.0
add_implementation
42.0
error_style
22.9
CRAP 19.5
« blib/lib/Froody/SimpleClient.pm ↑ Summary blib/lib/Froody/Server/Apache.pm »
1 =head1 NAME
2  
3 Froody::Dispatch - Easily call Froody Methods
4  
5 =head1 SYNOPSIS
6  
7 use Froody::Dispatch;
8 my $dispatcher = Froody::Dispatch->new();
9 my $response = $dispatcher->call( "foo.bar.baz",
10 fred => "wilma"
11 );
12  
13 or, as a client:
14  
15 $client = Froody::Dispatch->new;
16  
17 # uses reflection to load methods from the server
18 $client->add_endpoint( "uri" );
19  
20 # look mah, no arguments!
21 $rsp = $invoker->invoke($client, 'service.wibble');
22  
23 # ok, take some arguments then.
24 $rsp = $invoker->invoke($client, 'service.devide', divisor => 1, dividend => 2);
25  
26 # alternatively, args can be passed as a hashref:
27 $args = { devisor => 1, devidend => 2 };
28 $rsp = $invoker->invoke($client, 'service.devide', $args);
29  
30  
31 =head1 DESCRIPTION
32  
33 This class handles dispatching Froody Methods. It's used both from within the
34 servers where you don't want to have to worry about the little details and as a
35 client.
36  
37 =cut
38  
39 package Froody::Dispatch;
40 19 ▶use base qw( Froody::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 19 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 19 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 19 ▶use Froody::Response::Terse;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 19 ▶use Froody::Repository;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 19 ▶use Froody::Response::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 19 ▶use Froody::Invoker::Remote;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 19 ▶use Froody::Error qw(err);
Subroutine BEGIN: called CC=1 SCAR=0.0
53 19 ▶use Froody::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 19 my $logger = get_logger("froody.dispatch");
56  
57 19 __PACKAGE__->mk_accessors(qw{response_class error_class endpoints filters});
58  
59 =head1 METHODS
60  
61 =head2 Class Methods
62  
63 =over 4
64  
65 =item new
66  
67 Create a new instance of the dispatcher
68  
69 =cut
70  
71 11 ▶sub new {
Subroutine new: called CC=3 SCAR=11.3
Pod documented
72 11 my $class = shift;
73 11 my $self = $class->SUPER::new(@_);
74 11 ▶ $self->endpoints({}) unless $self->endpoints;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->endpoints 11 0 11
75 11 ▶ $self->error_class('Froody::Response::Error') unless $self->error_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->error_class 11 0 11
76 11 $self;
77 }
78  
79 =item parse_cli (@args)
80  
81 Parses a list of files, urls, modules, method filter expressions, and paths.
82  
83 What you're able to do:
84  
85 =over 4
86  
87 =item module
88  
89 -MModule::Name (or just Module::Name) - request that that module is registered.
90  
91 =item path
92  
93 -Ipath will inject that path into C<%INC>
94  
95 =item perl module file name
96  
97 filename will extract all module names from within the path
98  
99 =item method filter
100  
101 foo.bar.baz will be interpreted as a filter expression.
102  
103 =back
104  
105 Returns a hashref of include paths, modules, urls, and filters
106  
107 =cut
108  
109 0 ▶sub parse_cli {
Subroutine parse_cli: not called CC=10 SCAR=47.0
Pod documented
110 0 my ($self, @args) = @_;
111 0 my @urls;
112 my @modules;
113 0 my @filters;
114 0 my @includes;
115 0 for (@args) {
116 0 ▶ if ($_ =~ s/^-M// || $_ =~ m/::/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($_ =~ s/^-M// or $_ =~ /::/) { } 0 0 0
elsif ($_ =~ /^https?:/) { } 0 0 0
elsif (-f $_) { } 0 0 0
elsif ($_ =~ s/^-I//) { } 0 0 0
elsif ($_ =~ /\./) { } 0 0 0
Condition0%0/3
$_ =~ s/^-M// or $_ =~ /::/
l!l&&r!l&&!r
000
MC/DC0%0/2
$_ =~ s/^-M// or $_ =~ /::/
$_ =~ s/^-M// $_ =~ /::/
Truth table0%0/3
A or B
A= $_ =~ s/^-M//B= $_ =~ /::/
ABresult
1X1
011
000
117 0 push @modules, $_;
118 } elsif ( $_ =~ m/^https?:/) {
119 0 push @urls, $_;
120 } elsif ( -f $_ ) {
121 # fairly horrible, this.
122 0 my $data;
123 {
124 0 ▶ open my $fh, "<", $_ or die "can't open $_ for reading: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $_ or die "can't open $_ for reading: $!" 0 0 0
125 0 local $/;
126 0 $data = <$fh>;
127 }
128 0 ▶ while ($data =~ /^\s*package (.*?);$/gm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ /^\s*package (.*?);$/gm) 0 0 0
129 0 my $module = $1;
130 0 $module =~ s/'/::/g; # incase of crack
131 0 push @modules, $module;
132 }
133 } elsif ( $_ =~ s/^-I//) {
134 0 push @includes, $_;
135 } elsif ( $_ =~ m/\./) {
136 0 push @filters, $_;
137 } else {
138 # TODO: Write a real usage doc.
139 0 die "What should I do with $_? I understand perl modules, URLs, and froody method names";
140 }
141 }
142 return {
143 0 modules => \@modules,
144 urls => \@urls,
145 filters => \@filters,
146 includes => \@includes,
147 args => {},
148 }
149 }
150  
151 =item config ($args)
152  
153 Configures the dispatcher. Takes { filters => [], modules => [], urls => [], includes => [] } and
154 ensures that the dispatcher only contains methods that are present in either the modules
155 list or the urls list, and only if those modules match one or more of the filters in the
156 filters list. If the filters list is empty, then all methods will be registered.
157  
158 =cut
159  
160 7 ▶sub config {
Subroutine config: called CC=15 SCAR=27.3
Pod documented
161 7 my ($self, $args) = @_;
162 7 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 7 0 7
163 7 $self = $self->new; # we should be an instance.
164 }
165 7 my $repository = Froody::Repository->new;
166 7 $self->repository($repository);
167 7 for (@{ $args->{includes} }) {
168 0 unshift @INC, $_; #extend the search path
169 }
170 7 ▶ my @filters = @{ $args->{filters} || [] };
Condition100%2/2
$args->{'filters'} || []
l!l
25
MC/DC100%1/1
$args->{'filters'} || []
$args->{'filters'}
Truth table100%2/2
A
A= $args->{'filters'}
Aresult
11
00
171 7 $self->filters(@filters);
172 7 ▶ for (@{ $args->{urls} || [] }) {
Condition50%1/2
$args->{'urls'} || []
l!l
07
MC/DC0%0/1
$args->{'urls'} || []
$args->{'urls'}
Truth table50%1/2
A
A= $args->{'urls'}
Aresult
11
00
173 0 $self->add_endpoint( $_, @filters );
174 }
175 # warn Dumper($args->{modules}); use Data::Dumper;
176  
177 # implementation has-a api_specification
178 # implementation has-a list of filters
179
180 7 my %api;
181 7 ▶ for my $module (@{ $args->{modules} || [] }) {
Condition50%1/2
$args->{'modules'} || []
l!l
70
MC/DC0%0/1
$args->{'modules'} || []
$args->{'modules'}
Truth table50%1/2
A
A= $args->{'modules'}
Aresult
11
00
182
183 # make sure implementation is loaded for isa-checks etc.
184 7 $module->require;
185 7 $module->import();
186
187 7 my ($module_api, $invoker, @filters);
188 7 ▶ if ($module->isa('Froody::API')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($module->isa('Froody::API')) { } 3 4 7
elsif ($module->isa('Froody::Implementation')) { } 4 0 4
189 3 $invoker = bless {}, 'Froody::Invoker';
190 3 $module_api = $module;
191 } elsif ($module->isa("Froody::Implementation")) {
192 4 ($module_api, @filters) = $module->implements;
193 4 $invoker = $module->new;
194 } else {
195 0 Carp::confess "fatal error; $module not a Froody::API or Froody::Implementation";
196 }
197  
198 7 push @{ $api{$module_api} }, [ $invoker, @filters ];
199 }
200
201 7 @filters = _filter_to_rxs(@filters);
202  
203 # Register each API _once_, making sure to bind functions
204 7 foreach my $api (keys %api) {
205 7 $api->require;
206 7 $api->import();
207 7 my @structures = $api->load;
208 7 foreach my $impl (@{$api{$api}}) {
209 7 my ($invoker, @impl_filters) = @$impl;
210 7 $repository->load($invoker, \@structures, @filters, _filter_to_rxs(@impl_filters));
211 7 ▶ if ($invoker->isa("Froody::Implementation")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invoker->isa("Froody::Implementation")) 4 3 7
212 1 my @plugin_methods = grep { !eval { $repository->get_method($_->full_name)} }
213 4 ▶ @{ $invoker->plugin_methods || [] };
Condition100%2/2
$invoker->plugin_methods || []
l!l
13
MC/DC100%1/1
$invoker->plugin_methods || []
$invoker->plugin_methods
Truth table100%2/2
A
A= $invoker->plugin_methods
Aresult
11
00
214 4 for (@plugin_methods) {
215 # UGH
216 1 $repository->load($_->invoker, [$_], @filters);
217 }
218 }
219 }
220 }
221 7 return $self;
222 }
223  
224 14 ▶sub _filter_to_rxs {
Subroutine _filter_to_rxs: called CC=1 SCAR=0.0
225 14 map { Froody::Method->match_to_regex( $_ ) } @_;
226 }
227  
228 =item add_implementation
229  
230 Adds an implementation's methods to this dispatcher.
231  
232 =cut
233  
234 0 ▶sub add_implementation {
Subroutine add_implementation: not called CC=6 SCAR=37.4
Pod documented
235 0 my ($self, $module, @filters) = @_;
236 0 ▶ return if $self->endpoints->{$module}{loaded};
Branch: 1 branch
BranchTrueFalseTotal
if $self->endpoints->{$module}{"loaded"} 0 0 0
237
238 0 ▶ eval " use $module; 1" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval " use $module; 1" or die $@ 0 0 0
239 0 my ($api, $invoker);
240 0 my $repository = $self->repository;
241 0 $invoker = $self->endpoints->{$module}{invoker};
242 0 ▶ if ($module->isa('Froody::API')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($module->isa('Froody::API')) { } 0 0 0
elsif ($module->isa('Froody::Implementation')) { } 0 0 0
243 0 $api = $module;
244 0 ▶ $invoker ||= (bless {}, "Froody::Invoker");
Condition0%0/2
$invoker ||= bless({}, "Froody::Invoker")
l!l
00
MC/DC0%0/1
$invoker ||= bless({}, "Froody::Invoker")
$invoker
Truth table0%0/2
A
A= $invoker
Aresult
11
00
245 0 $self->repository->register_api( $api, $invoker, @filters );
246 } elsif ($module->isa("Froody::Implementation")) {
247 0 my @imp_filters;
248 0 $module->register_in_repository($repository, @filters);
249 }
250 0 $self->endpoints->{$module} = { invoker => $invoker, loaded => 1 };
251 }
252  
253 =item cli_config
254  
255 Parses arguments with L<parse_cli|/Froody::Dispatch>, and then calls
256 config with the arguments. This is intended to be used for parsing command
257 line options, and directly creating configuration details
258  
259 Returns a dispatch object, and the parsed options.
260  
261 =cut
262  
263 0 ▶sub cli_config {
Subroutine cli_config: not called CC=1 SCAR=6.9
Pod documented
264 0 my ($self, @args) = @_;
265 0 my $parsed = $self->parse_cli(@args);
266  
267 0 return ($self->config($parsed), $parsed);
268 }
269  
270 =item default_repository
271  
272 DEPRECATED: This is harmful -- you end up with random methods in your namespaces
273  
274 The first time this method is called it creates a default repository by
275 trawling through all loaded modues and checking which are subclasses of
276 Froody::Implementation.
277  
278 If you're running this in a mod_perl handler you might want to consider
279 calling this method at compile time to preload all the classes.
280  
281 See L<config|/Froody::Dispatch>
282  
283 =cut
284  
285 sub default_repository
286 0 ▶{
Subroutine default_repository: not called CC=1 SCAR=6.9
Pod documented
287 0 require Carp;
288 0 Carp::confess "Don't expect the default repo to introspect.";
289 }
290  
291 =item call_via ($invoker, $method, [@ARGS, $args])
292  
293 Calls C<$method> with C<$invoker>. If $invoker or $method are not
294 instances of C<Froody::Invoker> and C<Froody::Method> respectively then
295 this method will attempt to discover them in the registered list of endpoints
296 and the method repository.
297  
298 Returns a Froody::Response object.
299  
300 =cut
301  
302 26 ▶sub call_via {
Subroutine call_via: called CC=6 SCAR=19.6
Pod documented
303 26 my ($self, $invoker, $method ) = splice @_, 0, 3; #pull off first three args.
304 26 ▶ my $args = ref $_[0] eq 'HASH' ? $_[0]
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 24 2 26
305 : { @_ };
306  
307 26 ▶ Carp::confess "You must provide an invoker" unless $invoker;
Branch: 1 branch
BranchTrueFalseTotal
unless $invoker 0 26 26
308
309 26 ▶ if (!UNIVERSAL::isa($invoker, 'Froody::Invoker')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($invoker, "Froody::Invoker")) 0 26 26
310 0 ▶ $self->endpoints({}) unless $self->endpoints();
Branch: 1 branch
BranchTrueFalseTotal
unless $self->endpoints 0 0 0
311 $invoker = $self->endpoints->{$invoker}{invoker}
312 0 }
313  
314 26 ▶ $method = $self->get_method($method, $args)
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($method, "Froody::Method") 2 24 26
315 unless UNIVERSAL::isa($method, 'Froody::Method');
316
317 26 my $meta = {
318 dispatcher => $self,
319 method => $method,
320 params => $args,
321 repository => $self->repository,
322 };
323 26 return $invoker->invoke($method, $args, $meta);
324 }
325  
326 =item add_endpoint( "url" )
327  
328 Registers all methods from a remote repository within this one.
329  
330 TODO: add regex filtering of methods.
331  
332 =cut
333  
334 2 ▶sub add_endpoint {
Subroutine add_endpoint: called CC=4 SCAR=15.8
Pod documented
335 2 my ($self,$url, @method_filters) = @_;
336 2 ▶ die "add_endpoint requires an url" unless $url;
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 2 2
337
338 2 my $endpoints = $self->endpoints;
339 2 ▶ if ($endpoints->{$url}{loaded}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($endpoints->{$url}{"loaded"}) 0 2 2
340 0 $logger->warn("Attempted to add endpoint($url) more than once.");
341 0 return $self;
342 }
343 2 ▶ $self->endpoints->{ $url }{invoker} ||= Froody::Invoker::Remote->new()->url($url);
Condition33%1/3
$self->endpoints->{$url}{"invoker"} ||= "Froody::Invoker::Remote"->new->url($url)
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->endpoints->{$url}{"invoker"} ||= "Froody::Invoker::Remote"->new->url($url)
$self->endpoints->{$url}{"invoker"} "Froody::Invoker::Remote"->new->url($url)
Truth table33%1/3
A ||= B
A= $self->endpoints->{$url}{"invoker"}B= "Froody::Invoker::Remote"->new->url($url)
ABresult
1X1
011
000
344  
345 2 $self->load_specification($url, @method_filters);
346
347 2 return $self;
348 }
349  
350 =item load_specification ($name, @method_filters)
351  
352 Load method and errortype specifications from a named endpoint
353  
354 =cut
355  
356 2 ▶sub load_specification {
Subroutine load_specification: called CC=2 SCAR=6.9
Pod documented
357 2 my ($self, $name, @method_filters) = @_;
358
359 2 my $endpoint = $self->endpoints->{$name};
360 2 my $invoker = $endpoint->{invoker};
361
362 2 my $repo = $self->repository;
363 2 my $response = $self->call_via($invoker, 'froody.reflection.getSpecification' );
364 2 ▶ if ($response->as_xml->status eq 'ok') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->as_xml->status eq "ok") 2 0 2
365 2 my @structures = Froody::API::XML->load_spec($response->as_xml->xml);
366 2 $repo->load($invoker, \@structures, @method_filters);
367 2 $endpoint->{$endpoint}{loaded} = time;
368 }
369 }
370  
371 =back
372  
373 =head2 Instance Methods
374  
375 =over
376  
377 =item get_method( 'full_name', args )
378  
379 Retrieve a method
380  
381 =cut
382  
383 32 ▶sub get_method {
Subroutine get_method: called CC=9 SCAR=23.6
Pod documented
384 32 my ($self, $name, $args) = @_;
385 32 ▶ Froody::Error->throw("froody.invoke.nomethod", "Missing argument: method")
Branch: 1 branch
BranchTrueFalseTotal
unless length($name || "") 0 32 32
Condition50%1/2
$name || ''
l!l
320
MC/DC0%0/1
$name || ''
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
386 unless length($name || "");
387  
388 # get the method from the repository and just return it
389 32 my $repo = $self->repository;
390 32 my $method = eval { $repo->get_method($name, $args) };
391 32 ▶ return $method if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 29 3 32
392  
393 # We didn't find it? Maybe it's been declared in our spec that may have
394 # changed since we last loaded it. Let's look for it in here!
395 # TODO: Improve this so it DTRT more (though works for now)
396 3 ▶ if (err('froody.invoke.nosuchmethod')) {
Branch: 1 branch
BranchTrueFalseTotal
if (err("froody.invoke.nosuchmethod")) 3 0 3
397 3 local $@ = $@;
398 3 my $get_info = $repo->get_method("froody.reflection.getMethodInfo", $args);
399 3 ▶ for (keys %{$self->endpoints || {}}) {
Condition50%1/2
$self->endpoints || {}
l!l
30
MC/DC0%0/1
$self->endpoints || {}
$self->endpoints
Truth table50%1/2
A
A= $self->endpoints
Aresult
11
00
400 1 my $invoker = $self->endpoints->{$_}{invoker};
401  
402 1 my $info = $self->call_via($invoker, $get_info, { %$args, method_name => $name })->as_xml;
403 1 ▶ if ($info->status eq 'ok') {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->status eq "ok") 0 1 1
404 0 $method = Froody::API::XML->load_method( $info->xml->findnodes("/rsp/method") );
405 0 $method->invoker($invoker);
406 0 $repo->register_method($method);
407 }
408 1 ▶ return $method if $method;
Branch: 1 branch
BranchTrueFalseTotal
if $method 0 1 1
409 }
410 }
411  
412 # if we got this far then, darnit, we had an error so we should throw
413 # it again
414 3 die $@;
415 }
416  
417 =item dispatch( %args )
418  
419 Causes a dispatch to a froody method to happen. At a minimum you need to
420 pass in a method name:
421  
422 my $response = Froody::Dispatch->new->dispatch( method => "foo.bar.bob" );
423  
424 You can also pass in parameters:
425  
426 my $response = Froody::Dispatch->new->dispatch(
427 method => "foo.bar.bob",
428 param => { wibble => "wobble" },
429 );
430  
431 Which repository this class uses and how errors are reported depends on
432 the methods defined below.
433  
434 =cut
435  
436 26 ▶sub dispatch {
Subroutine dispatch: called CC=6 SCAR=18.7
Pod documented
437 26 my $self = shift;
438 26 my $args = { @_ };
439  
440 # Strip leading and trailing whitespace in all incomming attribute names.
441 26 ▶ if (defined $args->{method}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"method"}) 26 0 26
442 26 $args->{method} =~ s/^\s+//;
443 26 $args->{method} =~ s/\s+$//;
444 }
445 26 for (keys %{ $args->{params} }) {
446 13 ▶ next unless /^\s|\s$/; # if there is any leading or trailing whitespace in the key name
Branch: 1 branch
BranchTrueFalseTotal
unless /^\s|\s$/ 13 0 13
447 0 my $val = delete $args->{params}{$_};
448 0 s/^\s+//;
449 0 s/\s+$//;
450 0 $args->{params}{$_} = $val;
451 }
452  
453 26 my $response;
454
455 26 my $method = eval { $self->get_method( $args->{method}, $args->{params} ) };
456
457 26 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) { } 3 23 26
458 # something here eats $@. Bah.
459 3 $response = $self->error_class->from_exception( $e, $self->repository );
460  
461 } else {
462
463 23 ▶ Froody::Error->throw("froody.invoke.noinvoker", "No invoker defined for this method")
Branch: 1 branch
BranchTrueFalseTotal
unless $method->invoker 0 23 23
464 unless $method->invoker;
465
466 23 $response = $self->call_via($method->invoker, $method, $args->{params});
467 }
468
469 # throw an exception if what we got back wasn't an acceptable
470 # Froody::Response object
471 24 $self->_validate_response($response);
472  
473 24 return $self->render_response( $response);
474 }
475  
476 =item render_response( $response )
477  
478 =cut
479  
480 24 ▶sub render_response {
Subroutine render_response: called CC=9 SCAR=22.0
Pod documented
481 24 my ($self, $response) = @_;
482
483 24 my $style = $self->error_style;
484 24 ▶ my $error = $response->isa("Froody::Response::Error") ? $response : undef;
Branch: 1 branch
BranchTrueFalseTotal
$response->isa('Froody::Response::Error') ? : 4 20 24
485
486 24 ▶ if ($error && $style eq 'throw') {
Branch: 1 branch
BranchTrueFalseTotal
if ($error and $style eq "throw") 4 20 24
Condition66%2/3
$error and $style eq "throw"
!ll&&!rl&&r
2004
MC/DC50%1/2
$error and $style eq "throw"
$error $style eq "throw"
Truth table66%2/3
A and B
A= $errorB= $style eq "throw"
ABresult
0X0
100
111
487 4 $error->throw;
488 }
489  
490 20 my $is_an_error;
491 20 ▶ if ($response->can('status')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->can('status')) { } 13 7 20
492 13 $is_an_error = $response->status ne 'ok';
493 } else {
494 7 $response = $response->as_xml; #Response must be representable as XML for now.
495  
496 7 ▶ if ($response->status ne 'ok') {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->status ne "ok") 2 5 7
497 2 my $code = $response->xml->findvalue('/rsp/err/@code');
498 2 $is_an_error = 1;
499 # we need to fix our structure
500 2 ▶ $response->structure($self->repository->get_closest_errortype($code ? $code : '' ));
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 2 0 2
501 }
502 }
503  
504 20 ▶ if ($style eq "throw" && $is_an_error)
Branch: 1 branch
BranchTrueFalseTotal
if ($style eq "throw" and $is_an_error) 2 18 20
Condition66%2/3
$style eq "throw" and $is_an_error
!ll&&!rl&&r
0182
MC/DC50%1/2
$style eq "throw" and $is_an_error
$style eq "throw" $is_an_error
Truth table66%2/3
A and B
A= $style eq "throw"B= $is_an_error
ABresult
0X0
100
111
505 {
506 2 $response->as_error->throw;
507 }
508
509 18 return $response;
510 }
511  
512 # throws an error if the reponse isn't valid
513 sub _validate_response
514 24 ▶{
Subroutine _validate_response: called CC=5 SCAR=20.9
515 24 my $class = shift;
516 24 my $response = shift;
517  
518 # nothing? Throw an error
519 24 ▶ if ( ! $response ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response) 0 24 24
520 0 Froody::Error->throw("froody.invoke.badresponse", "No response");
521 }
522  
523 # if we didn't get the right sort of response, throw an error.
524 24 ▶ unless ( ref($response) && blessed($response)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $response and builtin::blessed($response) and $response->isa("Froody::Response")) 0 24 24
Condition33%2/6
ref $response and builtin::blessed($response) and $response->isa("Froody::Response")
!ll&&!rl&&r
0024
ref $response and builtin::blessed($response)
!ll&&!rl&&r
0024
MC/DC0%0/3
ref $response and builtin::blessed($response) and $response->isa("Froody::Response")
ref $response builtin::blessed($response) $response->isa("Froody::Response")
Truth table25%1/4
A and B and C
A= ref $responseB= builtin::blessed($response)C= $response->isa("Froody::Response")
ABCresult
0XX0
10X0
1100
1111
525 && $response->isa("Froody::Response")) {
526 0 Froody::Error->throw("froody.invoke.badresponse", "Bad response $response");
527 }
528 }
529  
530 =item call( 'method', [ args ] )
531  
532 Call a method (optionally with arguments) and return a
533 L<Froody::Response::Terse> response, as described in
534 L<Froody::DataFormats>. This is a thin wrapper for the
535 ->dispatch() method.
536  
537 =cut
538  
539 22 ▶sub call {
Subroutine call: called CC=2 SCAR=6.9
Pod documented
540 22 my $self = shift;
541 22 my $method = shift;
542 22 ▶ my $args = ref $_[0] eq "HASH" ? shift : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 2 20 22
543  
544 22 $self->_fix_args($method, $args);
545  
546 22 my $rsp = $self->dispatch( method => $method, params => $args );
547  
548 15 return $rsp->as_terse->{content};
549 }
550  
551 22 ▶sub _fix_args {
Subroutine _fix_args: called CC=6 SCAR=20.7
552 22 my ($self, $method, $args) = @_;
553 22 for (keys %$args) {
554 12 ▶ next unless (ref($args->{$_}) and blessed($args->{$_}));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args->{$_} and builtin::blessed($args->{$_}) 10 2 12
Condition100%3/3
ref $args->{$_} and builtin::blessed($args->{$_})
!ll&&!rl&&r
732
MC/DC100%2/2
ref $args->{$_} and builtin::blessed($args->{$_})
ref $args->{$_} builtin::blessed($args->{$_})
Truth table100%3/3
A and B
A= ref $args->{$_}B= builtin::blessed($args->{$_})
ABresult
0X0
100
111
555 2 ▶ next if ($args->{$_}->isa("Froody::Upload"));
Branch: 1 branch
BranchTrueFalseTotal
if $args->{$_}->isa("Froody::Upload") 2 0 2
556 0 ▶ if ($args->{$_}->isa("Class::DBI")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{$_}->isa('Class::DBI')) { } 0 0 0
557 # this is our typical DWIM case
558 0 $args->{$_} = $args->{$_}->id;
559 } else {
560 # you can't do this.
561 Froody::Error->throw(
562 'froody.invoke.blessedRef',
563 0 "Can't pass object '".ref($args->{$_})."' as the '$_' param to method $method",
564 );
565 }
566 }
567 }
568  
569 =item get_methods ( [@filters] )
570  
571 Provides a list of L<Froody::Method> objects. Optionally, the methods are filtered
572 by a list of filter patterns. If L<Froody::Method::config> was called with
573 a list of filters, the methods will be pre-filtered by that list. If you wish
574 to override the configured filters, call this method with C<undef>, or use
575 the repository methods directly.
576  
577 =cut
578  
579 3 ▶sub get_methods {
Subroutine get_methods: called CC=3 SCAR=11.8
Pod documented
580 3 my ($self, @filters) = @_;
581  
582 3 ▶ unless (@_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ > 1) 3 0 3
583 3 @filters = $self->filters;
584 }
585 3 ▶ if (!defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 0 3 3
586 0 @filters = ();
587 }
588 3 return $self->repository->get_methods(@filters);
589 }
590  
591  
592 =item repository
593  
594 Get/set the repository that we're calling methods on. If this is set
595 to undef (as it is by default) then we will use the default repository
596 (see above.)
597  
598 =cut
599  
600 sub repository
601 85 ▶{
Subroutine repository: called CC=7 SCAR=23.6
Pod documented
602 85 my $self = shift;
603  
604 85 ▶ unless (blessed($self))
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::blessed($self)) 0 85 85
605 0 { Froody::Error->throw("perl.methodcall.class", "repository cannot be called as a class method") }
606
607 85 ▶ unless (@_)
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 75 10 85
608 {
609 75 ▶ return $self->{repository} if defined $self->{repository};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"repository"} 75 0 75
610 0 return $self->default_repository;
611 }
612
613 10 ▶ unless (!defined($_[0]) || blessed($_[0]) && $_[0]->isa("Froody::Repository"))
Branch: 1 branch
BranchTrueFalseTotal
unless (not defined $_[0] or builtin::blessed($_[0]) and $_[0]->isa("Froody::Repository")) 0 10 10
Condition33%2/6
not defined $_[0] or builtin::blessed($_[0]) and $_[0]->isa("Froody::Repository")
l!l&&r!l&&!r
0100
builtin::blessed($_[0]) and $_[0]->isa("Froody::Repository")
!ll&&!rl&&r
0010
MC/DC0%0/3
not defined $_[0] or builtin::blessed($_[0]) and $_[0]->isa("Froody::Repository")
not defined $_[0] builtin::blessed($_[0]) $_[0]->isa("Froody::Repository")
Truth table25%1/4
not(A) or B and C
A= not defined $_[0]B= builtin::blessed($_[0])C= $_[0]->isa("Froody::Repository")
ABCresult
1XX1
0111
00X0
0100
614 0 { Froody::Error->throw("perl.methodcall.param", "repository must be passed undef or a Froody::Repository instance") }
615
616 10 $self->{repository} = shift;
617 10 return $self;
618 }
619  
620 =item error_style
621  
622 Get/set chained accessor that sets the style of errors that this should use. By
623 default this is C<response>, which causes all errors to be converted into valid
624 responses. The other option is C<throw> which turns all errors into
625 Froody::Error objects which are then immediatly thrown.
626  
627 =cut
628  
629 sub error_style
630 24 ▶{
Subroutine error_style: called CC=6 SCAR=31.3
Pod documented
631 24 my $self = shift;
632 24 ▶ return $self->{error_style} || "throw" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 24 0 24
Condition50%1/2
$self->{'error_style'} || 'throw'
l!l
024
MC/DC0%0/1
$self->{'error_style'} || 'throw'
$self->{'error_style'}
Truth table50%1/2
A
A= $self->{'error_style'}
Aresult
11
00
633
634 0 ▶ unless ($_[0] && ($_[0] eq "response" || $_[0] eq "throw"))
Branch: 1 branch
BranchTrueFalseTotal
unless ($_[0] and $_[0] eq "response" || $_[0] eq "throw") 0 0 0
Condition0%0/6
$_[0] and $_[0] eq "response" || $_[0] eq "throw"
!ll&&!rl&&r
000
$_[0] eq "response" or $_[0] eq "throw"
l!l&&r!l&&!r
000
MC/DC0%0/3
$_[0] and $_[0] eq "response" || $_[0] eq "throw"
$_[0] $_[0] eq "response" $_[0] eq "throw"
Truth table0%0/4
A and B or C
A= $_[0]B= $_[0] eq "response"C= $_[0] eq "throw"
ABCresult
0XX0
1000
11X1
1011
635 0 { Froody::Error->throw("perl.methodcall.param", "Invalid error style") }
636
637 0 $self->{error_style} = shift;
638 0 return $self;
639 }
640  
641 =back
642  
643 =head1 BUGS
644  
645 None known.
646  
647 Please report any bugs you find via the CPAN RT system.
648 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody>
649  
650 =head1 AUTHOR
651  
652 Copyright Fotango 2005. All rights reserved.
653  
654 Please see the main L<Froody> documentation for details of who has worked
655 on this project.
656  
657 This module is free software; you can redistribute it and/or modify it under
658 the same terms as Perl itself.
659  
660 =head1 SEE ALSO
661  
662 L<Froody>, L<Froody::Method>
663  
664 =cut
665  
666 19 1;
« blib/lib/Froody/SimpleClient.pm ↑ Summary blib/lib/Froody/Server/Apache.pm »