blib/lib/WWW/Wookie/Connector/Service.pm

filter: Statementstmt 59.5% 196 / 329 Branchbran 21.9% 18 / 82 Conditioncond 25.9% 7 / 27 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 89.3% 42 / 47 Podpod 100% 14 / 14 totaltotal 53.9% 279 / 517 SCAR 59.1
CC
51
Cov
50%
getUsers
42.0
getAvailableServices
23.2
getOrCreateInstance
21.8
CRAP 367.3
↑ Summary blib/script/TestWookieService.pl »
1 # -*- cperl; cperl-indent-level: 4 -*-
2 # Copyright (C) 2010-2021, Roland van Ipenburg
3 package WWW::Wookie::Connector::Service v1.1.3;
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use 5.020000;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 #use Log::Log4perl qw(:resurrect :easy get_logger);
11 4 ▶use Exception::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use HTTP::Status qw(HTTP_CREATED HTTP_OK HTTP_UNAUTHORIZED HTTP_FORBIDDEN);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use LWP::UserAgent qw/POST/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Moose qw/around has with/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Regexp::Common qw(URI);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use URI::Escape qw(uri_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use namespace::autoclean '-except' => 'meta', '-also' => qr/^__/sxm;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 ▶use WWW::Wookie::Connector::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use WWW::Wookie::Server::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use WWW::Wookie::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use WWW::Wookie::Widget;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use WWW::Wookie::Widget::Category;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 4 ▶use WWW::Wookie::Widget::Property;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 4 ▶use WWW::Wookie::Widget::Instance;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 4 ▶use WWW::Wookie::Widget::Instances;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 4 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 ## no critic qw(ProhibitCallsToUnexportedSubs)
34 4 Readonly::Scalar my $DEFAULT_ICON =>
35 q{http://www.oss-watch.ac.uk/images/logo2.gif};
36 4 Readonly::Scalar my $TIMEOUT => 15;
37 4 Readonly::Scalar my $AGENT => q{WWW::Wookie/}
38 . $WWW::Wookie::Connector::Service::VERSION;
39 4 Readonly::Scalar my $TESTUSER => q{testuser};
40  
41 4 Readonly::Scalar my $EMPTY => q{};
42 4 Readonly::Scalar my $QUERY => q{?};
43 4 Readonly::Scalar my $SLASH => q{/};
44 4 Readonly::Scalar my $TRUE => 1;
45 4 Readonly::Scalar my $FALSE => 0;
46  
47 4 Readonly::Scalar my $MORE_ARGS => 4;
48 4 Readonly::Scalar my $MOST_ARGS => 5;
49  
50 4 Readonly::Scalar my $GET => q{GET};
51 4 Readonly::Scalar my $POST => q{POST};
52 4 Readonly::Scalar my $DELETE => q{DELETE};
53 4 Readonly::Scalar my $PUT => q{PUT};
54  
55 4 Readonly::Scalar my $ALL => q{all};
56 4 Readonly::Scalar my $PARTICIPANTS => q{participants};
57 4 Readonly::Scalar my $PROPERTIES => q{properties};
58 4 Readonly::Scalar my $SERVICES => q{services};
59 4 Readonly::Scalar my $WIDGETS => q{widgets};
60 4 Readonly::Scalar my $WIDGETINSTANCES => q{widgetinstances};
61  
62 4 Readonly::Scalar my $DEFAULT_SCHEME => q{http};
63 4 Readonly::Scalar my $VALID_SCHEMES => $DEFAULT_SCHEME . q{s?}; # http(s)
64  
65 4 Readonly::Hash my %LOG => (
66 'GET_USERS' => q{Getting users for instance of '%s'},
67 'USING_URL' => q{Using URL '%s'},
68 'RESPONSE_CODE' => q{Got response code %s},
69 'DO_REQUEST' => q{Requesting %s '%s'},
70 'ALL_TRUE' => q{Requesting all widgets},
71 );
72  
73 4 Readonly::Hash my %ERR => (
74 'NO_WIDGET_INSTANCE' => q{No Widget instance},
75 'NO_PROPERTIES_INSTANCE' => q{No properties instance},
76 'NO_USER_OBJECT' => q{No User object},
77 'NO_WIDGET_GUID' => q{No GUID nor widget object},
78 'MALFORMED_URL' => q{URL for supplied Wookie Server is malformed: %s},
79 'INCORRECT_PARTICIPANTS_REST_URL' =>
80 q{Participants rest URL is incorrect: %s},
81 'INCORRECT_PROPERTIES_REST_URL' => q{Properties rest URL is incorrect: %s},
82 'INVALID_API_KEY' => q{Invalid API key},
83 'HTTP' => q{%s<br />%s},
84 );
85 ## use critic
86  
87 ###l4p Log::Log4perl::easy_init($ERROR);
88  
89 ###l4p has '_logger' => (
90 ###l4p 'is' => 'ro',
91 ###l4p 'isa' => 'Log::Log4perl::Logger',
92 ###l4p 'default' =>
93 ###l4p sub { Log::Log4perl->get_logger('WWW::Wookie::Connector::Service') },
94 ###l4p 'reader' => 'getLogger',
95 ###l4p );
96  
97 4 has '_conn' => (
98 'is' => 'rw',
99 'isa' => 'WWW::Wookie::Server::Connection',
100 'reader' => 'getConnection',
101 'writer' => '_setConnection',
102 );
103  
104 4 has '_locale' => (
105 'is' => 'rw',
106 'isa' => 'Str',
107 'reader' => 'getLocale',
108 'writer' => 'setLocale',
109 );
110  
111 ## no critic qw(Capitalization)
112 sub getAvailableServices {
113 ## use critic
114 1 ▶ my ( $self, $service_name ) = @_;
Subroutine getAvailableServices: called CC=5 SCAR=31.5
Pod documented
115 1 my $url = $self->_append_path($SERVICES);
116 1 __check_url( $url, $ERR{'MALFORMED_URL'} );
117 0 my $content = {};
118 0 ▶ if ($service_name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($service_name) 0 0 0
119 0 $url .= $SLASH . URI::Escape::uri_escape($service_name);
120 }
121 0 ▶ if ( $self->getLocale ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getLocale) 0 0 0
122 0 ${$content}{'locale'} = $self->getLocale;
123 }
124  
125 0 my %services = ();
126 0 my $response = $self->_do_request( $url, $content, $GET );
127 0 my $xml_obj = XML::Simple->new(
128 'ForceArray' => 1,
129 'KeyAttr' => { 'widget' => q{id}, 'service' => q{name} },
130 )->XMLin( $response->content );
131 0 ▶ while ( my ( $name, $value ) = each %{ ${$xml_obj}{'service'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{${$xml_obj;}{"service"};}) 0 0 0
132 ###l4p $self->getLogger->debug($name);
133 0 my $service = WWW::Wookie::Widget::Category->new( 'name' => $name );
134 0 ▶ while ( my ( $id, $value ) = each %{ ${$value}{'widget'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{${$value;}{"widget"};}) 0 0 0
135 0 $service->put(
136 WWW::Wookie::Widget->new( $id, $self->_parse_widget($value) ) );
137 }
138 0 $services{$name} = $service;
139 }
140 0 return values %services;
141 }
142  
143 ## no critic qw(Capitalization)
144 sub getAvailableWidgets {
145 ## use critic
146 1 ▶ my ( $self, $service ) = @_;
Subroutine getAvailableWidgets: called CC=6 SCAR=26.9
Pod documented
147 1 my %widgets = ();
148 1 my $url = $self->_append_path($WIDGETS);
149 1 my $content = {};
150 1 ▶ if ( !defined $service || $service eq $ALL ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $service or $service eq $ALL) { } 1 0 1
elsif ($service) { } 0 0 0
Condition33%1/3
not defined $service or $service eq $ALL
l!l&&r!l&&!r
100
MC/DC0%0/2
not defined $service or $service eq $ALL
not defined $service $service eq $ALL
Truth table33%1/3
not(A) or B
A= not defined $serviceB= $service eq $ALL
ABresult
1X1
011
000
151 ###l4p $self->getLogger->debug( $LOG{'ALL_TRUE'} );
152 1 ${$content}{'all'} = q{true};
153 }
154 elsif ($service) {
155 0 $url .= $SLASH . URI::Escape::uri_escape($service);
156 }
157 1 ▶ if ( $self->getLocale ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->getLocale) 0 1 1
158 0 ${$content}{'locale'} = $self->getLocale;
159 }
160 1 __check_url( $url, $ERR{'MALFORMED_URL'} );
161  
162 0 my $response = $self->_do_request( $url, $content, $GET );
163 0 my $xml_obj =
164 XML::Simple->new( 'ForceArray' => 1, 'KeyAttr' => 'id' )
165 ->XMLin( $response->content );
166 0 ▶ while ( my ( $id, $value ) = each %{ ${$xml_obj}{'widget'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{${$xml_obj;}{"widget"};}) 0 0 0
167 $widgets{$id} =
168 WWW::Wookie::Widget->new( $id,
169 0 $self->_parse_widget( ${ ${$xml_obj}{'widget'} }{$id} ) );
170 }
171 0 return values %widgets;
172 }
173  
174 4 has '_user' => (
175 'is' => 'ro',
176 'isa' => 'WWW::Wookie::User',
177 'reader' => '_getUser',
178 'writer' => '_setUser',
179 );
180  
181 ## no critic qw(Capitalization)
182 sub getUser {
183 ## use critic
184 1 ▶ my ( $self, $userid ) = @_;
Subroutine getUser: called CC=3 SCAR=13.3
Pod documented
185 1 ▶ if ( defined $userid && $userid =~ /$TESTUSER(\d+)/gsmxi ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $userid and $userid =~ /$TESTUSER(\d+)/gimsux) 0 1 1
Condition33%1/3
defined $userid and $userid =~ /$TESTUSER(\d+)/gimsux
!ll&&!rl&&r
100
MC/DC0%0/2
defined $userid and $userid =~ /$TESTUSER(\d+)/gimsux
defined $userid $userid =~ /$TESTUSER(\d+)/gimsux
Truth table33%1/3
A and B
A= defined $useridB= $userid =~ /$TESTUSER(\d+)/gimsux
ABresult
0X0
100
111
186 0 return WWW::Wookie::User->new( $userid, qq{Test User $1} );
187 }
188 1 return $self->_getUser;
189 }
190  
191 ## no critic qw(Capitalization)
192 sub setUser {
193 ## use critic
194 0 ▶ my ( $self, $login, $screen ) = @_;
Subroutine setUser: not called CC=1 SCAR=6.9
Pod documented
195 0 $self->_setUser( WWW::Wookie::User->new( $login, $screen ) );
196 0 return;
197 }
198  
199 has 'WidgetInstances' => (
200 'is' => 'rw',
201 'isa' => 'WWW::Wookie::Widget::Instances',
202 9 ▶ 'default' => sub { WWW::Wookie::Widget::Instances->new() },
Subroutine __ANON__: called CC=1 SCAR=0.0
203 4 'writer' => '_setWidgetInstances',
204 );
205  
206 ## no critic qw(Capitalization)
207 sub getWidget {
208 ## use critic
209 0 ▶ my ( $self, $widget_id ) = @_;
Subroutine getWidget: not called CC=1 SCAR=6.9
Pod documented
210 my @widgets =
211 0 grep { $_->getIdentifier eq $widget_id } $self->getAvailableWidgets;
212 0 return shift @widgets;
213  
214 ## no critic qw(ProhibitCommentedOutCode)
215 # API method isn't implemented using proper id on the server.
216 #my $url = $self->_append_path($WIDGETS);
217 #if ( defined $widget_id ) {
218 # $url .= $SLASH . URI::Escape::uri_escape($widget_id);
219 #}
220 #__check_url($url, $ERR{'MALFORMED_URL'});
221  
222 #my $response = $self->_do_request( $url, {}, $GET );
223 #my $xs = XML::Simple->new( 'ForceArray' => 1, 'KeyAttr' => 'id' );
224 #my $xml_obj = $xs->XMLin( $response->content );
225 #return WWW::Wookie::Widget->new( $widget_id,
226 # $self->_parse_widget($xml_obj) );
227 ## use critic
228 }
229  
230 ## no critic qw(Capitalization)
231 sub getOrCreateInstance {
232 ## use critic
233 2 ▶ my ( $self, $widget_or_guid ) = @_;
Subroutine getOrCreateInstance: called CC=8 SCAR=30.8
Pod documented
234 2 my $guid = $widget_or_guid;
235 2 ▶ if ( q{WWW::Wookie::Widget} eq ref $widget_or_guid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("WWW::Wookie::Widget" eq ref $widget_or_guid) 0 2 2
236 0 $guid = $widget_or_guid->getIdentifier;
237 }
238 2 my $result = eval {
239 2 ▶ if ( defined $guid && $guid eq $EMPTY ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $guid and $guid eq $EMPTY) 1 1 2
Condition66%2/3
defined $guid and $guid eq $EMPTY
!ll&&!rl&&r
011
MC/DC50%1/2
defined $guid and $guid eq $EMPTY
defined $guid $guid eq $EMPTY
Truth table66%2/3
A and B
A= defined $guidB= $guid eq $EMPTY
ABresult
0X0
100
111
240 ## no critic qw(RequireExplicitInclusion)
241 WookieConnectorException->throw(
242 1 'error' => $ERR{'NO_WIDGET_GUID'} );
243 ## use critic
244 }
245 1 my $url = $self->_append_path($WIDGETINSTANCES);
246 1 __check_url( $url, $ERR{'MALFORMED_URL'} );
247 0 my $content = { 'widgetid' => $guid };
248 0 ▶ if ( my $locale = $self->getLocale ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $locale = $self->getLocale) 0 0 0
249 0 ${$content}{'locale'} = $locale;
250 }
251 0 my $response = $self->_do_request( $url, $content );
252 0 ▶ if ( $response->code == HTTP_CREATED ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 201) 0 0 0
253 0 $response = $self->_do_request( $url, $content );
254 }
255  
256 0 my $instance = $self->_parse_instance( $guid, $response->content );
257 0 ▶ if ($instance) {
Branch: 1 branch
BranchTrueFalseTotal
if ($instance) 0 0 0
258 0 $self->WidgetInstances->put($instance);
259 0 $self->addParticipant( $instance, $self->getUser );
260 }
261 0 return $instance;
262 };
263  
264 2 ▶ if ( my $e = Exception::Class->caught('WookieConnectorException') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Exception::Class"->caught("WookieConnectorException")) 2 0 2
265 ###l4p $self->getLogger->error( $e->error );
266 2 $e->rethrow;
267 0 return $FALSE;
268 }
269 0 return $result;
270 }
271  
272 ## no critic qw(Capitalization)
273 sub getUsers {
274 ## use critic
275 0 ▶ my ( $self, $instance ) = @_;
Subroutine getUsers: not called CC=6 SCAR=37.4
Pod documented
276 0 ▶ if ( ref $instance ne q{WWW::Wookie::Widget::Instance} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $instance ne "WWW::Wookie::Widget::Instance") 0 0 0
277 0 $instance = $self->getOrCreateInstance($instance);
278 }
279 ###l4p $self->getLogger->debug( sprintf $LOG{'GET_USERS'},
280 ###l4p $instance->getIdentifier );
281 0 my $url = $self->_append_path($PARTICIPANTS);
282 ###l4p $self->getLogger->debug( sprintf $LOG{'USING_URL'}, $url );
283  
284 0 __check_url( $url, $ERR{'MALFORMED_URL'} );
285 0 my $response =
286 $self->_do_request( $url, { 'widgetid' => $instance->getIdentifier, },
287 $GET, );
288  
289 0 ▶ if ( $response->code > HTTP_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code > 200) 0 0 0
290 0 __throw_http_err($response);
291 }
292 0 my $xml_obj =
293 XML::Simple->new( 'ForceArray' => 1, 'KeyAttr' => 'id' )
294 ->XMLin( $response->content );
295 0 my @users = ();
296 0 ▶ while ( my ( $id, $value ) = each %{ ${$xml_obj}{'participant'} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{${$xml_obj;}{"participant"};}) 0 0 0
297 my $new_user = WWW::Wookie::User->new(
298 $id,
299 0 ▶ defined ${$value}{'displayName'} || $id,
Condition0%0/3
defined do { || $EMPTY
l!l&&r!l&&!r
000
MC/DC0%0/2
defined do { $value }->{'thumbnail_url'} || $EMPTY
defined do { $value }->{'thumbnail_url'} $EMPTY
Truth table0%0/3
A || B
A= defined do { $value }->{'thumbnail_url'}B= $EMPTY
ABresult
1X1
011
000
300 0 ▶ defined ${$value}{'thumbnail_url'} || $EMPTY,
Condition0%0/3
defined do { || $id
l!l&&r!l&&!r
000
MC/DC0%0/2
defined do { $value }->{'displayName'} || $id
defined do { $value }->{'displayName'} $id
Truth table0%0/3
A || B
A= defined do { $value }->{'displayName'}B= $id
ABresult
1X1
011
000
301 );
302 0 push @users, $new_user;
303 }
304 0 return @users;
305 }
306  
307 ## no critic qw(Capitalization)
308 sub addProperty {
309 ## use critic
310 1 ▶ my ( $self, $widget, $property ) = @_;
Subroutine addProperty: called CC=4 SCAR=25.0
Pod documented
311 1 my $url = $self->_append_path($PROPERTIES);
312 1 __check_url( $url, $ERR{'INCORRECT_PROPERTIES_REST_URL'} );
313 0 my $response = $self->_do_request(
314 $url,
315 {
316 'widgetid' => $widget->getIdentifier,
317 'propertyname' => $property->getName,
318 'propertyvalue' => $property->getValue,
319 'is_public' => $property->getIsPublic,
320 },
321 $POST,
322 );
323 0 ▶ if ( $response->code == HTTP_OK || $response->code == HTTP_CREATED ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($response->code == 200 or $response->code == 201) { } 0 0 0
elsif ($response->code > 201) { } 0 0 0
Condition0%0/3
$response->code == 200 or $response->code == 201
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->code == 200 or $response->code == 201
$response->code == 200 $response->code == 201
Truth table0%0/3
A or B
A= $response->code == 200B= $response->code == 201
ABresult
1X1
011
000
324 0 return $TRUE;
325 }
326 elsif ( $response->code > HTTP_CREATED ) {
327 0 return $response->content;
328 }
329 0 return $FALSE;
330 }
331  
332 ## no critic qw(Capitalization)
333 sub getProperty {
334 ## use critic
335 4 ▶ my ( $self, $widget_instance, $property_instance ) = @_;
Subroutine getProperty: called CC=2 SCAR=7.2
Pod documented
336 4 my $url = $self->_append_path($PROPERTIES);
337 4 __check_widget($widget_instance);
338 3 __check_property($property_instance);
339 2 __check_url( $url, $ERR{'MALFORMED_URL'} );
340 1 my $response = $self->_do_request(
341 $url,
342 {
343 'widgetid' => $widget_instance->getIdentifier,
344 'propertyname' => $property_instance->getName,
345 },
346 $GET,
347 );
348 1 ▶ if ( !$response->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 1 0 1
349 1 __throw_http_err($response);
350 0 return $FALSE;
351 }
352 0 return WWW::Wookie::Widget::Property->new( $property_instance->getName,
353 $response->content );
354  
355 }
356  
357 ## no critic qw(Capitalization)
358 sub setProperty {
359 ## use critic
360 3 ▶ my ( $self, $widget, $property ) = @_;
Subroutine setProperty: called CC=5 SCAR=20.9
Pod documented
361 3 my $url = $self->_append_path($PROPERTIES);
362 3 my $result = eval {
363 3 __check_widget($widget);
364 2 __check_property($property);
365 1 __check_url( $url, $ERR{'INCORRECT_PROPERTIES_REST_URL'} );
366 0 my $response = $self->_do_request(
367 $url,
368 {
369 'widgetid' => $widget->getIdentifier,
370 'propertyname' => $property->getName,
371 'propertyvalue' => $property->getValue,
372 'is_public' => $property->getIsPublic,
373 },
374  
375 ## no critic qw(ProhibitFlagComments)
376 # TODO: $PUT breaks, but should be used instead of $POST
377 ## use critic
378 $POST,
379 );
380 0 ▶ if ( $response->code == HTTP_CREATED || $response == HTTP_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 201 or $response == 200) { } 0 0 0
Condition0%0/3
$response->code == 201 or $response == 200
l!l&&r!l&&!r
000
MC/DC0%0/2
$response->code == 201 or $response == 200
$response->code == 201 $response == 200
Truth table0%0/3
A or B
A= $response->code == 201B= $response == 200
ABresult
1X1
011
000
381 0 return $property;
382 }
383 else {
384 0 __throw_http_err($response);
385 }
386 };
387 3 ▶ if ( my $e = Exception::Class->caught('WookieConnectorException') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Exception::Class"->caught("WookieConnectorException")) 2 1 3
388 ###l4p $self->getLogger->error( $e->error );
389 2 $e->rethrow;
390 0 return $FALSE;
391 }
392 1 ▶ if ( my $e = Exception::Class->caught('WookieWidgetInstanceException') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "Exception::Class"->caught("WookieWidgetInstanceException")) 1 0 1
393 ###l4p $self->getLogger->error( $e->error );
394 1 $e->rethrow;
395 0 return $FALSE;
396 }
397 0 return $result;
398 }
399  
400 ## no critic qw(Capitalization)
401 sub deleteProperty {
402 ## use critic
403 3 ▶ my ( $self, $widget, $property ) = @_;
Subroutine deleteProperty: called CC=2 SCAR=9.7
Pod documented
404 3 my $url = $self->_append_path($PROPERTIES);
405 3 __check_url( $url, $ERR{'INCORRECT_PROPERTIES_REST_URL'} );
406 2 __check_widget($widget);
407 1 __check_property($property);
408 0 my $response = $self->_do_request(
409 $url,
410 {
411 'widgetid' => $widget->getIdentifier,
412 'propertyname' => $property->getName,
413 },
414 $DELETE,
415 );
416 0 ▶ if ( $response->code == HTTP_OK ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 200) 0 0 0
417 0 return $TRUE;
418 }
419 0 return $FALSE;
420 }
421  
422 ## no critic qw(Capitalization)
423 sub addParticipant {
424 ## use critic
425 2 ▶ my ( $self, $widget_instance, $user ) = @_;
Subroutine addParticipant: called CC=4 SCAR=23.7
Pod documented
426 2 __check_widget($widget_instance);
427 1 my $url = $self->_append_path($PARTICIPANTS);
428 1 __check_url( $url, $ERR{'INCORRECT_PARTICIPANTS_REST_URL'} );
429 0 my $response = $self->_do_request(
430 $url,
431 {
432 'widgetid' => $widget_instance->getIdentifier,
433 'participant_id' => $self->getUser->getLoginName,
434 'participant_display_name' => $user->getScreenName,
435 'participant_thumbnail_url' => $user->getThumbnailUrl,
436 },
437 );
438 0 ▶ if ( $response->code == HTTP_OK ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($response->code == 200) { } 0 0 0
elsif ($response->code == 201) { } 0 0 0
elsif ($response->code > 201) { } 0 0 0
439 0 return $TRUE;
440 }
441 elsif ( $response->code == HTTP_CREATED ) {
442 0 return $TRUE;
443 }
444 elsif ( $response->code > HTTP_CREATED ) {
445 0 return $response->content;
446 }
447 0 return $FALSE;
448 }
449  
450 ## no critic qw(Capitalization)
451 sub deleteParticipant {
452 ## use critic
453 2 ▶ my ( $self, $widget, $user ) = @_;
Subroutine deleteParticipant: called CC=4 SCAR=23.7
Pod documented
454 2 __check_widget($widget);
455 1 my $url = $self->_append_path($PARTICIPANTS);
456 1 __check_url( $url, $ERR{'INCORRECT_PARTICIPANTS_REST_URL'} );
457 0 my $response = $self->_do_request(
458 $url,
459 {
460 'widgetid' => $widget->getIdentifier,
461 'participant_id' => $self->getUser->getLoginName,
462 'participant_display_name' => $user->getScreenName,
463 'participant_thumbnail_url' => $user->getThumbnailUrl,
464 },
465 $DELETE,
466 );
467 0 ▶ if ( $response->code == HTTP_OK ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($response->code == 200) { } 0 0 0
elsif ($response->code == 201) { } 0 0 0
elsif ($response->code > 201) { } 0 0 0
468 0 return $TRUE;
469 }
470 elsif ( $response->code == HTTP_CREATED ) {
471 0 return $TRUE;
472 }
473 elsif ( $response->code > HTTP_CREATED ) {
474 0 __throw_http_err($response);
475 }
476 0 return $FALSE;
477 }
478  
479 ## no critic qw(Capitalization)
480 sub _setWidgetInstancesHolder {
481 ## use critic
482 9 ▶ my $self = shift;
Subroutine _setWidgetInstancesHolder: called CC=1 SCAR=0.0
483 9 $self->_setWidgetInstances( WWW::Wookie::Widget::Instances->new );
484 9 return;
485 }
486  
487 has '_ua' => (
488 'is' => 'rw',
489 'isa' => 'LWP::UserAgent',
490 'default' => sub {
491 9 ▶ LWP::UserAgent->new(
Subroutine __ANON__: called CC=1 SCAR=0.0
492 'timeout' => $TIMEOUT,
493 'agent' => $AGENT,
494 );
495 },
496 4 );
497  
498 around 'BUILDARGS' => sub {
499 9 ▶ my $orig = shift;
Subroutine __ANON__: called CC=4 SCAR=14.2
500 9 my $class = shift;
501  
502 9 ▶ if ( @_ == $MORE_ARGS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == $MORE_ARGS) 0 9 9
503 0 push @_, $EMPTY;
504 }
505 9 ▶ if ( @_ == $MOST_ARGS && !ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == $MOST_ARGS and not ref $_[0]) 7 2 9
Condition66%2/3
@_ == $MOST_ARGS and not ref $_[0]
!ll&&!rl&&r
207
MC/DC50%1/2
@_ == $MOST_ARGS and not ref $_[0]
@_ == $MOST_ARGS not ref $_[0]
Truth table66%2/3
A and not(B)
A= @_ == $MOST_ARGSB= not ref $_[0]
ABresult
0X0
100
111
506 7 my ( $url, $api_key, $shareddata_key, $loginname, $screenname ) = @_;
507 7 return $class->$orig(
508 '_user' => WWW::Wookie::User->new( $loginname, $screenname ),
509 '_conn' => WWW::Wookie::Server::Connection->new(
510 $url, $api_key, $shareddata_key,
511 ),
512 );
513 }
514 2 return $class->$orig(@_);
515 4 };
516  
517 sub BUILD {
518 9 ▶ my $self = shift;
Subroutine BUILD: called CC=1 SCAR=0.0
Pod documented
519 9 $self->_setWidgetInstancesHolder;
520 9 return;
521 }
522  
523 sub _append_path {
524 16 ▶ my ( $self, $path ) = @_;
Subroutine _append_path: called CC=1 SCAR=0.0
525 16 return $self->getConnection->getURL . URI::Escape::uri_escape($path);
526 }
527  
528 sub __check_url {
529 my ( $url, $message ) = @_;
530 if ( $url !~ m{^$RE{URI}{HTTP}{-keep}{ '-scheme' => $VALID_SCHEMES }$}smx )
531 {
532 ## no critic qw(RequireExplicitInclusion)
533 WookieConnectorException->throw( 'error' => sprintf $message, $url );
534 ## use critic
535 }
536 return;
537 }
538  
539 sub __check_widget {
540 my ($ref) = @_;
541 if ( ref $ref ne q{WWW::Wookie::Widget::Instance} ) {
542 ## no critic qw(RequireExplicitInclusion)
543 WookieWidgetInstanceException->throw(
544 ## use critic
545 'error' => $ERR{'NO_WIDGET_INSTANCE'},
546 );
547 }
548 return;
549 }
550  
551 sub __check_property {
552 my ($ref) = @_;
553 if ( ref $ref ne q{WWW::Wookie::Widget::Property} ) {
554 ## no critic qw(RequireExplicitInclusion)
555 WookieConnectorException->throw(
556 ## use critic
557 'error' => $ERR{'NO_PROPERTIES_INSTANCE'},
558 );
559 }
560 return;
561 }
562  
563 sub __throw_http_err {
564 my ($response) = @_;
565 ## no critic qw(RequireExplicitInclusion)
566 WookieConnectorException->throw(
567 ## use critic
568 'error' => sprintf $ERR{'HTTP'},
569 $response->headers->as_string, $response->content,
570 );
571 return;
572 }
573  
574 sub _do_request {
575 1 ▶ my ( $self, $url, $payload, $method ) = @_;
Subroutine _do_request: called CC=6 SCAR=19.7
576  
577 # Widgets and Services request doesn't require API key stuff:
578 1 ▶ if ( $url !~ m{/(?:widgets|services)(?:[?/]|$)}gismx ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($url =~ m[/(?:widgets|services)(?:[?/]|$)]gimsux) 1 0 1
579 $payload = {
580 'api_key' => $self->getConnection->getApiKey,
581 'shareddatakey' => $self->getConnection->getSharedDataKey,
582 'userid' => $self->getUser->getLoginName,
583 1 %{$payload},
584 };
585 }
586 1 ▶ if ( !defined $method ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method) 0 1 1
587 0 $method = $POST;
588 }
589  
590 1 ▶ if ( ( my $content = [ POST $url, [ %{$payload} ] ]->[0]->content ) ne
Branch: 1 branch
BranchTrueFalseTotal
if ((my $content = [POST($url, [%{$payload;}])]->[0]->content) ne $EMPTY) 1 0 1
591 $EMPTY )
592 {
593 1 $url .= $QUERY . $content;
594 }
595 ###l4p $self->getLogger->debug( sprintf $LOG{'DO_REQUEST'}, $method, $url );
596 1 my $request = HTTP::Request->new(
597 $method => $url,
598 HTTP::Headers->new(),
599 );
600 1 my $response = $self->_ua->request($request);
601 ###l4p $self->getLogger->debug( sprintf $LOG{'RESPONSE_CODE'}, $response->code );
602 1 ▶ if ( $response->code == HTTP_UNAUTHORIZED
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code == 401 or $response->code == 403) 0 1 1
Condition33%1/3
$response->code == 401 or $response->code == 403
l!l&&r!l&&!r
001
MC/DC0%0/2
$response->code == 401 or $response->code == 403
$response->code == 401 $response->code == 403
Truth table33%1/3
A or B
A= $response->code == 401B= $response->code == 403
ABresult
1X1
011
000
603 || $response->code == HTTP_FORBIDDEN )
604 {
605 ## no critic qw(RequireExplicitInclusion)
606 0 WookieConnectorException->throw( 'error' => $ERR{'INVALID_API_KEY'} );
607 ## use critic
608 }
609 1 return $response;
610 }
611  
612 sub _parse_instance {
613 0 ▶ my ( $self, $guid, $xml ) = @_;
Subroutine _parse_instance: not called CC=2 SCAR=17.9
614 0 my $xml_obj =
615 XML::Simple->new( 'ForceArray' => 1, 'KeyAttr' => 'id' )->XMLin($xml);
616 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my $instance = "WWW::Wookie::Widget::Instance"->new(do { $xml_obj }->{"url"}[0], $guid, do { $xml_obj }->{"title"}[0], do { $xml_obj }->{"height"}[0], do { $xml_obj }->{"width"}[0])) 0 0 0
617 my $instance = WWW::Wookie::Widget::Instance->new(
618 0 ${$xml_obj}{'url'}[0], $guid,
619 0 ${$xml_obj}{'title'}[0], ${$xml_obj}{'height'}[0],
620 0 ${$xml_obj}{'width'}[0],
621 )
622 )
623 {
624 0 $self->WidgetInstances->put($instance);
625 0 $self->addParticipant( $instance, $self->getUser );
626 0 return $instance;
627 }
628 0 return;
629 }
630  
631 sub _parse_widget {
632 0 ▶ my ( $self, $xml ) = @_;
Subroutine _parse_widget: not called CC=4 SCAR=30.0
633 0 my $title = ${ ${$xml}{'name'}[0] }{'content'};
634 my $description =
635 0 ref ${$xml}{'description'}[0]
636 0 ? ${ ${$xml}{'description'}[0] }{'content'}
637 0 ▶ : ${$xml}{'description'}[0];
Branch: 1 branch
BranchTrueFalseTotal
ref do { $xml }->{'description'}[0] ? : 0 0 0
638 my $icon =
639 0 ref ${$xml}{'icon'}[0]
640 0 ? ${ ${$xml}{'icon'}[0] }{'content'}
641 0 ▶ : ${$xml}{'icon'}[0];
Branch: 1 branch
BranchTrueFalseTotal
ref do { $xml }->{'icon'}[0] ? : 0 0 0
642 0 ▶ if ( !$icon ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($icon) 0 0 0
643 0 $icon = $DEFAULT_ICON;
644 }
645 0 return ( $title, $description, $icon );
646 }
647  
648 4 with 'WWW::Wookie::Connector::Service::Interface';
649  
650 4 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
651  
652 4 __PACKAGE__->meta->make_immutable;
653  
654 4 1;
655  
656 __END__
657  
658 =encoding utf8
659  
660 =for stopwords Bitbucket API Readonly Wookie guid Ipenburg login MERCHANTABILITY
661  
662 =head1 NAME
663  
664 WWW::Wookie::Connector::Service - Wookie connector service, handles all the
665 data requests and responses
666  
667 =head1 VERSION
668  
669 This document describes WWW::Wookie::Connector::Service version C<v1.1.3>
670  
671 =head1 SYNOPSIS
672  
673 use WWW::Wookie::Connector::Service;
674  
675 =head1 DESCRIPTION
676  
677 =head1 SUBROUTINES/METHODS
678  
679 This module is an implementation of the
680 L<WWW::Wookie::Connector::Service::Interface
681 |WWW::Wookie::Connector::Service::Interface/"SUBROUTINES/METHODS">.
682  
683 =head2 C<new>
684  
685 Create a new connector
686  
687 =over
688  
689 =item 1. URL to Wookie host as string
690  
691 =item 2. Wookie API key as string
692  
693 =item 3. Shared data key to use as string
694  
695 =item 4. User login name
696  
697 =item 5. User display name
698  
699 =back
700  
701 =head2 C<getAvailableServices>
702  
703 Get a all available service categories in the server. Returns an array of
704 L<WWWW::Wookie::Widget::Category|WW::Wookie::Widget::Category> objects.
705 Throws a C<WookieConnectorException>.
706  
707 =head2 C<getAvailableWidgets>
708  
709 Get all available widgets in the server, or only the available widgets in the
710 specified service category. Returns an array of
711 L<WWW::Wookie::Widget|WWW::Wookie::Widget> objects, otherwise false. Throws a
712 C<WookieConnectorException>.
713  
714 =over
715  
716 =item 1. Service category name as string
717  
718 =back
719  
720 =head2 C<getWidget>
721  
722 Get the details of the widget specified by it's identifier. Returns a
723 L<WWW::Wookie::Widget|WWW::Wookie::Widget> object.
724  
725 =over
726  
727 =item 1. The identifier of an available widget
728  
729 =back
730  
731 =head2 C<getConnection>
732  
733 Get the currently active connection to the Wookie server. Returns a
734 L<WWW::Wookie::Server::Connection|WWW::Wookie::Server::Connection> object.
735  
736 =head2 C<setUser>
737  
738 Set the current user.
739  
740 =over
741  
742 =item 1. User name for the current Wookie connection
743  
744 =item 2. Screen name for the current Wookie connection
745  
746 =back
747  
748 =head2 C<getUser>
749  
750 Retrieve the details of the current user. Returns an instance of the user as a
751 L<WWW::Wookie::User|WWW::Wookie::User> object.
752  
753 =head2 C<getOrCreateInstance>
754  
755 Get or create a new instance of a widget. The current user will be added as a
756 participant. Returns a
757 L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance> object if
758 successful, otherwise false. Throws a C<WookieConnectorException>.
759  
760 =over
761  
762 =item 1. Widget as guid string or a L<WWW::Wookie::Widget|WWW::Wookie::Widget>
763 object
764  
765 =back
766  
767 =head2 C<addParticipant>
768  
769 Add a participant to a widget. Returns true if successful, otherwise false.
770 Throws a C<WookieWidgetInstanceException> or a C<WookieConnectorException>.
771  
772 =over
773  
774 =item 1. Instance of widget as
775 L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance> object
776  
777 =item 2. Instance of user as L<WWW::Wookie::User|WWW::Wookie::User> object
778  
779 =back
780  
781 =head2 C<deleteParticipant>
782  
783 Delete a participant. Returns true if successful, otherwise false. Throws a
784 C<WookieWidgetInstanceException> or a C<WookieConnectorException>.
785  
786 =over
787  
788 =item 1. Instance of widget as
789 L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance> object
790  
791 =item 2. Instance of user as L<WWW::Wookie::User|WWW::Wookie::User> object
792  
793 =back
794  
795 =head2 C<getUsers>
796  
797 Get all participants of the current widget. Returns an array of
798 L<WWW::Wookie::User|WWW::Wookie::User> instances. Throws a
799 C<WookieConnectorException>.
800  
801 =over
802  
803 =item 1. Instance of widget as
804 L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance> object
805  
806 =back
807  
808 =head2 C<addProperty>
809  
810 Adds a new property. Returns true if successful, otherwise false. Throws a
811 C<WookieConnectorException>.
812  
813 =over
814  
815 =item 1. Instance of widget as
816 L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance> object
817  
818 =item 2. Instance of property as
819 L<WWW::Wookie::Widget::Property|WWW::Wookie::Widget::Property> object
820  
821 =back
822  
823 =head2 C<setProperty>
824  
825 Set a new property. Returns the property as
826 L<WWW::Wookie::Widget::Property|WWW::Wookie::Widget::Property> if successful,
827 otherwise false. Throws a C<WookieWidgetInstanceException> or a
828 C<WookieConnectorException>.
829  
830 =over
831  
832 =item 1. Instance of widget as
833 L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance> object
834  
835 =item 2. Instance of property as
836 L<WWW::Wookie::Widget::Property|WWW::Wookie::Widget::Property> object
837  
838 =back
839  
840 =head2 C<getProperty>
841  
842 Get a property. Returns the property as
843 L<WWW::Wookie::Widget::Property|WWW::Wookie::Widget::Property> if successful,
844 otherwise false. Throws a C<WookieWidgetInstanceException> or a
845 C<WookieConnectorException>.
846  
847 =over
848  
849 =item 1. Instance of widget as
850 L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance> object
851  
852 =item 2. Instance of property as
853 L<WWW::Wookie::Widget::Property|WWW::Wookie::Widget::Property> object
854  
855 =back
856  
857 =head2 C<deleteProperty>
858  
859 Delete a property. Returns true if successful, otherwise false. Throws a
860 C<WookieWidgetInstanceException> or a C<WookieConnectorException>.
861  
862 =over
863  
864 =item 1. Instance of widget as
865 L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance> object
866  
867 =item 2. Instance of property as
868 L<WWW::Wookie::Widget::Property|WWW::Wookie::Widget::Property> object
869  
870 =back
871  
872 =head2 C<setLocale>
873  
874 Set a locale.
875  
876 =over
877  
878 =item 1. Locale as string
879  
880 =back
881  
882 =head2 C<getLocale>
883  
884 Get the current locale setting. Returns current locale as string.
885  
886 =head2 C<BUILD>
887  
888 The Moose internal BUILD method.
889  
890 =head1 CONFIGURATION AND ENVIRONMENT
891  
892 =head1 DEPENDENCIES
893  
894 =over 4
895  
896 =item * L<HTTP::Headers|HTTP::Headers>
897  
898 =item * L<HTTP::Request|HTTP::Request>
899  
900 =item * L<HTTP::Request::Common|HTTP::Request::Common>
901  
902 =item * L<HTTP::Status|HTTP::Status>
903  
904 =item * L<LWP::UserAgent|LWP::UserAgent>
905  
906 =item * L<Log::Log4perl|Log::Log4perl>
907  
908 =item * L<Moose|Moose>
909  
910 =item * L<Moose::Util::TypeConstraints|Moose::Util::TypeConstraints>
911  
912 =item * L<Readonly|Readonly>
913  
914 =item * L<Regexp::Common|Regexp::Common>
915  
916 =item * L<WWW::Wookie::Connector::Exceptions|WWW::Wookie::Connector::Exceptions>
917  
918 =item * L<WWW::Wookie::Server::Connection|WWW::Wookie::Server::Connection>
919  
920 =item * L<WWW::Wookie::User|WWW::Wookie::User>
921  
922 =item * L<WWW::Wookie::Widget::Instance|WWW::Wookie::Widget::Instance>
923  
924 =item * L<WWW::Wookie::Widget::Instances|WWW::Wookie::Widget::Instances>
925  
926 =item * L<WWW::Wookie::Widget::Property|WWW::Wookie::Widget::Property>
927  
928 =item * L<WWW::Wookie::Widget|WWW::Wookie::Widget>
929  
930 =item * L<XML::Simple|XML::Simple>
931  
932 =item * L<namespace::autoclean|namespace::autoclean>
933  
934 =back
935  
936 =head1 INCOMPATIBILITIES
937  
938 =head1 DIAGNOSTICS
939  
940 =head1 BUGS AND LIMITATIONS
941  
942 Please report any bugs or feature requests at
943 L<Bitbucket|https://bitbucket.org/rolandvanipenburg/www-wookie/issues>.
944  
945 =head1 AUTHOR
946  
947 Roland van Ipenburg, E<lt>roland@rolandvanipenburg.comE<gt>
948  
949 =head1 LICENSE AND COPYRIGHT
950  
951 Copyright 2010-2021 by Roland van Ipenburg
952  
953 This library is free software; you can redistribute it and/or modify
954 it under the same terms as Perl itself, either Perl version 5.14.0 or,
955 at your option, any later version of Perl 5 you may have available.
956  
957 =head1 DISCLAIMER OF WARRANTY
958  
959 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
960 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
961 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
962 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
963 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
964 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
965 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
966 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
967 NECESSARY SERVICING, REPAIR, OR CORRECTION.
968  
969 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
970 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
971 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENSE, BE
972 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
973 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
974 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
975 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
976 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
977 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
978 SUCH DAMAGES.
979  
980 =cut
↑ Summary blib/script/TestWookieService.pl »