Client/API.pm

filter: Statementstmt 100% 57 / 57 Branchbran 70.0% 7 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 12 / 12 Podpod 50.0% 1 / 2 totaltotal 90.7% 78 / 86 CC 7 SCAR 7.6
CC
7
Cov
93%
_decode_api
4.1
actions
3.0
new_from_js
2.0
CRAP 2.1
« Client.pm ↑ Summary Client/API/Method.pm »
1 package RPC::ExtDirect::Client::API;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶no warnings 'uninitialized'; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 11 ▶use RPC::ExtDirect::Util::Accessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use RPC::ExtDirect::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use RPC::ExtDirect::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 11 ▶use base 'RPC::ExtDirect::API';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 ### PUBLIC CLASS METHOD (CONSTRUCTOR) ###
16 #
17 # Instantiate a new API declaration from JavaScript code
18 #
19  
20 21 ▶sub new_from_js {
Subroutine new_from_js: called CC=2 SCAR=7.1
Pod undocumented
21 21 my ($class, %params) = @_;
22
23 21 ▶ my $config = delete $params{config} || RPC::ExtDirect::Config->new();
Condition33%1/3
delete $params{'config'} || 'RPC::ExtDirect::Config'->new
l!l&&r!l&&!r
2100
MC/DC0%0/2
delete $params{'config'} || 'RPC::ExtDirect::Config'->new
delete $params{'config'} 'RPC::ExtDirect::Config'->new
Truth table33%1/3
A || B
A= delete $params{'config'}B= 'RPC::ExtDirect::Config'->new
ABresult
1X1
011
000
24 21 my $js = delete $params{js};
25
26 21 my $api_href = _decode_api($js);
27
28 my $self = $class->SUPER::new_from_hashref(
29 config => $config,
30 api_href => $api_href->{actions},
31 type => $api_href->{type},
32 url => $api_href->{url},
33 21 %params,
34 );
35
36 21 return $self;
37 }
38  
39 ### PUBLIC INSTANCE METHOD ###
40 #
41 # Return an Action object or a list of Action objects, depending on context
42 #
43  
44 3 ▶sub actions {
Subroutine actions: called CC=3 SCAR=11.0
Pod documented
45 3 my ($self, @actions) = @_;
46  
47 3 ▶ if ( wantarray ) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 2 1 3
48 my @set = @actions ? @actions
49 2 ▶ : keys %{ $self->{actions} }
Branch: 1 branch
BranchTrueFalseTotal
@actions ? : 1 1 2
50 ;
51
52 2 my @result = map { $self->get_action_by_name($_) } @set;
53  
54 2 return @result;
55 }
56 else {
57 1 my $action_name = shift @actions;
58
59 1 return $self->get_action_by_name($action_name);
60 };
61 }
62  
63 ### PUBLIC INSTANCE METHODS ###
64 #
65 # Read-write accessors
66 #
67  
68 11 RPC::ExtDirect::Util::Accessor->mk_accessors(
69 simple => [qw/ type url /],
70 );
71  
72 ############## PRIVATE METHODS BELOW ##############
73  
74 ### PRIVATE PACKAGE SUBROUTINE ###
75 #
76 # Decode API declaration and check basic constraints
77 #
78  
79 21 ▶sub _decode_api {
Subroutine _decode_api: called CC=4 SCAR=14.0
80 21 my ($js) = @_;
81  
82 21 $js =~ s/^[^{]+//;
83
84 21 my ($api_js) = eval { JSON->new->utf8(1)->decode_prefix($js) };
85  
86 21 ▶ die "Can't decode API declaration: $@\n" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 21 21
87  
88 21 ▶ die "Empty API declaration\n"
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $api_js 0 21 21
89 unless 'HASH' eq ref $api_js;
90  
91 die "Unsupported API type\n"
92 21 ▶ unless $api_js->{type} =~ /remoting|polling/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $api_js->{"type"} =~ /remoting|polling/i 0 21 21
93
94 # Convert the JavaScript API definition to the format
95 # API::new_from_hashref expects
96 21 my $actions = $api_js->{actions};
97
98 my %remote_actions
99 21 = map { $_ => _convert_action($actions->{$_}) } keys %$actions;
100
101 21 $api_js->{actions} = \%remote_actions;
102
103 21 return $api_js;
104 }
105  
106 ### PRIVATE PACKAGE SUBROUTINE ###
107 #
108 # Convert JavaScript Action definition to remote Action definition
109 #
110  
111 13 ▶sub _convert_action {
Subroutine _convert_action: called CC=1 SCAR=0.0
112 13 my ($action_def) = @_;
113
114 13 my %methods = map { delete $_->{name} => $_ } @$action_def;
115
116 return {
117 13 remote => 1,
118 methods => \%methods,
119 };
120 }
121  
122 11 1;
« Client.pm ↑ Summary Client/API/Method.pm »