File Coverage

blib/lib/Dancer2/RPCPlugin.pm
Criterion Covered Total %
statement 40 40 100.0
branch 2 2 100.0
condition 2 2 100.0
subroutine 12 12 100.0
pod 2 2 100.0
total 58 58 100.0


line stmt bran cond sub pod time code
1             package Dancer2::RPCPlugin;
2 20     20   79899 use Moo::Role;
  20         44  
  20         143  
3              
4 20     20   12325 BEGIN { require Dancer2::Plugin::RPC; $Dancer2::RPCPlugin::VERSION = Dancer2::Plugin::RPC->VERSION; }
  20         715  
5              
6 20     20   5033 use Dancer2::RPCPlugin::DispatchFromConfig;
  20         56  
  20         657  
7 20     20   6005 use Dancer2::RPCPlugin::DispatchFromPod;
  20         75  
  20         714  
8 20     20   161 use Dancer2::RPCPlugin::DispatchItem;
  20         40  
  20         477  
9              
10 20     20   4724 use Params::Validate ':all';
  20         26762  
  20         3519  
11              
12 20     20   267 use v5.10;
  20         96  
13 20     20   481 no if $] >= 5.018, warnings => 'experimental::smartmatch';
  20         47  
  20         210  
14              
15             # returns xmlrpc for Dancer2::Plugin::RPC::XMLRPC
16             # returns jsonrpc for Dancer2::Plugin::RPC::JSONRPC
17             # returns restrpc for Dancer2::Plugin::RPC::RESTRPC
18             sub rpcplugin_tag {
19 37 100   37 1 2324 my $full_name = ref($_[0]) ? ref($_[0]) : $_[0];
20 37         187 (my $proto = $full_name) =~ s/.*:://;
21 37         377 return "\L${proto}";
22             }
23              
24             sub dispatch_builder {
25 28     28 1 6964 my $self = shift;
26 28         97 my ($endpoint, $publish, $arguments, $settings) = @_;
27              
28 28   100     176 given ($publish // 'config') {
29 28         117 when ('config') {
30             return sub {
31 1     1   309 $self->app->log(
32             debug => "[build_dispatch_table_from_config]"
33             );
34 1         74 my $dispatch_builder = Dancer2::RPCPlugin::DispatchFromConfig->new(
35             plugin_object => $self,
36             plugin => $self->rpcplugin_tag,
37             config => $settings,
38             endpoint => $endpoint,
39             );
40 1         9 return $dispatch_builder->build_dispatch_table();
41 1         11 };
42             }
43 27         74 when ('pod') {
44             return sub {
45 26     26   470 $self->app->log(
46             debug => "[build_dispatch_table_from_pod]"
47             );
48 26         3248 my $dispatch_builder = Dancer2::RPCPlugin::DispatchFromPod->new(
49             plugin_object => $self,
50             plugin => $self->rpcplugin_tag,
51             packages => $arguments,
52             endpoint => $endpoint,
53             );
54 26         283 return $dispatch_builder->build_dispatch_table();
55 26         206 };
56             }
57 1         3 default {
58 1         3 return $_;
59             }
60             }
61             }
62              
63             1;
64              
65             __END__
66              
67             =head1 NAME
68              
69             Dancer2::RPCPlugin - Role to support generic dispatch-table-building
70              
71             =head1 DESCRIPTION
72              
73             =head2 dispatch_builder(%parameters)
74              
75             =head3 Parameters
76              
77             Positional:
78              
79             =over
80              
81             =item 1. endpoint
82              
83             =item 2. publish
84              
85             =item 3. arguments (list of packages for POD-publishing)
86              
87             =item 4. settings (config->{plugins}{RPC::proto})
88              
89             =back
90              
91             =head2 rpcplugin_tag
92              
93             =head3 Parameters
94              
95             None.
96              
97             =head3 Responses
98              
99             <jsonrpc|restrpc|xmlrpc>
100              
101             =head2 dispatch_item(%parameters)
102              
103             =head3 Parameters
104              
105             Named:
106              
107             =over
108              
109             =item code => $code_ref [Required]
110              
111             =item package => $package [Optional]
112              
113             =back
114              
115             =head3 Responses
116              
117             An instance of the class L<Dancer2::RPCPlugin::DispatchItem>.
118              
119             =head1 COPYRIGHT
120              
121             (c) MMXVI - Abe Timmerman <abeltje@cpan.org>
122              
123             =cut