blib/lib/WebService/Redmine.pm

filter: Statementstmt 70.3% 114 / 162 Branchbran 57.8% 37 / 64 Conditioncond 46.4% 26 / 56 MC/DCmcdc 37.1% 13 / 35 Subroutinesub 80.9% 17 / 21 Podpod 100% 3 / 3 totaltotal 61.5% 210 / 341 CC 56 SCAR 33.3
CC
56
Cov
63%
_response
90.0
_request
72.0
new
24.6
CRAP 27.9
↑ Summary
1 package WebService::Redmine;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 our $VERSION = '0.06';
8  
9 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use JSON::XS qw/encode_json decode_json/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Encode qw/decode/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =pod
16  
17 =encoding UTF-8
18  
19 =head1 NAME
20  
21 WebService::Redmine - Wrapper for RedMine REST API (http://www.redmine.org/projects/redmine/wiki/Rest_api).
22  
23 =head1 SYNOPSIS
24  
25 use WebService::Redmine;
26 my $redminer = WebService::Redmine->new(
27 host => 'example.com/redmine',
28 key => 'xxx',
29 );
30 # password-based auth is also supported:
31 #my $redminer = WebService::Redmine->new(
32 # host => 'example.com/redmine',
33 # user => 'redminer',
34 # pass => 'p@s$w0rD',
35 #);
36  
37 my $project = $redminer->createProject({ project => {
38 identifier => 'my-project',
39 name => 'My Project',
40 description => 'My project, created with *WebService::Redmine*',
41 }});
42 if (!$project) {
43 say STDERR 'Error(s) creating project: ', join("\n", map { $_ } @{ $redminer->errorDetails->{errors} });
44 exit 1;
45 }
46 my $project_id = $project->{project}{id};
47  
48 $redminer->updateProject($project_id, { project => {
49 parent_id => 42, # Make a project with numeric ID 42 parent for $project_id
50 inherit_members => 1, # Inherit all members and their permissions from the parent
51 }});
52
53 my $issue = $redminer->createIssue({ issue => {
54 project_id => $project_id,
55 subject => 'Test issue for WebService::Redmine',
56 description => 'Issue description',
57 }});
58  
59 $redminer->deleteProject($project_id);
60  
61 =head1 DESCRIPTION
62  
63 This module is a client for RedMine REST API. Please note that although
64 RedMine API is designed to support both JSON and XML, this module is B<JSON only>.
65  
66 =head1 METHODS NAMING AND OTHER CALL CONVENTIONS
67  
68 All methods are dynamically converted to actual HTTP requests using following conventions.
69  
70 =head2 Getting a Collection of Objects
71  
72 $redminer->projects; # ->users, ->issues, ->timeEntries ...
73 $redminer->getProjects; # ->getUsers, ->getIssues, ->getTimeEntries ...
74 $redminer->readProjects; # ->readUsers, ->readIssues, ->readTimeEntries ...
75
76 # Second page when displaying 10 items per page:
77 $redminer->projects({ offset => 9, limit => 10 });
78  
79 # Filtering issues:
80 $redminer->issues({ project_id => 42, assigned_to_id => 'me' });
81  
82 =head2 Getting an Object
83  
84 $redminer->project(1); # ->user(1), ->issue(1), ->timeEntry(1) ...
85 $redminer->getProject(1); # ->getUser(1), ->getIssue(1), ->getTimeEntry(1) ...
86 $redminer->readProject(1); # ->readUsers(1), ->readIssue(1), ->readTimeEntry(1) ...
87
88 # Showing an object with additional metadata:
89 $redminer->issue(1, { include => 'relations,changesets' });
90  
91 =head2 Creating an Object
92  
93 $redminer->createProject({
94 # ...
95 }); # ->createUser, ->createIssue, ->createTimeEntry ...
96  
97 =head2 Updating an Object
98  
99 $redminer->updateProject(1, {
100 # ...
101 }); # ->updateUser(...), ->updateIssue(...), ->updateTimeEntry(...) ...
102  
103 =head2 Deleting an Object
104  
105 $redminer->deleteProject(1); # ->deleteUser(1), ->deleteIssue(1), ->deleteTimeEntry(1) ...
106  
107 =head2 Objects Belonging to Other Objects
108  
109 #
110 # Example for project membership(s)
111 #
112 my $project_id = 42;
113 my $membership_id = 42;
114  
115 # Listing *project* memberships and creating a membership within a *project*
116 # require identifying a project and thus have to be spelled like this:
117 $redminer->projectMemberships($project_id, { limit => 50 });
118 $redminer->createProjectMembership($project_id, { ... });
119  
120 # Viewing/Updating/Deleting a membership is performed directly by its ID, thus:
121 my $membership = $redminer->membership($membership_id);
122 $redminer->updateMembership($membership_id, { ... });
123 $redminer->deleteMembership($membership_id);
124  
125 =head2 Complex Object Names
126  
127 Such complex names as C<TimeEntry> which should be dispatched to C<time_entries>
128 are recognized and thus can be spelled in CamelCase (see examples above).
129 If this is not the case, please report bugs.
130  
131 =head2 Return Values
132  
133 All successfull calls return hash references. For C<update*> and C<delete*> calls
134 hash references are empty.
135  
136 If a call fails, C<undef> is returned. In this case detailed error information can
137 be retrieved using C<errorDetails> method:
138
139 if (!$redminer->deleteIssue(42)) {
140 my $details = $redminer->errorDetails;
141 # Process $details here...
142 }
143  
144 =head1 METHODS
145  
146 =head2 new
147  
148 my $redminer = WebService::Redmine->new(%options);
149  
150 Following options are recognized:
151  
152 =over
153  
154 =item *
155  
156 B<host>: RedMine host. Beside host name, may include port, path and/or URL scheme (C<http> is used by default).
157  
158 =item *
159  
160 B<key>: API key. For details, please refer to http://www.redmine.org/projects/redmine/wiki/Rest_api#Authentication
161  
162 =item *
163  
164 B<user>, B<pass>: User name and password for password-based authentication
165  
166 =item *
167  
168 B<work_as>: User login for impersonation. For details, please refer to http://www.redmine.org/projects/redmine/wiki/Rest_api#User-Impersonation.
169  
170 =item *
171  
172 B<no_wrapper_object>: Automatically add/remove wrapper object for data. See below.
173  
174 =back
175  
176 =head3 no_wrapper_object
177  
178 By default RedMine API requires you to wrap you object data:
179  
180 my $project = $redminer->createProject({
181 project => {
182 identifier => 'some-id',
183 name => 'Some Name',
184 }
185 });
186 # $project contains something like
187 # { project => { id => 42, identifier => 'some-id', name => 'Some Name' ... } }
188  
189 By default this module follows this convention. However, if you turn on
190 the C<no_wrapper_object> flag
191  
192 my $redminer = WebService::Redmine->new(
193 host => 'example.com/redmine',
194 key => 'xxx',
195 no_wrapper_object => 1,
196 );
197  
198 you can skip "wrapping" object data, which results in simpler data structures:
199  
200 my $project = $redminer->createProject({
201 identifier => 'some-id',
202 name => 'Some Name',
203 });
204 # $project contains something like
205 # { id => 42, identifier => 'some-id', name => 'Some Name' ... }
206  
207 Please note that wrapping can be skipped only while operating on single objects,
208 i.e. this flag is honored for C<create*> and C<update*> requests as well as for
209 C<get>ting individual objects. This flag is ignored for C<delete*> calls and calls
210 like C<issues>.
211  
212 =cut
213  
214 sub new
215 1 ▶{
Subroutine new: called CC=12 SCAR=32.0
Pod documented
216 1 my $class = shift;
217 1 my %arg = @_;
218
219 my $self = {
220 error => '',
221 1 ▶ protocol => $arg{protocol} // 'http',
Condition50%1/2
$arg{"protocol"} // "http"
l!l
01
MC/DC0%0/1
$arg{"protocol"} // "http"
$arg{"protocol"}
Truth table50%1/2
A
A= $arg{"protocol"}
Aresult
11
00
222 ua => LWP::UserAgent->new,
223 };
224  
225 1 foreach my $param (qw/host user pass key work_as no_wrapper_object/) {
226 6 ▶ $self->{$param} = $arg{$param} // '';
Condition100%2/2
$arg{$param} // ""
l!l
24
MC/DC100%1/1
$arg{$param} // ""
$arg{$param}
Truth table100%2/2
A
A= $arg{$param}
Aresult
11
00
227 }
228  
229 1 ▶ if (length $self->{host} && $self->{host} =~ m|^(https?)://|i) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{'host'} and $self->{'host'} =~ m[^(https?)://]i) { } 0 1 1
Condition33%1/3
length $self->{"host"} and $self->{"host"} =~ m[^(https?)://]i
!ll&&!rl&&r
100
MC/DC0%0/2
length $self->{"host"} and $self->{"host"} =~ m[^(https?)://]i
length $self->{"host"} $self->{"host"} =~ m[^(https?)://]i
Truth table33%1/3
A and B
A= length $self->{"host"}B= $self->{"host"} =~ m[^(https?)://]i
ABresult
0X0
100
111
230 0 $self->{protocol} = lc $1;
231 0 $self->{host} =~ s/^https?://i;
232 } else {
233 1 ▶ $self->{protocol} = 'http' if $self->{protocol} !~ /^https?$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"protocol"} =~ /^https?$/i 0 1 1
234 }
235  
236 1 my $auth = '';
237 1 ▶ if (!length $self->{key} && length $self->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not length $self->{"key"} and length $self->{"user"}) 0 1 1
Condition33%1/3
not length $self->{"key"} and length $self->{"user"}
!ll&&!rl&&r
010
MC/DC0%0/2
not length $self->{"key"} and length $self->{"user"}
not length $self->{"key"} length $self->{"user"}
Truth table33%1/3
not(A) and B
A= not length $self->{"key"}B= length $self->{"user"}
ABresult
0X0
100
111
238 0 $auth = $self->{user};
239 0 ▶ if (length $self->{pass}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"pass"}) 0 0 0
240 0 $auth .= ':' . $self->{pass};
241 }
242 0 $auth .= '@';
243 }
244 1 $self->{uri} = "$self->{protocol}://$auth$self->{host}";
245  
246 1 $self->{ua}->default_header('Content-Type' => 'application/json');
247
248 1 ▶ if (length $self->{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"key"}) 0 1 1
249 0 $self->{ua}->default_header('X-Redmine-API-Key' => $self->{key});
250 }
251 1 ▶ if (length $self->{work_as}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"work_as"}) 0 1 1
252 0 $self->{ua}->default_header('X-Redmine-Switch-User' => $self->{work_as});
253 }
254  
255 1 bless $self, $class;
256  
257 1 return $self;
258 }
259  
260 =head2 error
261  
262 Error during the last call. This is an empty string for successfull calls, otherwise
263 it contains an HTTP status line.
264  
265 If the call failed before sending an actual request (e.g. method name could not
266 be dispatched into an HTTP request), contains description of the client error.
267  
268 =cut
269  
270 0 ▶sub error { return $_[0]->{error} }
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
271  
272 =head2 errorDetails
273  
274 Contains detailed error messages from the last call. This is an empty hash reference
275 for successfull calls, otherwise please see http://www.redmine.org/projects/redmine/wiki/Rest_api#Validation-errors.
276  
277 If the call failed before sending an actual request (e.g. method name could not
278 be dispatched into an HTTP request), return value is
279  
280 {
281 client_error => 1
282 }
283  
284 =cut
285  
286 0 ▶sub errorDetails { return $_[0]->{error_details} }
Subroutine errorDetails: not called CC=1 SCAR=6.9
Pod documented
287  
288 8 ▶sub _set_error { $_[0]->{error} = $_[1] // ''; return; }
Subroutine _set_error: called CC=2 SCAR=7.2
Condition50%1/2
$_[1] // ""
l!l
80
MC/DC0%0/1
$_[1] // ""
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
289  
290 sub _set_client_error
291 8 ▶{
Subroutine _set_client_error: called CC=1 SCAR=0.0
292 8 my $self = shift;
293 8 my $error = shift;
294  
295 $self->{error_details} = {
296 8 client_error => 1
297 };
298  
299 8 return $self->_set_error($error);
300 }
301  
302 sub AUTOLOAD
303 1 ▶{
Subroutine AUTOLOAD: called CC=2 SCAR=7.4
304 1 our $AUTOLOAD;
305 1 my $self = shift;
306 1 my $method = substr($AUTOLOAD, length(__PACKAGE__) + 2);
307 1 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 1 0 1
308 0 return $self->_response($self->_request($method, @_));
309 }
310  
311 sub _request
312 0 ▶{
Subroutine _request: not called CC=8 SCAR=42.8
313 0 my $self = shift;
314 0 ▶ my $r = $self->_dispatch_name(@_) // return;
Condition0%0/2
$self->_dispatch_name(@_) // (return)
l!l
00
MC/DC0%0/1
$self->_dispatch_name(@_) // (return)
$self->_dispatch_name(@_)
Truth table0%0/2
A
A= $self->_dispatch_name(@_)
Aresult
11
00
315  
316 0 $self->_set_error;
317  
318 0 my $uri = URI->new(sprintf('%s/%s.json', $self->{uri}, $r->{path}));
319 0 ▶ if ($r->{method} eq 'GET' && ref $r->{query} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"method"} eq "GET" and ref $r->{"query"} eq "HASH") 0 0 0
Condition0%0/3
$r->{"method"} eq "GET" and ref $r->{"query"} eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$r->{"method"} eq "GET" and ref $r->{"query"} eq "HASH"
$r->{"method"} eq "GET" ref $r->{"query"} eq "HASH"
Truth table0%0/3
A and B
A= $r->{"method"} eq "GET"B= ref $r->{"query"} eq "HASH"
ABresult
0X0
100
111
320 0 foreach my $param (keys %{ $r->{query} }) {
321 # 2DO: implement passing arrays as foo=1&foo=2&foo=3 if needed
322 0 $uri->query_param($param => $r->{query}{$param});
323 }
324 }
325  
326 0 my $request = HTTP::Request->new($r->{method}, $uri);
327  
328 0 ▶ if ($r->{method} ne 'GET' && defined $r->{content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"method"} ne "GET" and defined $r->{"content"}) 0 0 0
Condition0%0/3
$r->{"method"} ne "GET" and defined $r->{"content"}
!ll&&!rl&&r
000
MC/DC0%0/2
$r->{"method"} ne "GET" and defined $r->{"content"}
$r->{"method"} ne "GET" defined $r->{"content"}
Truth table0%0/3
A and B
A= $r->{"method"} ne "GET"B= defined $r->{"content"}
ABresult
0X0
100
111
329 0 my $json = eval { Encode::decode('UTF-8', JSON::XS::encode_json($r->{content})) };
330 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
331 0 return $self->_set_client_error('Malformed input data:' . $@);
332 }
333 0 $request->header('Content-Length' => length $json);
334 0 $request->content($json);
335 }
336  
337 0 return $request;
338 }
339  
340 sub _response
341 0 ▶{
Subroutine _response: not called CC=9 SCAR=45.0
342 0 my $self = shift;
343 0 ▶ my $request = shift // return;
Condition0%0/2
shift() // (return)
l!l
00
MC/DC0%0/1
shift() // (return)
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
344 0 my $response = $self->{ua}->request($request);
345  
346 0 ▶ if (!$response->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($response->is_success) 0 0 0
347 0 ▶ $self->{error_details} = eval {
Condition0%0/2
eval { do { &JSON::XS::decode_json(scalar $response->decoded_content) } } // {}
l!l
00
MC/DC0%0/1
eval { do { &JSON::XS::decode_json(scalar $response->decoded_content) } } // {}
eval { do { &JSON::XS::decode_json(scalar $response->decoded_content) } }
Truth table0%0/2
A
A= eval { do { &JSON::XS::decode_json(scalar $response->decoded_content) } }
Aresult
11
00
348 0 JSON::XS::decode_json($response->decoded_content)
349 } // {};
350 0 return $self->_set_error($response->status_line);
351 }
352  
353 0 $self->{error_details} = {};
354  
355 0 ▶ if ($request->method eq 'PUT' || $request->method eq 'DELETE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->method eq "PUT" or $request->method eq "DELETE") 0 0 0
Condition0%0/3
$request->method eq "PUT" or $request->method eq "DELETE"
l!l&&r!l&&!r
000
MC/DC0%0/2
$request->method eq "PUT" or $request->method eq "DELETE"
$request->method eq "PUT" $request->method eq "DELETE"
Truth table0%0/3
A or B
A= $request->method eq "PUT"B= $request->method eq "DELETE"
ABresult
1X1
011
000
356 0 return {};
357 }
358  
359 0 my $content = eval { JSON::XS::decode_json($response->decoded_content) };
360 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
361 0 return $self->_set_error($@);
362 }
363  
364 0 ▶ if ($self->{expect_single_object} && $self->{no_wrapper_object}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"expect_single_object"} and $self->{"no_wrapper_object"}) 0 0 0
Condition0%0/3
$self->{"expect_single_object"} and $self->{"no_wrapper_object"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"expect_single_object"} and $self->{"no_wrapper_object"}
$self->{"expect_single_object"} $self->{"no_wrapper_object"}
Truth table0%0/3
A and B
A= $self->{"expect_single_object"}B= $self->{"no_wrapper_object"}
ABresult
0X0
100
111
365 0 $content = delete $content->{$self->{expect_single_object}};
366 }
367  
368 0 return $content;
369 }
370  
371 sub _dispatch_name
372 29 ▶{
Subroutine _dispatch_name: called CC=18 SCAR=28.9
373 29 my $self = shift;
374 29 ▶ my $name = shift // return $self->_set_client_error('Undefined method name');
Condition100%2/2
shift() // (return $self->_set_client_error("Undefined method name"))
l!l
281
MC/DC100%1/1
shift() // (return $self->_set_client_error("Undefined method name"))
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
375 28 my @args = @_;
376  
377 28 my ($action, $objects) = ($name =~ /^(get|read|create|update|delete)?([A-Za-z]+?)$/x);
378
379 28 ▶ if (!$action || $action eq 'read') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $action or $action eq "read") 12 16 28
Condition100%3/3
not $action or $action eq "read"
l!l&&r!l&&!r
10216
MC/DC100%2/2
not $action or $action eq "read"
not $action $action eq "read"
Truth table100%3/3
not(A) or B
A= not $actionB= $action eq "read"
ABresult
1X1
011
000
380 12 $action = 'get';
381 }
382 28 ▶ if (!$objects) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($objects) 1 27 28
383 1 return $self->_set_client_error("Malformed method name '$name'");
384 }
385  
386 27 my %METHOD = (
387 get => 'GET' ,
388 create => 'POST' ,
389 update => 'PUT' ,
390 delete => 'DELETE',
391 );
392  
393 my $data = {
394 27 method => $METHOD{$action},
395 path => '',
396 content => undef,
397 query => undef,
398 };
399  
400 27 ▶ if ($action eq 'get') {
Branch: 2 branches
BranchTrueFalseTotal
if ($action eq 'get') { } 13 14 27
elsif ($action eq 'create' or $action eq 'update') { } 11 3 14
401 13 ▶ if (ref $args[-1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[-1] eq "HASH") 10 3 13
402 # If last argument is a hash reference, treat it as a filtering clause:
403 10 $data->{query} = pop @args;
404 }
405 ▶ } elsif ($action eq 'create' || $action eq 'update') {
Condition100%3/3
$action eq "create" or $action eq "update"
l!l&&r!l&&!r
743
MC/DC100%2/2
$action eq "create" or $action eq "update"
$action eq "create" $action eq "update"
Truth table100%3/3
A or B
A= $action eq "create"B= $action eq "update"
ABresult
1X1
011
000
406 # If last argument is an array/hash reference, treat it as a request body:
407 11 ▶ if (ref $args[-1] ne 'ARRAY' && ref $args[-1] ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[-1] ne "ARRAY" and ref $args[-1] ne "HASH") 4 7 11
Condition66%2/3
ref $args[-1] ne "ARRAY" and ref $args[-1] ne "HASH"
!ll&&!rl&&r
074
MC/DC50%1/2
ref $args[-1] ne "ARRAY" and ref $args[-1] ne "HASH"
ref $args[-1] ne "ARRAY" ref $args[-1] ne "HASH"
Truth table66%2/3
A and B
A= ref $args[-1] ne "ARRAY"B= ref $args[-1] ne "HASH"
ABresult
0X0
100
111
408 4 return $self->_set_client_error(
409 'No data provided for a create/update method'
410 );
411 }
412 7 $data->{content} = pop @args;
413 }
414  
415 23 $objects = $self->_normalize_objects($objects);
416 23 my $i = 0;
417 23 my @objects;
418 23 ▶ while ($objects =~ /([A-Z][a-z]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($objects =~ /([A-Z][a-z]+)/g) 29 21 50
419 29 my $object = $self->_object($1);
420 29 my $category = $self->_category($object);
421
422 29 push @objects, $category;
423  
424 29 ▶ next if $object eq $category;
Branch: 1 branch
BranchTrueFalseTotal
if $object eq $category 9 20 29
425  
426 20 my $is_last_object = pos($objects) == length($objects);
427  
428 # We need to attach an object ID to the path if an object is singular and
429 # we either perform anything but creation or we create a new object inside
430 # another object (e.g. createProjectMembership)
431 20 ▶ if ($action ne 'create' || !$is_last_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action ne "create" or not $is_last_object) 15 5 20
Condition100%3/3
$action ne "create" or not $is_last_object
l!l&&r!l&&!r
1235
MC/DC100%2/2
$action ne "create" or not $is_last_object
$action ne "create" not $is_last_object
Truth table100%3/3
A or not(B)
A= $action ne "create"B= not $is_last_object
ABresult
1X1
011
000
432 15 my $object_id = $args[$i++];
433  
434 15 ▶ return $self->_set_client_error(
Branch: 1 branch
BranchTrueFalseTotal
if not defined $object_id or ref \$object_id ne "SCALAR" 2 13 15
Condition66%2/3
not defined $object_id or ref \$object_id ne "SCALAR"
l!l&&r!l&&!r
2013
MC/DC50%1/2
not defined $object_id or ref \$object_id ne "SCALAR"
not defined $object_id ref \$object_id ne "SCALAR"
Truth table66%2/3
not(A) or B
A= not defined $object_idB= ref \$object_id ne "SCALAR"
ABresult
1X1
011
000
435 sprintf 'Incorrect object ID for %s in query %s', $object, $name
436 ) if !defined $object_id || ref \$object_id ne 'SCALAR';
437  
438 13 push @objects, $object_id;
439 }
440  
441 18 ▶ $self->_dispatch_last_object($action, $object, $data) if $is_last_object;
Branch: 1 branch
BranchTrueFalseTotal
if $is_last_object 12 6 18
442 }
443
444 21 $data->{path} = join '/', @objects;
445  
446 21 return $data;
447 }
448  
449 sub _dispatch_last_object
450 12 ▶{
Subroutine _dispatch_last_object: called CC=7 SCAR=22.0
451 12 my $self = shift;
452 12 my $action = shift;
453 12 my $object = shift;
454 12 my $data = shift;
455  
456 12 delete $self->{expect_single_object};
457  
458 12 ▶ if (length $object) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $object) 12 0 12
459 12 ▶ if ($action eq 'get' || $action eq 'create') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "get" or $action eq "create") 7 5 12
Condition100%3/3
$action eq "get" or $action eq "create"
l!l&&r!l&&!r
255
MC/DC100%2/2
$action eq "get" or $action eq "create"
$action eq "get" $action eq "create"
Truth table100%3/3
A or B
A= $action eq "get"B= $action eq "create"
ABresult
1X1
011
000
460 7 $self->{expect_single_object} = $object;
461 }
462 12 ▶ if ($self->{no_wrapper_object}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_wrapper_object"}) 0 12 12
463 0 ▶ if ($action eq 'create' || $action eq 'update') {
Branch: 1 branch
BranchTrueFalseTotal
if ($action eq "create" or $action eq "update") 0 0 0
Condition0%0/3
$action eq "create" or $action eq "update"
l!l&&r!l&&!r
000
MC/DC0%0/2
$action eq "create" or $action eq "update"
$action eq "create" $action eq "update"
Truth table0%0/3
A or B
A= $action eq "create"B= $action eq "update"
ABresult
1X1
011
000
464 # Wrap object data unless we pass everything as is:
465 0 $data->{content} = { $object => $data->{content} };
466 }
467 }
468 }
469  
470 12 return 1;
471 }
472  
473 sub _normalize_objects
474 23 ▶{
Subroutine _normalize_objects: called CC=1 SCAR=0.0
475 23 my $self = shift;
476 23 my $objects = shift;
477  
478 23 $objects = ucfirst $objects;
479 # These are tokens that form a *single* entry in the resulting request path,
480 # e.g.: PUT /time_entries/1.json
481 # But it is natural to spell them like this:
482 # $api->updateTimeEntry(1, { ... });
483 23 $objects =~ s/TimeEntr/Timeentr/g;
484 23 $objects =~ s/IssueCategor/Issuecategor/g;
485 23 $objects =~ s/IssueStatus/Issuestatus/g;
486 23 $objects =~ s/CustomField/Customfield/g;
487  
488 23 return $objects;
489 }
490  
491 sub _object
492 29 ▶{
Subroutine _object: called CC=1 SCAR=0.0
493 29 my $self = shift;
494 29 my $object = lc(shift);
495
496 # Process compound words:
497 29 $object =~ s/timeentr/time_entr/igx;
498 29 $object =~ s/issue(categor|status)/issue_$1/igx;
499 29 $object =~ s/customfield/custom_field/igx;
500
501 29 return $object;
502 }
503  
504 # If an object is singular, pluralize it to make its category name: user -> users
505 sub _category
506 29 ▶{
Subroutine _category: called CC=5 SCAR=16.3
507 29 my $self = shift;
508 29 my $object = shift;
509  
510 29 my $category = $object;
511  
512 29 ▶ if ($category !~ /s$/ || $category =~ /us$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $category =~ /s$/ or $category =~ /us$/) 20 9 29
Condition66%2/3
not $category =~ /s$/ or $category =~ /us$/
l!l&&r!l&&!r
2009
MC/DC50%1/2
not $category =~ /s$/ or $category =~ /us$/
not $category =~ /s$/ $category =~ /us$/
Truth table66%2/3
not(A) or B
A= not $category =~ /s$/B= $category =~ /us$/
ABresult
1X1
011
000
513 20 ▶ if ($object =~ /y$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($object =~ /y$/) { } 5 15 20
elsif ($category =~ /us$/) { } 0 15 15
514 5 $category =~ s/y$/ies/;
515 } elsif ($category =~ /us$/) {
516 0 $category .= 'es';
517 } else {
518 15 $category .= 's';
519 }
520 }
521  
522 29 return $category;
523 }
524  
525 =head1 SEE ALSO
526  
527 Redmine::API (https://metacpan.org/pod/Redmine::API). Major differences
528 between this module and Redmine::API are:
529  
530 =over
531  
532 =item *
533  
534 B<Dependencies>. Redmine::API depends on Moo and REST::Client which in turn depends on
535 LWP::UserAgent, URI and possibly others. WebService::Redmine uses pure Perl OOP and
536 depends directly on LWP::UserAgent and URI.
537  
538 =item *
539  
540 B<Call conventions>. Although both modules use dynamic dispatching for building actual HTTP
541 requests, they do it in a different manner. In particular, WebService::Redmine tries to
542 dispatch a single method name without using chains of interrim objects as Redmine::API does.
543  
544 =back
545  
546 Fork this project on GitHub: https://github.com/igelhaus/redminer
547  
548 =head1 AUTHOR
549  
550 Anton Soldatov, E<lt>igelhaus@gmail.comE<gt>
551  
552 =head1 COPYRIGHT AND LICENSE
553  
554 Copyright (C) 2014 by Anton Soldatov
555  
556 This library is free software; you can redistribute it and/or modify
557 it under the same terms as Perl itself, either Perl version 5.10.0 or,
558 at your option, any later version of Perl 5 you may have available.
559  
560 =cut
561  
562 3 1;
563  
564 __END__
↑ Summary