MechanicalTurk/Command/DeleteHIT.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 49 CC 6 SCAR 33.6
CC
6
Cov
0%
deleteHIT
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 28.7
1 package Net::Amazon::MechanicalTurk::Command::DeleteHIT;
2 use strict;
3 use warnings;
4 use Carp;
5  
6 our $VERSION = '1.00';
7  
8 =head1 NAME
9  
10 Net::Amazon::MechanicalTurk::Command::DeleteHIT - Attempts to delete a HIT.
11  
12 =head1 SYNOPSIS
13  
14 # Deletes a hit by HITId and approves any submitted assignments for the hit.
15 $mturk->deleteHIT($hitId, 1);
16  
17 # Tries to delete a HIT, but will fail if there are any submitted assignments.
18 $mturk->deleteHIT($hitId, 0);
19  
20 =head1 C<addRetry>
21  
22 addRetry
23  
24 Deletes a HIT from MechanicalTurk. In order to delete the HIT, this fuction
25 will first try disposing the hit. If the hit can not be disposed, it will
26 attempt to force expire the hit and then dispose it. If that operation fails
27 and the autoApprove flag is on, then submitted assignments will be approved
28 and another disposal attempt will be made.
29  
30 Even, if this method fails, the HIT may have been changed. It could have
31 been force expired, but still failed disposal. Assignments may also have
32 been approved, even though the last disposal attempt failed.
33  
34 =cut
35  
36 sub deleteHIT {
37 my ($mturk, $hitId, $autoApprove) = @_;
38
39 eval { $mturk->DisposeHIT( HITId => $hitId ); };
40 return unless $@;
41
42 my $hit = $mturk->GetHIT( HITId => $hitId );
43 my $status = $hit->{HITStatus}[0];
44
45 return if ($status eq "Disposed");
46
47 # Try to expire the HIT and then dispose it
48 eval { $mturk->ForceExpireHIT( HITId => $hitId ); };
49 eval { $mturk->DisposeHIT( HITId => $hitId ); };
50 return unless $@;
51
52 # Approve all submitted hits
53 if ($autoApprove) {
54 my $assignments = $mturk->GetAssignmentsForHITAll(
55 HITId => $hitId,
56 AssignmentStatus => 'Submitted'
57 );
58 while (my $assignment = $assignments->next) {
59 my $assignmentId = $assignment->{AssignmentId}[0];
60 $mturk->ApproveAssignment( AssignmentId => $assignmentId );
61 }
62 }
63 else {
64 Carp::croak("Could not delete hit $hitId with autoApproval off.");
65 }
66
67 eval { $mturk->DisposeHIT( HITId => $hitId ); };
68 return unless $@;
69
70 Carp::croak("Could not delete hit, it may be in the process of being worked on.\n\n" . $@);
71 }
72  
73 return 1;