| 1 | package JSON::RPC::Spec; | ||||||||||||
| 2 | 7 | ▶ | use Moo; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | 7 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 7 | ▶ | use version; our $VERSION = version->declare("v1.0.7"); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 7 | ▶ | use Try::Tiny; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 7 | ▶ | use Router::Simple; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 7 | ▶ | use JSON::RPC::Spec::Procedure; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 7 | ▶ | use JSON::RPC::Spec::Client; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 7 | with qw( | |||||||||||
| 10 | JSON::RPC::Spec::Common | ||||||||||||
| 11 | ); | ||||||||||||
| 12 | |||||||||||||
| 13 | 7 | ▶ | use constant DEBUG => $ENV{PERL_JSON_RPC_SPEC_DEBUG} || 0; | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'PERL_JSON_RPC_SPEC_DEBUG'} || 0
MC/DC100%1/1
$ENV{'PERL_JSON_RPC_SPEC_DEBUG'} || 0
$ENV{'PERL_JSON_RPC_SPEC_DEBUG'}
Truth table100%2/2
A
A= $ENV{'PERL_JSON_RPC_SPEC_DEBUG'}
| |||||||||||||
| 14 | |||||||||||||
| 15 | has router => ( | ||||||||||||
| 16 | is => 'lazy', | ||||||||||||
| 17 | isa => sub { | ||||||||||||
| 18 | 8 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||
| 19 | 8 | ▶ | $self->can('match') or Carp::croak('method match required.'); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | }, | ||||||||||||
| 21 | 7 | ); | |||||||||||
| 22 | |||||||||||||
| 23 | 7 | has _procedure => (is => 'lazy'); | |||||||||||
| 24 | |||||||||||||
| 25 | 7 | has _client => ( | |||||||||||
| 26 | is => 'lazy', | ||||||||||||
| 27 | handles => [qw(compose)], | ||||||||||||
| 28 | ); | ||||||||||||
| 29 | |||||||||||||
| 30 | 7 | has [qw(_is_batch _content)] => (is => 'rw'); | |||||||||||
| 31 | |||||||||||||
| 32 | 7 | ▶ | use namespace::clean; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 33 | |||||||||||||
| 34 | |||||||||||||
| 35 | sub _build_router { | ||||||||||||
| 36 | 5 | ▶ | Router::Simple->new; | ||||||||||
|
Subroutine
_build_router: called CC=1 SCAR=0.0
| |||||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | sub _build__procedure { | ||||||||||||
| 40 | 6 | ▶ | JSON::RPC::Spec::Procedure->new(router => shift->router); | ||||||||||
|
Subroutine
_build__procedure: called CC=1 SCAR=0.0
| |||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | sub _build__client { | ||||||||||||
| 44 | 1 | ▶ | JSON::RPC::Spec::Client->new; | ||||||||||
|
Subroutine
_build__client: called CC=1 SCAR=0.0
| |||||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | sub _parse_json { | ||||||||||||
| 48 | 47 | ▶ | my ($self, $extra_args) = @_; | ||||||||||
|
Subroutine
_parse_json: called CC=9 SCAR=22.0
| |||||||||||||
| 49 | 47 | warn qq{-- start parsing @{[$self->_content]}\n} if DEBUG; | |||||||||||
| 50 | |||||||||||||
| 51 | 2 | ▶ | unless (length $self->_content) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 52 | 45 | return $self->_rpc_invalid_request; | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | # JSON decode | ||||||||||||
| 56 | # rpc call with invalid JSON: | ||||||||||||
| 57 | # rpc call Batch, invalid JSON: | ||||||||||||
| 58 | 45 | my ($req, $err); | |||||||||||
| 59 | try { | ||||||||||||
| 60 | 45 | ▶ | $req = $self->coder->decode($self->_content); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | } | ||||||||||||
| 62 | catch { | ||||||||||||
| 63 | 3 | ▶ | $err = $_; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.4
| |||||||||||||
| 64 | 3 | warn qq{-- error : @{[$err]} } if DEBUG; | |||||||||||
| 65 | 45 | }; | |||||||||||
| 66 | 3 | ▶ | if ($err) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | 42 | return $self->_rpc_parse_error; | |||||||||||
| 68 | } | ||||||||||||
| 69 | |||||||||||||
| 70 | # Batch mode flag | ||||||||||||
| 71 | 19 | ▶ | if (ref $req eq 'ARRAY') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 72 | 23 | $self->_is_batch(1); | |||||||||||
| 73 | } | ||||||||||||
| 74 | else { | ||||||||||||
| 75 | 23 | $self->_is_batch(0); | |||||||||||
| 76 | 42 | $req = [$req]; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | # rpc call with an empty Array: | ||||||||||||
| 80 | 42 | ▶ | unless (scalar @{$req}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 40 | return $self->_rpc_invalid_request; | |||||||||||
| 82 | } | ||||||||||||
| 83 | |||||||||||||
| 84 | # procedure call and create response | ||||||||||||
| 85 | 40 | my @response; | |||||||||||
| 86 | 40 | for my $obj (@{$req}) { | |||||||||||
| 87 | 48 | my $res = $self->_procedure->parse($obj, $extra_args); | |||||||||||
| 88 | |||||||||||||
| 89 | # notification is ignore | ||||||||||||
| 90 | 40 | ▶ | push @response, $res if $res; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | } | ||||||||||||
| 92 | 34 | ▶ | return unless @response; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 93 | 21 | ▶ | return [@response] if $self->_is_batch; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 94 | 0 | return $response[0]; | |||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | # parse JSON string to hash | ||||||||||||
| 98 | sub parse_without_encode { | ||||||||||||
| 99 | 19 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
parse_without_encode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 100 | 19 | my $json_string = shift; | |||||||||||
| 101 | 19 | my $extra_args = +[@_]; | |||||||||||
| 102 | 19 | $self->_content($json_string); | |||||||||||
| 103 | 19 | return $self->_parse_json($extra_args); | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | # parse JSON string to JSON string | ||||||||||||
| 107 | sub parse { | ||||||||||||
| 108 | 28 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
parse: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 109 | 28 | my $json_string = shift; | |||||||||||
| 110 | 28 | my $extra_args = +[@_]; | |||||||||||
| 111 | 28 | $self->_content($json_string); | |||||||||||
| 112 | 28 | my $result = $self->_parse_json($extra_args); | |||||||||||
| 113 | 28 | ▶ | return unless $result; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 25 | return $self->coder->encode($result); | |||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | # register method | ||||||||||||
| 118 | sub register { | ||||||||||||
| 119 | 15 | ▶ | my ($self, $pattern, $cb) = @_; | ||||||||||
|
Subroutine
register: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 120 | 15 | ▶ | if (!defined $pattern) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 1 | Carp::croak('pattern required'); | |||||||||||
| 122 | } | ||||||||||||
| 123 | 14 | ▶ | if (ref $cb ne 'CODE') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 124 | 1 | Carp::croak('code required'); | |||||||||||
| 125 | } | ||||||||||||
| 126 | 13 | $self->router->connect($pattern, +{$self->_callback_key => $cb}, +{}); | |||||||||||
| 127 | 13 | return $self; | |||||||||||
| 128 | } | ||||||||||||
| 129 | |||||||||||||
| 130 | 7 | 1; | |||||||||||
| 131 | __END__ | ||||||||||||