File Coverage

lib/Smartcat/App/DocumentApi.pm
Criterion Covered Total %
statement 33 62 53.2
branch 0 16 0.0
condition 0 3 0.0
subroutine 11 14 78.5
pod 0 4 0.0
total 44 99 44.4


line stmt bran cond sub pod time code
1 2     2   15 use strict;
  2         4  
  2         64  
2 2     2   12 use warnings;
  2         3  
  2         56  
3              
4 2     2   10 use utf8;
  2         3  
  2         11  
5 2     2   41 no utf8;
  2         32  
  2         9  
6              
7             package Smartcat::App::DocumentApi;
8              
9 2     2   100 use Smartcat::Client::DocumentApi;
  2         4  
  2         72  
10              
11 2     2   12 use Smartcat::Client::Object::BilingualFileImportSetingsModel;
  2         3  
  2         13  
12 2     2   121 use Smartcat::Client::Object::UploadDocumentPropertiesModel;
  2         4  
  2         38  
13 2     2   110 use Smartcat::App::Utils;
  2         4  
  2         224  
14              
15 2     2   16 use Carp;
  2         4  
  2         157  
16             $Carp::Internal{ ('Smartcat::Client::DocumentApi') }++;
17             $Carp::Internal{ (__PACKAGE__) }++;
18              
19 2     2   13 use Log::Any qw($log);
  2         3  
  2         11  
20              
21             sub new {
22 1     1 0 5 my ( $class, $api, $rundata ) = @_;
23              
24 1         10 my $self = bless(
25             {
26             api => Smartcat::Client::DocumentApi->new($api),
27             rundata => $rundata
28             },
29             $class
30             );
31              
32 1         323 return $self;
33             }
34              
35             sub update_document {
36 0     0 0   my ( $self, $path, $document_id ) = @_;
37 0 0 0       return unless $path && $document_id;
38              
39 0           my $settings =
40             Smartcat::Client::Object::BilingualFileImportSetingsModel->new(
41             confirmMode => "atLastStage" );
42 0           my $doc_props =
43             Smartcat::Client::Object::UploadDocumentPropertiesModel->new(
44             bilingualFileImportSetings => $settings );
45              
46 0           $log->info("Updating document '$document_id' with '$path'...");
47 0           my $utf8_path = $path;
48 0           utf8::encode($utf8_path);
49 0           my %args = (
50             document_id => $document_id,
51             update_document_model => $doc_props,
52             file => $utf8_path
53             );
54             $args{disassemble_algorithm_name} =
55             $self->{rundata}->{disassemble_algorithm_name}
56 0 0         if defined $self->{rundata}->{disassemble_algorithm_name};
57             $args{preset_disassemble_algorithm} =
58             $self->{rundata}->{preset_disassemble_algorithm}
59 0 0         if defined $self->{rundata}->{preset_disassemble_algorithm};
60              
61 0           my $document = eval { $self->{api}->document_update(%args) };
  0            
62 0 0         die $log->error(
63             sprintf(
64             "Failed to update document '%s' with '%s'.\nError:\n%s",
65             $document_id, $path, format_error_message($@)
66             )
67             ) unless $document;
68             }
69              
70             sub get_document {
71 0     0 0   my ( $self, $document_id ) = @_;
72 0 0         return unless $document_id;
73              
74 0           $log->info("Getting document '$document_id'...");
75 0           my %args = ( document_id => $document_id, );
76              
77 0           my $document = eval { $self->{api}->document_get(%args); };
  0            
78 0 0         die $log->error(
79             sprintf(
80             "Failed to get document '%s'.\nError:\n%s",
81             $document_id, format_error_message($@)
82             )
83             ) unless $document;
84              
85 0           return $document;
86             }
87              
88             sub delete_documents {
89 0     0 0   my ( $self, $document_ids ) = @_;
90 0 0         return unless $document_ids;
91              
92 0           $log->info( 'Deleting documents: ' . join( ', ', @$document_ids ) . '...' );
93 0           my %args = ( document_ids => $document_ids, );
94              
95 0           eval { $self->{api}->document_delete(%args); };
  0            
96 0 0         die $log->error(
97             sprintf(
98             "Failed to delete documents: %s.\nError:\n%s",
99             join( ', ', @$document_ids ),
100             format_error_message($@)
101             )
102             ) if $@;
103              
104 0           return;
105             }
106              
107             1;