Client/Protocol/RPC.pm

filter: Statementstmt 29.8% 26 / 87 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 77.7% 7 / 9 Podpod n/a totaltotal 22.6% 33 / 146 CC 25 SCAR 49.8
CC
25
Cov
20%
_build_result
210.0
__ANON__
156.0
BEGIN
1.0
CRAP 146.0
↑ Summary Client/Protocol/REST.pm »
1 package Net::OpenSocial::Client::Protocol::RPC;
2  
3 1 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'Net::OpenSocial::Client::Protocol';
5 1 with 'Net::OpenSocial::Client::ErrorHandler';
6  
7 1 ▶use Net::OpenSocial::Client::ResultSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Net::OpenSocial::Client::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::OpenSocial::Client::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::OpenSocial::Client::Resource::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Net::OpenSocial::Client::Type::Operation qw(CREATE UPDATE);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 0 ▶override 'execute' => sub {
Subroutine __ANON__: not called CC=12 SCAR=50.5
14 0 my ( $self, $container, $requests ) = @_;
15  
16 0 my $url = $container->rpc_endpoint;
17 0 ▶ return $self->ERROR(q{This container doesn't support RPC endpoint.})
Branch: 1 branch
BranchTrueFalseTotal
unless $url 0 0 0
18 unless $url;
19 0 my $method = 'POST';
20  
21 0 my @obj;
22 0 my %id_req_map = ();
23 0 for my $request (@$requests) {
24 0 my $service = $request->rpc_service;
25 0 my $operation = $request->operation;
26 0 my $obj = {};
27 0 $obj->{method} = sprintf( q{%s.%s}, $service, $operation );
28 0 $obj->{id} = $request->id;
29 0 $obj->{params} = $request->params;
30 0 $obj->{params}{userId} = $request->user_id;
31 0 $obj->{params}{groupId} = $request->group_id;
32 0 ▶ if ( $operation eq CREATE
Branch: 1 branch
BranchTrueFalseTotal
if ($operation eq "create" or $operation eq "update") 0 0 0
Condition0%0/3
$operation eq "create" or $operation eq "update"
l!l&&r!l&&!r
000
MC/DC0%0/2
$operation eq "create" or $operation eq "update"
$operation eq "create" $operation eq "update"
Truth table0%0/3
A or B
A= $operation eq "create"B= $operation eq "update"
ABresult
1X1
011
000
33 || $operation eq UPDATE )
34 {
35 0 ▶ unless ( $request->has_resource ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->has_resource) 0 0 0
36 0 return $self->ERROR(q{CREATE/UPDATE operations need resource.});
37 }
38 0 my $resource = $request->resource;
39 0 $obj->{params}{$service} = $resource->fields;
40 }
41 0 push( @obj, $obj );
42 0 $id_req_map{ $request->id } = $request;
43 }
44 0 ▶ my $content = $self->formatter->encode( @obj > 1 ? [@obj] : $obj[0] );
Branch: 1 branch
BranchTrueFalseTotal
@obj > 1 ? : 0 0 0
45  
46 0 my $http_req = $self->request_builder->build_request(
47 method => $method,
48 url => $url,
49 container => $container,
50 content_type => $self->formatter->content_type,
51 content => $content,
52 );
53  
54 0 my $http_res = $self->agent->request($http_req);
55  
56 0 my $result_set = Net::OpenSocial::Client::ResultSet->new;
57 0 ▶ unless ( $http_res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($http_res->is_success) 0 0 0
58 0 for my $request (@$requests) {
59 0 my $error = Net::OpenSocial::Client::Result->new(
60 is_error => 1,
61 code => $http_res->code,
62 message => $http_res->message,
63 );
64 0 $result_set->set_result( $request->id => $error );
65 }
66 0 return $result_set;
67 }
68 0 my $results = $self->formatter->decode( $http_res->content );
69  
70 0 ▶ unless ( ref $results eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $results eq "ARRAY") 0 0 0
71 0 $results = [$results];
72 }
73 0 for my $result (@$results) {
74 my $res_id
75 0 ▶ = exists $result->{id} ? $result->{id} : $requests->[0]->id;
Branch: 1 branch
BranchTrueFalseTotal
exists $result->{'id'} ? : 0 0 0
76 0 my $origin_req = $id_req_map{$res_id};
77 0 $result_set->set_result(
78 $res_id => $self->_build_result( $origin_req->service, $origin_req->operation, $result ) );
79 }
80 0 return $result_set;
81 1 };
82  
83 0 ▶sub _build_result {
Subroutine _build_result: not called CC=14 SCAR=53.5
84 0 my ( $self, $service, $operation, $obj ) = @_;
85 0 ▶ $obj ||= {};
Condition0%0/2
$obj ||= {}
l!l
00
MC/DC0%0/1
$obj ||= {}
$obj
Truth table0%0/2
A
A= $obj
Aresult
11
00
86 0 ▶ if ( exists $obj->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $obj->{'error'}) { } 0 0 0
87 return Net::OpenSocial::Client::Result->new(
88 is_error => 1,
89 0 %{ $obj->{error} }
90 );
91 }
92 else {
93  
94 #my $result = exists $obj->{result} ? $obj->{result} : $obj;
95 0 ▶ my $result = $obj->{data} || {};
Condition0%0/2
$obj->{'data'} || {}
l!l
00
MC/DC0%0/1
$obj->{'data'} || {}
$obj->{'data'}
Truth table0%0/2
A
A= $obj->{'data'}
Aresult
11
00
96 0 ▶ if ( ( $operation eq CREATE || $operation eq UPDATE ) && scalar( keys %$result ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($operation eq 'create' || $operation eq 'update' and scalar keys %$result == 0) { } 0 0 0
Condition0%0/6
$operation eq "create" || $operation eq "update" and scalar keys %$result == 0
!ll&&!rl&&r
000
$operation eq "create" or $operation eq "update"
l!l&&r!l&&!r
000
MC/DC0%0/3
$operation eq "create" || $operation eq "update" and scalar keys %$result == 0
$operation eq "create" $operation eq "update" scalar keys %$result == 0
Truth table0%0/5
A or B and C
A= $operation eq "create"B= $operation eq "update"C= scalar keys %$result == 0
ABCresult
00X0
1X00
0100
1X11
0111
97  
98 # VOID result
99 0 return Net::OpenSocial::Client::Result->new();
100 }
101 else {
102 0 my $coll = Net::OpenSocial::Client::Collection->new();
103 0 ▶ if ( exists $result->{list} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $result->{'list'}) { } 0 0 0
elsif (keys %$result > 0) { } 0 0 0
104 $coll->total_results( $result->{totalResults} )
105 0 ▶ if exists $result->{totalResults};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"totalResults"} 0 0 0
106 $coll->start_index( $result->{startIndex} )
107 0 ▶ if exists $result->{startIndex};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"startIndex"} 0 0 0
108 $coll->items_per_page( $result->{itemsPerPage} )
109 0 ▶ if exists $result->{itemsPerPage};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"itemsPerPage"} 0 0 0
110 0 my $list = $result->{list};
111 0 ▶ unless ( ref $list eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $list eq "ARRAY") 0 0 0
112  
113 # invalid response format
114 }
115 0 for my $item ( @{ $result->{list} } ) {
116 0 my $resource = Net::OpenSocial::Client::Resource::Factory
117 ->gen_resource( $service, $item );
118 0 $coll->add_item($resource);
119 }
120 }
121 elsif ( keys %$result > 0 ) {
122 0 my $resource = Net::OpenSocial::Client::Resource::Factory
123 ->gen_resource( $service, $result );
124 0 $coll->add_item($resource);
125 }
126 0 return Net::OpenSocial::Client::Result->new( data => $coll );
127 }
128 }
129 }
130  
131 1 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1 __PACKAGE__->meta->make_immutable;
133 1 1;
134  
135 =head1 NAME
136  
137 Net::OpenSocial::Client::Protocol::PRC - Protocol class for RPC.
138  
139 =head1 SYNOPSIS
140  
141 my $protocol = Net::OpenSocial::Client::Protocol::RPC->new(
142 formatter => Net::OpenSocial::CLient::Formatter::JSON->new,
143 request_builder => Net::OpenSocial::Client::HTTPRequestBuilder::OAuth->new(...),
144 agent => LWP::UserAgent->new,
145 );
146 my $result_set = $protocol->execute($container, $requests);
147  
148 =head1 DESCRIPTION
149  
150 Protocol class for RPC.
151  
152 =head1 METHODS
153  
154 =head2 execute($container, $requests)
155  
156 =head1 SEE ALSO
157  
158 L<Net::OpenSocial::Client::Protocol>
159  
160 =head1 AUTHOR
161  
162 Lyo Kato, E<lt>lyo.kato@gmail.comE<gt>
163  
164 =head1 COPYRIGHT AND LICENSE
165  
166 Copyright (C) 2009 by Lyo Kato
167  
168 This library is free software; you can redistribute it and/or modify
169 it under the same terms as Perl itself, either Perl version 5.8.8 or,
170 at your option, any later version of Perl 5 you may have available.
171  
172 =cut
↑ Summary Client/Protocol/REST.pm »