File Coverage

blib/lib/Dancer/RPCPlugin/DispatchMethodList.pm
Criterion Covered Total %
statement 38 38 100.0
branch 6 6 100.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 57 57 100.0


line stmt bran cond sub pod time code
1             package Dancer::RPCPlugin::DispatchMethodList;
2 8     8   63583 use warnings;
  8         32  
  8         331  
3 8     8   52 use strict;
  8         17  
  8         214  
4              
5 8     8   42 use Exporter 'import';
  8         19  
  8         390  
6             our @EXPORT = ('list_methods');
7              
8 8     8   283 use Dancer::RPCPlugin::PluginNames;
  8         16  
  8         216  
9 8     8   46 use Scalar::Util 'blessed';
  8         15  
  8         435  
10 8     8   506 use Types::Standard qw/ StrMatch ArrayRef /;
  8         59261  
  8         132  
11 8     8   6556 use Params::ValidationCompiler 'validation_for';
  8         14108  
  8         2742  
12              
13             =head1 NAME
14              
15             Dancer::RPCPlugin::DispatchMethodList - Class for maintaining a global methodlist.
16              
17             =head1 SYNOPSIS
18              
19             use Dancer::RPCPlugin::DispatchMethodList;
20             my $methods = Dancer::RPCPlugin::DispatchMethodList->new();
21              
22             $methods->set_partial(
23             protocol => <jsonrpc|restrpc|xmlrpc>,
24             endpoint => </configured>,
25             methods => [ @method_names ],
26             );
27              
28             # ....
29             my $method_list = $methods->list_methods(protocol => <any|jsonrpc|restrpc|xmlrpc>);
30              
31             =head1 DESCRIPTION
32              
33             This class implements a singleton that can hold the collection of all method names.
34              
35             =head2 my $dml = Dancer::RPCPlugin::DispatchMethodList->new()
36              
37             =head3 Parameters
38              
39             None!
40              
41             =head3 Responses
42              
43             $_singleton = bless $parameters, $class;
44              
45             =cut
46              
47             my $_singleton;
48             sub new {
49 46 100   46 1 1232 return $_singleton if $_singleton;
50              
51 8         31 my $class = shift;
52 8         47 $_singleton = bless {protocol => {}}, $class;
53             }
54              
55             =head2 $dml->set_partial(%parameters)
56              
57             =head3 Parameters
58              
59             Named, list:
60              
61             =over
62              
63             =item protocol => <jsonrpc|restrpc|xmlrpc> (required)
64              
65             =item endpoint => $endpoint (required)
66              
67             =item methods => \@method_list
68              
69             =back
70              
71             =head3 Responses
72              
73             $self
74              
75             =cut
76              
77             sub set_partial {
78 45     45 1 917 my $self = shift;
79 45         290 my $pn_re = Dancer::RPCPlugin::PluginNames->new->regex;
80 45         647 my %args = validation_for(
81             params => {
82             protocol => {type => StrMatch[ qr/^$pn_re$/ ], optional => 0},
83             endpoint => {type => StrMatch[ qr/^.*$/] , optional => 0},
84             methods => {type => ArrayRef},
85             },
86             )->(@_);
87 45         121170 $self->{protocols}{$args{protocol}}{$args{endpoint}} = $args{methods};
88 45         4616 return $self;
89             }
90              
91             =head2 list_methods(@parameters)
92              
93             Method that returns information about the dispatch-table.
94              
95             =head3 Parameters
96              
97             Positional, list
98              
99             =over
100              
101             =item 1. $protocol => undef || <any|jsonrpc|restrpc|xmlrpc> (optional)
102              
103             =back
104              
105             =head3 Responses
106              
107             In case of no C<$protocol>:
108              
109             {
110             xmlrpc => {
111             $endpoint1 => [ list ],
112             $endpoint2 => [ list ],
113             },
114             jsonrpc => {
115             $endpoint1 => [ list ],
116             $endpoint2 => [ list ],
117             },
118             }
119              
120             In case of specified C<$protocol>:
121              
122             {
123             $endpoint1 => [ list ],
124             $endpoint2 => [ list ],
125             }
126              
127             =cut
128              
129             sub list_methods {
130 6     6 1 2309 my $self;
131 6 100       26 if (blessed($_[0])) {
132 5         12 $self = shift;
133             }
134             else {
135 1         2 $self = $_singleton;
136             }
137 6         30 my $pn_re = Dancer::RPCPlugin::PluginNames->new->regex;
138 6         99 my ($protocol) = validation_for(
139             params => [
140             {
141             type => StrMatch [qr/^(?:any|$pn_re)$/],
142             default => 'any',
143             },
144             ],
145             )->(@_);
146              
147 6 100       7582 if ($protocol eq 'any') {
148 4         220 return $self->{protocols};
149             }
150             else {
151 2         92 return $self->{protocols}{$protocol};
152             }
153             }
154              
155             1;
156              
157             =head1 COPYRIGHT
158              
159             (c) MMXVI - Abe Timmerman <abeltje@cpan.org>
160              
161             =cut