File Coverage

lib/Dancer/RPCPlugin/DispatchFromConfig.pm
Criterion Covered Total %
statement 43 43 100.0
branch 4 6 66.6
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 57 59 96.6


line stmt bran cond sub pod time code
1             package Dancer::RPCPlugin::DispatchFromConfig;
2 8     8   271934 use warnings;
  8         16  
  8         311  
3 8     8   46 use strict;
  8         16  
  8         179  
4 8     8   38 use Exporter 'import';
  8         13  
  8         431  
5             our @EXPORT = qw/dispatch_table_from_config/;
6              
7 8     8   44 use Dancer qw/error warning info debug/;
  8         20  
  8         72  
8              
9 8     8   11695 use Dancer::RPCPlugin::DispatchItem;
  8         20  
  8         356  
10 8     8   2063 use Dancer::RPCPlugin::PluginNames;
  8         22  
  8         227  
11 8     8   44 use Types::Standard qw/ Int Str StrMatch Any /;
  8         13  
  8         51  
12 8     8   5990 use Params::ValidationCompiler 'validation_for';
  8         15  
  8         2521  
13              
14             sub dispatch_table_from_config {
15 14     14 1 255 my $pn_re = Dancer::RPCPlugin::PluginNames->new->regex;
16 14         323 my %args = validation_for(
17             params => {
18             plugin => { type => StrMatch[ qr/^$pn_re$/ ] },
19             config => { type => Any },
20             endpoint => { type => Str, optional => 0 },
21             }
22             )->(@_);
23 14         30558 my $config = $args{config}{ $args{endpoint} };
24              
25 14         785 my @pkgs = keys %$config;
26              
27 14         31 my $dispatch;
28 14         40 for my $pkg (@pkgs) {
29 5 50       285 eval "require $pkg" if $pkg ne 'main';
30 5 50       475 error("Loading $pkg: $@") if $@;
31              
32 5         12 my @rpc_methods = keys %{ $config->{$pkg} };
  5         25  
33 5         16 for my $rpc_method (@rpc_methods) {
34 9         23 my $subname = $config->{$pkg}{$rpc_method};
35 9         81 debug("[bdfc] $args{endpoint}: $rpc_method => $subname");
36 9 100       545 if (my $handler = $pkg->can($subname)) {
37 8         31 $dispatch->{$rpc_method} = dispatch_item(
38             package => $pkg,
39             code => $handler
40             );
41             }
42             else {
43 1         10 die "Handler not found for $rpc_method: $pkg\::$subname doesn't seem to exist.\n";
44             }
45             }
46             }
47              
48             # we don't want "Encountered CODE ref, using dummy placeholder"
49             # thus we use Data::Dumper::Dumper() directly.
50 13         54 local ($Data::Dumper::Indent, $Data::Dumper::Sortkeys, $Data::Dumper::Terse) = (0, 1, 1);
51 13         82 debug(
52             "[build_dispatcher_from_config]->{$args{plugin}} ",
53             Data::Dumper::Dumper($dispatch)
54             );
55              
56 13         1141 return $dispatch;
57             }
58              
59             1;
60              
61             =head1 NAME
62              
63             Dancer::RPCPlugin::DispatchFromConfig - Build dispatch-table from the Dancer Config
64              
65             =head1 SYNOPSIS
66              
67             use Dancer::Plugin;
68             use Dancer::RPCPlugin::DispatchFromConfig;
69             sub dispatch_call {
70             my $config = plugin_setting();
71             return dispatch_table_from_config($config);
72             }
73              
74             =head1 DESCRIPTION
75              
76             =head2 dispatch_table_from_config(%arguments)
77              
78             =head3 Parameters
79              
80             Named:
81              
82             =over
83              
84             =item plugin => <xmlrpc|jsonrpc|restrpc>
85              
86             =item config => $config_from_plugin
87              
88             =item endpoint => '/endpoint_for_dispatch_table'
89              
90             =back
91              
92             =head3 Responses
93              
94             A (partial) dispatch-table.
95              
96             =head1 COPYRIGHT
97              
98             (c) MMXV - Abe Timmerman <abeltje@cpan.org>
99              
100             =cut