File Coverage

lib/Smartcat/App.pm
Criterion Covered Total %
statement 98 98 100.0
branch 10 12 83.3
condition 2 6 33.3
subroutine 27 27 100.0
pod 1 5 20.0
total 138 148 93.2


line stmt bran cond sub pod time code
1              
2             =begin comment
3              
4             Smartcat App
5              
6             A simple command line application which allows to sync local files and Smartcat project content.
7              
8             =end comment
9              
10             =cut
11              
12 2     2   154130 use strict;
  2         12  
  2         62  
13 2     2   11 use warnings;
  2         2  
  2         93  
14              
15             package Smartcat::App;
16              
17 2     2   983 use App::Cmd::Setup -app;
  2         75452  
  2         20  
18              
19 2     2   469 use Class::Load;
  2         5  
  2         81  
20 2     2   1515 use Data::Dumper;
  2         14665  
  2         167  
21 2     2   1071 use File::Copy qw/copy/;
  2         5018  
  2         156  
22 2     2   16 use File::Spec::Functions qw(catfile);
  2         12  
  2         97  
23 2     2   1579 use IO::Uncompress::Unzip qw($UnzipError);
  2         124300  
  2         249  
24 2     2   2399 use JSON qw/encode_json/;
  2         22685  
  2         16  
25              
26 2     2   1905 use Smartcat::Client::ProjectApi;
  2         172800  
  2         94  
27 2     2   1513 use Smartcat::Client::DocumentApi;
  2         10406  
  2         92  
28 2     2   1342 use Smartcat::Client::DocumentExportApi;
  2         3041  
  2         65  
29              
30 2     2   1127 use Smartcat::Client::Object::BilingualFileImportSetingsModel;
  2         1086336  
  2         17  
31 2     2   1316 use Smartcat::Client::Object::CreateDocumentPropertyModel;
  2         5184  
  2         13  
32 2     2   1342 use Smartcat::Client::Object::UploadDocumentPropertiesModel;
  2         4086  
  2         11  
33              
34 2     2   981 use Smartcat::App::ProjectApi;
  2         5  
  2         74  
35 2     2   843 use Smartcat::App::DocumentApi;
  2         5  
  2         61  
36 2     2   856 use Smartcat::App::DocumentExportApi;
  2         5  
  2         61  
37              
38 2     2   781 use Smartcat::App::Config;
  2         5  
  2         17  
39 2     2   2758 use Log::Log4perl qw(:easy);
  2         119897  
  2         12  
40 2     2   1632 use Log::Any qw($log);
  2         4  
  2         20  
41 2     2   1870 use Log::Any::Adapter;
  2         766  
  2         10  
42              
43             our $VERSION = '0.0.11';
44              
45             sub init {
46 2     2 0 6 my $self = shift;
47              
48 2         75 my $base_url = undef;
49 2 50 33     13 $base_url = $self->{config}->base_url if ($self->{config}->base_url && $self->{config}->base_url ne '');
50              
51             $self->{api} = Smartcat::Client::ApiClient->new(
52             username => $self->{config}->username,
53             password => $self->{config}->password,
54 2         79 base_url => $base_url,
55             );
56              
57 2         4491 my $log_level = "INFO";
58 2 100       12 if ( defined $self->{rundata}->{debug} ) {
59 1         3 $log_level = "DEBUG";
60 1         8 print "*** DEBUG mode is turned on ***\n";
61             }
62              
63 2         27 my $appender = "";
64 2         6 my $filter = <<"EOT";
65             log4perl.filter.MatchWarnInfoDebug = Log::Log4perl::Filter::LevelRange
66             log4perl.filter.MatchWarnInfoDebug.LevelMin = DEBUG
67             log4perl.filter.MatchWarnInfoDebug.LevelMax = WARN
68             log4perl.appender.screen.Filter = MatchWarnInfoDebug
69             EOT
70 2         4 my $logger = "";
71 2 50 33     15 if ( defined $self->{config}->log && $self->{config}->log ne "" ) {
72 2         84 print "*** Printing to '$self->{config}->{log}' log file ***\n";
73 2         48 $logger = ", file";
74 2         7 $appender = <<"EOT";
75             log4perl.appender.file = Log::Log4perl::Appender::File
76             log4perl.appender.file.filename = $self->{config}->{log}
77             log4perl.appender.file.mode = append
78             log4perl.appender.file.layout = PatternLayout
79             log4perl.appender.file.layout.ConversionPattern = [%d] %p> %m%n
80             EOT
81 2         19 $filter = <<"EOT";
82             log4perl.filter.MatchWarnInfo = Log::Log4perl::Filter::LevelRange
83             log4perl.filter.MatchWarnInfo.LevelMin = INFO
84             log4perl.filter.MatchWarnInfo.LevelMax = WARN
85             log4perl.appender.screen.Filter = MatchWarnInfo
86             EOT
87              
88             }
89              
90 2         40 Log::Log4perl->init( \ <<"EOT");
91             log4perl.logger = $log_level, screen $logger
92              
93             log4perl.appender.screen = Log::Log4perl::Appender::Screen
94             log4perl.appender.screen.stderr = 0
95             log4perl.appender.screen.layout = PatternLayout
96             log4perl.appender.screen.layout.ConversionPattern = %m%n
97             $filter
98              
99             $appender
100              
101             EOT
102              
103 2         31569 Log::Any::Adapter->set('Log::Log4perl');
104             }
105              
106             sub new {
107 4     4 1 4944 my ( $class, $arg ) = @_;
108              
109 4         32 my $self = $class->SUPER::new($arg);
110 4         12166 $self->{config} = Smartcat::App::Config->load;
111 4         12 $self->{rundata} = {};
112              
113 4         17 return $self;
114             }
115              
116             sub project_api {
117 4     4 0 724 my $self = shift @_;
118              
119             $self->{project_api} =
120             Smartcat::App::ProjectApi->new( $self->{api}, $self->{rundata} )
121 4 100       54 unless defined $self->{project_api};
122 4         25 return $self->{project_api};
123             }
124              
125             sub document_api {
126 2     2 0 6 my $self = shift @_;
127              
128             $self->{document_api} =
129             Smartcat::App::DocumentApi->new( $self->{api}, $self->{rundata} )
130 2 100       23 unless defined $self->{document_api};
131 2         14 return $self->{document_api};
132             }
133              
134             sub document_export_api {
135 2     2 0 4 my $self = shift @_;
136              
137             $self->{document_export_api} =
138             Smartcat::App::DocumentExportApi->new( $self->{api}, $self->{rundata} )
139 2 100       15 unless defined $self->{document_export_api};
140 2         14 return $self->{document_export_api};
141             }
142              
143             1;
144             __END__
145              
146             =encoding utf-8
147              
148             =head1 NAME
149              
150             Smartcat::App - Smartcat cli application
151              
152             =head1 SYNOPSIS
153              
154             use Smartcat::App;
155              
156             =head1 DESCRIPTION
157              
158             Smartcat::App is a simple application which use Smartcat Integration API to sync local translation files with Smartcat project content.
159              
160             =head1 AUTHOR
161              
162             Taras Semenenko E<lt>taras.semenenko@gmail.comE<gt>
163              
164             =head1 LICENSE
165              
166             This library is free software; you can redistribute it and/or modify
167             it under the same terms as Perl itself.
168              
169             =head1 SEE ALSO
170              
171             Smartcat::Client
172              
173             =cut