File Coverage

blib/lib/Sledge/Plugin/JSONRPC.pm
Criterion Covered Total %
statement 17 48 35.4
branch 0 6 0.0
condition 0 3 0.0
subroutine 5 9 55.5
pod n/a
total 22 66 33.3


line stmt bran cond sub pod time code
1             package Sledge::Plugin::JSONRPC;
2 1     1   22516 use strict;
  1         2  
  1         33  
3 1     1   4 use warnings;
  1         3  
  1         25  
4 1     1   760 use JSON::Syck;
  1         3570  
  1         123  
5              
6             our $VERSION = '0.01';
7              
8             sub import {
9 1     1   9 my $self = shift;
10 1         2 my $pkg = caller;
11              
12             $pkg->register_hook(BEFORE_INIT => sub {
13 0     0   0 my $self = shift;
14 0         0 $self->{_body} = do { local $/; };
  0         0  
  0         0  
15 1         7 });
16              
17 1     1   7 no strict 'refs';
  1         2  
  1         338  
18 1         5 *{"$pkg\::jsonrpc"} = \&_jsonrpc;
  1         14  
19             }
20              
21             sub _jsonrpc {
22 0     0     my $self = shift;
23              
24 0           my $req;
25             # Deserialize request
26 0           eval { $req = _deserialize_jsonrpc($self) };
  0            
27 0 0 0       if ($@ || !$req) {
28 0           warn qq{Invalid JSONRPC request "$@"};
29 0           _serialize_jsonrpc($self,{
30             result => undef,
31             eror => 'Invalid request'
32             });
33 0           return 0;
34             }
35              
36 0           my $res = 0;
37 0           my $method = $req->{method};
38 0 0         if ($method) {
39 0 0         if (my $code = $self->can("jsonrpc_$method")) {
40 0           $res = $self->$code($req);
41             } else {
42 0           warn qq{Couldn't find jsonrpc method "$method"};
43             }
44             }
45              
46             # Serialize response
47 0           _serialize_jsonrpc($self,{
48             result => $res,
49             error => undef,
50             id => $req->{id},
51             });
52 0           return $res;
53             }
54              
55             sub _deserialize_jsonrpc {
56 0     0     my $self = shift;
57              
58 0           my $req = JSON::Syck::Load($self->{_body});
59 0           return $req;
60             }
61              
62             sub _serialize_jsonrpc {
63 0     0     my ($self, $status) = @_;
64              
65 0           my $res = JSON::Syck::Dump($status);
66              
67 0           $self->r->content_type('text/javascript+json');
68 0           $self->set_content_length(length $res);
69 0           $self->send_http_header;
70 0           $self->r->print($res);
71 0           $self->invoke_hook('AFTER_OUTPUT');
72 0           $self->finished(1);
73             }
74              
75             =head1 NAME
76              
77             Sledge::Plugin::JSONRPC - JSONRPC plugin for Sledge
78              
79             =head1 VERSION
80              
81             This documentation refers to Sledge::Plugin::JSONRPC version 0.01
82              
83             =head1 SYNOPSIS
84              
85             package Your::Pages;
86             use Sledge::Plugin::JSON;
87             # entry point
88             sub dispatch_jsonrpc {
89             shift->jsonrpc;
90             }
91            
92             sub jsonrpc_get {
93             my $self = shift;
94             .......
95             return \@data;
96             }
97              
98             =head1 DESCRIPTION
99              
100             Sledge::Plugin::JSONRPC is easy to implement JSONRPC plugin for Sledge.
101              
102             =head1 AUTHOR
103              
104             Atsushi Kobayashi, C<< >>
105              
106             =head1 SEE ALSO
107              
108             L
109              
110             L
111              
112             L
113              
114             =head1 BUGS
115              
116             Please report any bugs or feature requests to
117             C, or through the web interface at
118             L.
119             I will be notified, and then you'll automatically be notified of progress on
120             your bug as I make changes.
121              
122             =head1 SUPPORT
123              
124             You can find documentation for this module with the perldoc command.
125              
126             perldoc Sledge::Plugin::JSONRPC
127              
128             You can also look for information at:
129              
130             =over 4
131              
132             =item * AnnoCPAN: Annotated CPAN documentation
133              
134             L
135              
136             =item * CPAN Ratings
137              
138             L
139              
140             =item * RT: CPAN's request tracker
141              
142             L
143              
144             =item * Search CPAN
145              
146             L
147              
148             =back
149              
150             =head1 ACKNOWLEDGEMENTS
151              
152             =head1 COPYRIGHT & LICENSE
153              
154             Copyright 2006 Atsushi Kobayashi, all rights reserved.
155              
156             This program is free software; you can redistribute it and/or modify it
157             under the same terms as Perl itself.
158              
159             =cut
160              
161             1; # End of Sledge::Plugin::JSONRPC