OneSky/Project.pm

filter: Statementstmt 100% 75 / 75 Branchbran 90.9% 20 / 22 Conditioncond 94.4% 17 / 18 MC/DCmcdc 90.0% 9 / 10 Subroutinesub 100% 11 / 11 Podpod 80.0% 4 / 5 totaltotal 96.4% 136 / 141 SCAR 30.0
CC
20
Cov
97%
export_file
8.0
list_files
6.0
upload_file
4.0
CRAP 20.0
↑ Summary OneSky.pm »
1 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Net::OneSky::Project;
5 8 $Net::OneSky::Project::VERSION = '0.0.2';
6 8 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 8 ▶use JSON qw( decode_json );
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 8 has 'client' => (
14 is => 'ro',
15 isa => 'Net::OneSky',
16 required => 1
17 );
18  
19 8 has 'id' => (
20 is => 'ro',
21 isa => 'Num',
22 required => 1
23 );
24  
25  
26 sub locales {
27 3 ▶ my $self = shift;
Subroutine locales: called CC=4 SCAR=13.9
Pod documented
28 3 my $include_base = shift;
29 3 my $uri = $self->base_uri . '/languages';
30 3 my $resp = $self->client->get($uri);
31 3 ▶ die "ERROR Fetching locales:" unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 1 2 3
32 2 my $data = decode_json($resp->content);
33  
34 6 grep { defined }
35 6 ▶ map { (!$_->{is_base_language} || $include_base) ? $_->{code} : undef}
Branch: 1 branch
BranchTrueFalseTotal
!$_->{'is_base_language'} || $include_base ? : 5 1 6
Condition100%3/3
not $_->{"is_base_language"} or $include_base
l!l&&r!l&&!r
411
MC/DC100%2/2
not $_->{"is_base_language"} or $include_base
not $_->{"is_base_language"} $include_base
Truth table100%3/3
not(A) or B
A= not $_->{"is_base_language"}B= $include_base
ABresult
1X1
011
000
36 2 @{$data->{data}};
37 }
38  
39  
40 sub list_files {
41 4 ▶ my $self = shift;
Subroutine list_files: called CC=6 SCAR=17.9
Pod documented
42  
43 4 my $uri = $self->base_uri . '/files';
44 4 my @files;
45 4 my $page_count = 0;
46 4 my $page = 0;
47 4 my $per_page = 100;
48 4 my $form_data = [
49 page => -1,
50 per_page => $per_page
51 ];
52  
53 # The first condition makes sure the loop executes at least once, in which
54 # $page_count is initialized to be used in the second condition in
55 # subsequent iterations.
56 4 ▶ until ($page_count > 0 && $page >= $page_count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page_count > 0 and $page >= $page_count) 5 2 7
Condition100%3/3
$page_count > 0 and $page >= $page_count
!ll&&!rl&&r
412
MC/DC100%2/2
$page_count > 0 and $page >= $page_count
$page_count > 0 $page >= $page_count
Truth table100%3/3
A and B
A= $page_count > 0B= $page >= $page_count
ABresult
0X0
100
111
57 5 $form_data->[1] = ++$page;
58 5 my $resp = $self->client->get($uri, $form_data);
59 5 ▶ die "ERROR fetching file list:" unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 1 4 5
60 4 my $data = decode_json($resp->content);
61  
62 4 ▶ $page_count = $data->{meta}->{page_count} unless $page_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $page_count 3 1 4
63 4 ▶ die "NO files fetching list" unless $page_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $page_count 1 3 4
64  
65 3 push @files, map { $_->{file_name} } @{$data->{data}};
66 }
67  
68 2 return @files;
69 }
70  
71  
72 sub upload_file {
73 3 ▶ my $self = shift;
Subroutine upload_file: called CC=4 SCAR=13.9
Pod documented
74 3 ▶ my $file = shift || die 'Missing file';
Condition100%2/2
shift() || die('Missing file')
l!l
21
MC/DC100%1/1
shift() || die('Missing file')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
75 2 ▶ my $format = shift || die 'Missing format';
Condition100%2/2
shift() || die('Missing format')
l!l
11
MC/DC100%1/1
shift() || die('Missing format')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
76 1 my $locale = shift;
77  
78 1 my $uri = $self->base_uri . '/files';
79  
80 1 my $form_data = [
81 file => [$file, basename($file)],
82 file_format => $format,
83 ];
84  
85 1 ▶ push(@$form_data, locale => $locale) if $locale;
Branch: 1 branch
BranchTrueFalseTotal
if $locale 1 0 1
86  
87 1 $self->client->file_upload($uri, $form_data)
88 }
89  
90  
91 sub export_file {
92 5 ▶ my $self = shift;
Subroutine export_file: called CC=8 SCAR=20.8
Pod documented
93 5 ▶ my $locale = shift || die 'missing locale';
Condition100%2/2
shift() || die('missing locale')
l!l
41
MC/DC100%1/1
shift() || die('missing locale')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
94 4 ▶ my $remote_file = shift || die 'missing remote_file';
Condition100%2/2
shift() || die('missing remote_file')
l!l
31
MC/DC100%1/1
shift() || die('missing remote_file')
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
95 3 my $local_file = shift;
96 3 ▶ my $block_until_finished = shift || 0;
Condition100%2/2
shift() || 0
l!l
12
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
97  
98 3 ▶ die 'block_until_finished is not (yet) implemented!' if $block_until_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $block_until_finished 1 2 3
99  
100 2 my $uri = $self->base_uri . '/translations';
101  
102 2 my $form_data = [
103 locale => $locale,
104 source_file_name => $remote_file
105 ];
106  
107 2 ▶ push(@$form_data, export_file_name => $local_file) if $local_file;
Branch: 1 branch
BranchTrueFalseTotal
if $local_file 1 1 2
108  
109 2 my $resp = $self->client->get($uri, $form_data);
110  
111 2 ▶ die $resp->content unless $resp->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $resp->is_success 0 2 2
112  
113 2 ▶ die 'OneSky returned incomplete response (expecting you to retry later) and block_until_finished is not implemented.'
Branch: 1 branch
BranchTrueFalseTotal
if $resp->code == 202 1 1 2
114 if($resp->code == 202);
115  
116 1 $resp->content;
117 }
118  
119 sub base_uri {
120 10 ▶ my $self = shift;
Subroutine base_uri: called CC=2 SCAR=7.1
Pod undocumented
121 10 ▶ my $version = shift || 1;
Condition50%1/2
shift() || 1
l!l
010
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
122  
123 10 "/$version/projects/" . $self->id;
124 }
125  
126 8 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
127  
128 # ABSTRACT: OneSky Project interface https://github.com/onesky/api-documentation-platform/blob/master/resources/project.md
129 8 1;
130  
131 __END__
132  
133 =pod
134  
135 =encoding UTF-8
136  
137 =head1 NAME
138  
139 Net::OneSky::Project - OneSky Project interface https://github.com/onesky/api-documentation-platform/blob/master/resources/project.md
140  
141 =head1 VERSION
142  
143 version 0.0.2
144  
145 =head1 SYNOPSIS
146  
147 use Net::OneSky;
148  
149 my $project = $one_sky_client->project(42);
150  
151 my @languages = $project->locales
152  
153 my @files = $project->list_files
154  
155 $project->upload_file($filename, $file_format, $locale);
156  
157 my $file = $project->export_file($locale, $remote_file, $local_file_name, $block_until_finished)
158  
159 =head1 METHODS
160  
161 =head2 locales([$include_base_language])
162  
163 Returns a list of locales in a project. By default this call skips the base
164 language, but passing a true value as the first argument will cause it to be
165 included.
166  
167 =head2 list_files()
168  
169 Returns a list of file names in a project
170  
171 =head2 upload_file($filename, $file_format[, $locale])
172  
173 Uploads a file in the given format. $locale is optional. If undefined, the
174 file is assumed to be the base locale.
175  
176 =head2 export_file($locale, $remote_file [, $local_file, $block_until_finished])
177  
178 Downloads a file for the given locale.
179  
180 =head1 AUTHOR
181  
182 Erik Ogan <erik@change.org>
183  
184 =head1 COPYRIGHT AND LICENSE
185  
186 This software is copyright (c) 2016-2018 by Change.org.
187  
188 This is free software; you can redistribute it and/or modify it under
189 the same terms as the Perl 5 programming language system itself.
190  
191 =cut
↑ Summary OneSky.pm »