Client/Protocol/REST.pm

filter: Statementstmt 34.8% 23 / 66 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 75.0% 6 / 8 Podpod n/a totaltotal 26.6% 29 / 109 CC 17 SCAR 42.5
CC
17
Cov
24%
_build_result
110.0
__ANON__
72.0
BEGIN
1.0
CRAP 70.1
« Client/Protocol/RPC.pm ↑ Summary Client/HTTPRequestBuilder/OAuth.pm »
1 package Net::OpenSocial::Client::Protocol::REST;
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::Resource::Factory;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Net::OpenSocial::Client::Collection;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶override 'execute' => sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
13 0 my ( $self, $container, $requests ) = @_;
14  
15 0 ▶ return $self->ERROR(q{This container doesn't support rest endpoint.})
Branch: 1 branch
BranchTrueFalseTotal
unless $container->rest_endpoint 0 0 0
16 unless $container->rest_endpoint;
17  
18 0 my $result = Net::OpenSocial::Client::ResultSet->new;
19 0 for my $request (@$requests) {
20  
21 0 my $method = $request->http_method_for_operation();
22  
23 0 my %params = %{ $request->params };
24 0 my $app_id = delete $params{appId};
25  
26 my $url = join(
27 '/',
28 0 ▶ grep { defined($_) && $_ ne '' } (
Condition0%0/3
defined $_ && $_ ne ''
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table0%0/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
29 $container->rest_endpoint, $request->service,
30 $request->user_id, $request->group_id,
31 $app_id,
32 )
33 );
34  
35 0 $params{format} = $self->formatter->name;
36  
37 0 my %build_args = (
38 method => $method,
39 url => $url,
40 params => {%params},
41 container => $container,
42 );
43  
44 0 ▶ if ( $method eq 'POST' || $method eq 'PUT' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "POST" or $method eq "PUT") 0 0 0
Condition0%0/3
$method eq "POST" or $method eq "PUT"
l!l&&r!l&&!r
000
MC/DC0%0/2
$method eq "POST" or $method eq "PUT"
$method eq "POST" $method eq "PUT"
Truth table0%0/3
A or B
A= $method eq "POST"B= $method eq "PUT"
ABresult
1X1
011
000
45 0 ▶ unless ( $request->has_resource ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($request->has_resource) 0 0 0
46 0 return $self->ERROR(q{No resource data found.});
47 }
48 0 my $resource = $request->resource;
49 0 $build_args{content_type} = $self->formatter->content_type;
50 $build_args{content}
51 0 = $self->formatter->encode( $resource->fields );
52 }
53  
54 0 my $http_req = $self->request_builder->build_request(%build_args);
55 0 my $http_res = $self->agent->request($http_req);
56  
57 0 ▶ unless ( $http_res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($http_res->is_success) 0 0 0
58 0 my $error = Net::OpenSocial::Client::Result->new(
59 is_error => 1,
60 code => $http_res->code,
61 message => $http_res->message,
62 );
63 0 $result->set_result( $request->id => $error );
64 0 next;
65 }
66 0 my $res = $self->formatter->decode( $http_res->content );
67 0 $result->set_result(
68 $request->id => $self->_build_result( $request->service, $res ) );
69 }
70 0 return $result;
71 1 };
72  
73 0 ▶sub _build_result {
Subroutine _build_result: not called CC=10 SCAR=47.0
74 0 my ( $self, $service, $obj ) = @_;
75 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
76 0 ▶ my $result = exists $obj->{response} ? $obj->{response} : $obj;
Branch: 1 branch
BranchTrueFalseTotal
exists $obj->{'response'} ? : 0 0 0
77 0 ▶ if ( exists $result->{entry} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $result->{'entry'}) { } 0 0 0
78 0 my $entries = $result->{entry};
79 0 ▶ $entries = [$entries] unless ref $entries eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entries eq "ARRAY" 0 0 0
80 0 my $coll = Net::OpenSocial::Client::Collection->new;
81 $coll->total_results( $result->{totalResults} )
82 0 ▶ if exists $result->{totalResults};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"totalResults"} 0 0 0
83 $coll->start_index( $result->{startIndex} )
84 0 ▶ if exists $result->{startIndex};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"startIndex"} 0 0 0
85 $coll->items_per_page( $result->{itemsPerPage} )
86 0 ▶ if exists $result->{itemsPerPage};
Branch: 1 branch
BranchTrueFalseTotal
if exists $result->{"itemsPerPage"} 0 0 0
87 0 for my $entry (@$entries) {
88 0 ▶ next unless ( keys %$entry > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$entry > 0 0 0 0
89 0 my $resource
90 = Net::OpenSocial::Client::Resource::Factory->gen_resource(
91 $service, $entry );
92 0 $coll->add_item($resource);
93 }
94 0 return Net::OpenSocial::Client::Result->new( data => $coll );
95 }
96 else {
97  
98 # VOID result
99 0 return Net::OpenSocial::Client::Result->new;
100 }
101 }
102  
103 1 ▶no Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 1 __PACKAGE__->meta->make_immutable;
105 1 1;
106  
107 =head1 NAME
108  
109 Net::OpenSocial::Client::Protocol::REST - Protocol class for RESTful API.
110  
111 =head1 SYNOPSIS
112  
113 my $protocol = Net::OpenSocial::Client::Protocol::REST->new(
114 formatter => Net::OpenSocial::CLient::Formatter::JSON->new,
115 request_builder => Net::OpenSocial::Client::HTTPRequestBuilder::OAuth->new(...),
116 agent => LWP::UserAgent->new,
117 );
118 my $result_set = $protocol->execute($container, $requests);
119  
120 =head1 DESCRIPTION
121  
122 Protocol class for RESTful API.
123  
124 =head1 METHODS
125  
126 =head2 execute($container, $requests)
127  
128 =head1 SEE ALSO
129  
130 L<Net::OpenSocial::Client::Protocol>
131  
132 =head1 AUTHOR
133  
134 Lyo Kato, E<lt>lyo.kato@gmail.comE<gt>
135  
136 =head1 COPYRIGHT AND LICENSE
137  
138 Copyright (C) 2009 by Lyo Kato
139  
140 This library is free software; you can redistribute it and/or modify
141 it under the same terms as Perl itself, either Perl version 5.8.8 or,
142 at your option, any later version of Perl 5 you may have available.
143  
144 =cut
« Client/Protocol/RPC.pm ↑ Summary Client/HTTPRequestBuilder/OAuth.pm »