blib/lib/Catalyst/Plugin/Server/JSONRPC/DispatchType/JSONRPCPath.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 120 CC 15 SCAR 35.7
CC
15
Cov
0%
register
72.0
methods
30.0
list
12.0
CRAP 35.5
1 package Catalyst::Plugin::Server::JSONRPC::DispatchType::JSONRPCPath;
2  
3 use strict;
4 use base qw/Catalyst::DispatchType::Path/;
5 use Text::SimpleTable;
6 use Data::Dumper;
7  
8 __PACKAGE__->mk_accessors(qw/config/);
9  
10 =head1 NAME
11  
12 Catalyst::Plugin::Server::JSONRPC::DispatchType::JSONRPCPath - JSONRPCPath DispatchType
13  
14 =head1 SYNOPSIS
15  
16 See L<Catalyst>.
17  
18 =head1 DESCRIPTION
19  
20 =head1 METHODS
21  
22 =head2 $self->list($c)
23  
24 Generates a nice debug-table containing the JSONRPCPath methods.
25  
26 =cut
27  
28 sub list {
29 my ( $self, $c ) = @_;
30 my $prefixwarning = 1;
31  
32 ### Because this is the only place where we need the config
33 $self->config( $c->server->jsonrpc->config );
34  
35 my $paths = Text::SimpleTable->new(
36 [ 36, 'JSONRPCPath Method' ],
37 [ 37, 'Private' ]
38 );
39  
40 for my $method ( sort keys %{ $self->methods($c) } ) {
41 my $action = $self->methods($c)->{$method};
42 $paths->row( $method, "/$action" );
43 }
44  
45 $c->log->debug( "Loaded JSONRPC entrypoint:\n host.tld" .
46 $self->config->path);
47 $c->log->debug( "Loaded JSONRPCPath Method actions:\n" . $paths->draw )
48 if ( keys %{ $self->methods($c) } );
49 $c->log->debug( 'WARNING: JSONRPC prefix set, but _not_ used!' ) if
50 ($prefixwarning && $self->config->prefix);
51  
52 }
53  
54 =head2 $self->methods()
55  
56 Returns a hashref containing 'methods' => action_object mappings. Methods
57 are in the form of "example.bla.get"
58  
59 =cut
60  
61 sub methods {
62 my ( $self, $c ) = @_;
63 my $prefixwarning = 1;
64  
65 ### Cached list of method => path mapping
66 return $self->{methods} if $self->{methods};
67 $self->{methods} = {};
68  
69 ### Because this is the only place where we need the config
70 $self->config( $c->server->jsonrpc->config)
71 unless $self->config;
72  
73 for my $path ( sort keys %{ $self->{_paths} } ) {
74 my $action = UNIVERSAL::isa($self->{_paths}->{$path}, 'ARRAY') ?
75 $self->{_paths}->{$path}->[0] : $self->{_paths}->{$path};
76 $path = "/$path" unless $path eq '/';
77 my ($method) = $path =~ m|^/?(.*)$|;
78 my $separator= $self->config->separator;
79 my $prefix = $self->config->prefix;
80 $method =~ s|/|$separator|g;
81 $method =~ s|^$prefix\.||g;
82 $self->{methods}->{$method} = $action;
83 }
84  
85 return $self->{methods};
86 }
87  
88 =head2 $self->register( $c, $action )
89  
90 Registers the JSONRPCPath actions into the dispatcher
91  
92 =cut
93  
94 sub register {
95 my ( $self, $c, $action ) = @_;
96  
97 my $attrs = $action->attributes;
98 my @register;
99  
100 foreach my $r ( @{ $attrs->{JSONRPCPath} || [] } ) {
101 unless ($r) {
102 $r = $action->namespace;
103 $r = '/' unless $r;
104 }
105 elsif ( $r !~ m!^/! ) { # It's a relative path
106 $r = $action->namespace . "/$r";
107 }
108 push( @register, $r );
109 }
110  
111 if ( $attrs->{JSONRPCGlobal} ) {
112 push( @register, $action->name ); # Register sub name against root
113 }
114  
115 if ( $attrs->{JSONRPCLocal} || $attrs->{JSONRPC} ) {
116 push( @register, join( '/', $action->namespace, $action->name ) );
117  
118 # Register sub name as a relative path
119 }
120  
121 $self->register_path( $c, $_, $action ) for @register;
122  
123 $c->server->jsonrpc->dispatcher->{Path} = $self
124 unless (scalar %{$c->server->jsonrpc->dispatcher});
125  
126 return 1 if @register;
127 return 0;
128 }
129  
130 sub match {
131 my $self = shift;
132 my ($c, $name) = @_;
133  
134 ### This subtile line is available to prevent backing up to
135 ### a default action
136 return unless $c->req->path eq $name;
137  
138 $self->SUPER::match( @_ );
139 }
140  
141  
142 =head1 AUTHOR
143  
144 Sergey Nosenko C<darknos@cpan.org>
145  
146 =head1 BASED ON
147  
148 Catalyst::Plugin::Server::JSONRPC::DispatchType::JSONRPCPath of
149  
150 Michiel Ootjers C<michiel@cpan.org>
151 Jos Boumans, C<kane@cpan.org>
152  
153 =head1 COPYRIGHT
154  
155 This program is free software, you can redistribute it and/or modify it under
156 the same terms as Perl itself.
157  
158 =cut
159  
160 1;