| 1 | package JSON::RPC::Parser; | ||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 3 | ▶ | use JSON::RPC::Procedure; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 3 | ▶ | use Carp (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 3 | ▶ | use Plack::Request; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | use Class::Accessor::Lite | ||||||||||||||
| 7 | 3 | ▶ | new => 1, | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | rw => [ qw( | ||||||||||||||
| 9 | coder | ||||||||||||||
| 10 | ) ] | ||||||||||||||
| 11 | 3 | ; | |||||||||||||
| 12 | |||||||||||||||
| 13 | 20 | ▶ | sub construct_procedure { | ||||||||||||
|
Subroutine
construct_procedure: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 14 | 20 | my $self = shift; | |||||||||||||
| 15 | 20 | JSON::RPC::Procedure->new( @_ ); | |||||||||||||
| 16 | } | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 21 | ▶ | sub construct_from_req { | ||||||||||||
|
Subroutine
construct_from_req: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||
| 19 | 21 | my ($self, $req) = @_; | |||||||||||||
| 20 | |||||||||||||||
| 21 | 21 | my $method = $req->method; | |||||||||||||
| 22 | 21 | my $proc; | |||||||||||||
| 23 | 21 | ▶ | if ($method eq 'POST') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 24 | 15 | $proc = $self->construct_from_post_req( $req ); | |||||||||||||
| 25 | } elsif ($method eq 'GET') { | ||||||||||||||
| 26 | 5 | $proc = $self->construct_from_get_req( $req ); | |||||||||||||
| 27 | } else { | ||||||||||||||
| 28 | 1 | Carp::croak( "Invalid method: $method" ); | |||||||||||||
| 29 | } | ||||||||||||||
| 30 | |||||||||||||||
| 31 | 18 | return $proc; | |||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 15 | ▶ | sub construct_from_post_req { | ||||||||||||
|
Subroutine
construct_from_post_req: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||
| 35 | 15 | my ($self, $req) = @_; | |||||||||||||
| 36 | |||||||||||||||
| 37 | 15 | my $request = eval { $self->coder->decode( $req->content ) }; | |||||||||||||
| 38 | 15 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 1 | Carp::croak( "JSON parse error: $@" ); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | 14 | my $ref = ref $request; | |||||||||||||
| 43 | 14 | ▶ | if ($ref ne 'ARRAY') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | # is not a batch request | ||||||||||||||
| 45 | return $self->construct_procedure( | ||||||||||||||
| 46 | method => $request->{method}, | ||||||||||||||
| 47 | id => $request->{id}, | ||||||||||||||
| 48 | params => $request->{params}, | ||||||||||||||
| 49 | jsonrpc => $request->{jsonrpc}, | ||||||||||||||
| 50 | has_id => exists $request->{id}, | ||||||||||||||
| 51 | 8 | ); | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 6 | my @procs; | |||||||||||||
| 55 | 6 | foreach my $req ( @$request ) { | |||||||||||||
| 56 | 8 | ▶ | Carp::croak( "Invalid parameter") unless ref $req eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 57 | push @procs, $self->construct_procedure( | ||||||||||||||
| 58 | method => $req->{method}, | ||||||||||||||
| 59 | id => $req->{id}, | ||||||||||||||
| 60 | params => $req->{params}, | ||||||||||||||
| 61 | jsonrpc => $req->{jsonrpc}, | ||||||||||||||
| 62 | has_id => exists $req->{id}, # when not true it's a notification in JSON-RPC 2.0 | ||||||||||||||
| 63 | 7 | ); | |||||||||||||
| 64 | } | ||||||||||||||
| 65 | 5 | return \@procs; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 5 | ▶ | sub construct_from_get_req { | ||||||||||||
|
Subroutine
construct_from_get_req: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 69 | 5 | my ($self, $req) = @_; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 5 | my $params = $req->query_parameters; | |||||||||||||
| 72 | 5 | my $decoded_params; | |||||||||||||
| 73 | 5 | ▶ | if ($params->{params}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 4 | $decoded_params = eval { $self->coder->decode( $params->{params} ) }; | |||||||||||||
| 75 | } | ||||||||||||||
| 76 | return $self->construct_procedure( | ||||||||||||||
| 77 | method => $params->{method}, | ||||||||||||||
| 78 | id => $params->{id}, | ||||||||||||||
| 79 | params => $decoded_params, | ||||||||||||||
| 80 | jsonrpc => $params->{jsonrpc}, | ||||||||||||||
| 81 | has_id => exists $params->{id}, | ||||||||||||||
| 82 | 5 | ); | |||||||||||||
| 83 | } | ||||||||||||||
| 84 | |||||||||||||||
| 85 | 3 | 1; | |||||||||||||
| 86 | |||||||||||||||
| 87 | __END__ | ||||||||||||||