File Coverage

blib/lib/Plack/App/JSONRPC.pm
Criterion Covered Total %
statement 34 34 100.0
branch 2 2 100.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1             package Plack::App::JSONRPC;
2 2     2   24383 use 5.008001;
  2         7  
  2         80  
3 2     2   11 use strict;
  2         5  
  2         63  
4 2     2   20 use warnings;
  2         4  
  2         103  
5             our $VERSION = "0.02";
6              
7 2     2   1641 use parent qw(Plack::Component);
  2         638  
  2         9  
8 2     2   33173 use JSON::RPC::Spec;
  2         176717  
  2         74  
9 2     2   2151 use Plack::Request;
  2         167849  
  2         76  
10 2     2   1862 use Plack::Util::Accessor qw(rpc);
  2         694  
  2         16  
11              
12             sub prepare_app {
13 4     4 1 36321 my ($self) = @_;
14 4         99 my $rpc = JSON::RPC::Spec->new;
15 4         4514 while (my ($name, $callback) = each %{$self->{methods}}) {
  16         713  
16 12         39 $rpc->register($name, $callback);
17             }
18 4         22 $self->rpc($rpc);
19 4         100 return;
20             }
21              
22             sub call {
23 4     4 1 35 my ($self, $env) = @_;
24 4         28 my $req = Plack::Request->new($env);
25 4         40 my $body = $self->rpc->parse($req->content);
26 4 100       7466 if (length $body) {
27 3         37 return [200, ['Content-Type' => 'application/json'], [$body]];
28             }
29 1         9 return [204, [], []];
30             }
31              
32             1;
33             __END__