OneSky.pm

filter: Statementstmt 97.9% 95 / 97 Branchbran 75.0% 6 / 8 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 22 / 22 Podpod 66.6% 4 / 6 totaltotal 87.1% 136 / 156 SCAR 23.1
CC
10
Cov
91%
_untaint_cwd
5.2
version_string
4.1
user_agent
2.1
CRAP 10.1
« OneSky/Project.pm ↑ Summary
1 package Net::OneSky;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
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 LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use HTTP::Request::Common qw(GET POST);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Net::OneSky::Project;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Time::Local;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 8 ▶use Cwd qw(getcwd abs_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶use IPC::Open3;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # Do not specify version in the base URL, to allow clients to specify as needed
23 8 ▶use constant BASE_URL => URI->new('https://platform.api.onesky.io/');
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 8 has 'api_key' => (
26 is => 'ro',
27 isa => 'Str',
28 required => 1
29 );
30  
31 8 has 'api_secret' => (
32 is => 'ro',
33 isa => 'Str',
34 required => 1
35 );
36  
37 has 'base_url' => (
38 is => 'ro',
39 isa => 'URI',
40 required => 1,
41 5 ▶ default => sub { BASE_URL } # must be a sub even though it is a constant
Subroutine __ANON__: called CC=1 SCAR=0.0
42 8 );
43  
44  
45 sub project {
46 3 ▶ my $self = shift;
Subroutine project: called CC=1 SCAR=0.0
Pod documented
47 3 my $id = shift;
48  
49 3 return new Net::OneSky::Project(id => $id, client => $self);
50 }
51  
52  
53 sub get {
54 2 ▶ my $self = shift;
Subroutine get: called CC=2 SCAR=7.0
Pod documented
55 2 my $uri = shift;
56 2 ▶ my $data = shift || [];
Condition50%1/2
shift() || []
l!l
02
MC/DC0%0/1
shift() || []
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
57  
58 2 $data = $self->authenticate($data);
59  
60 2 $uri = URI->new_abs($uri, $self->{base_url});
61 2 $uri->query_form(@$data);
62 2 my $req = GET($uri);
63  
64 2 return $self->user_agent->request($req);
65 }
66  
67  
68 sub file_upload {
69 1 ▶ my $self = shift;
Subroutine file_upload: called CC=1 SCAR=0.0
Pod documented
70 1 my $uri = shift;
71 1 my $data = shift;
72  
73 1 $data = $self->authenticate($data);
74  
75 1 my $req = POST(URI->new_abs($uri, $self->{base_url}),
76 Content_Type => 'form-data',
77 Content => $data);
78  
79 1 return $self->user_agent->request($req);
80 }
81  
82  
83 sub user_agent {
84 3 ▶ my $self = shift;
Subroutine user_agent: called CC=2 SCAR=7.2
Pod undocumented
85  
86 3 ▶ $self->{_user_agent} ||= LWP::UserAgent->new;
Condition33%1/3
$self->{"_user_agent"} ||= "LWP::UserAgent"->new
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->{"_user_agent"} ||= "LWP::UserAgent"->new
$self->{"_user_agent"} "LWP::UserAgent"->new
Truth table33%1/3
A ||= B
A= $self->{"_user_agent"}B= "LWP::UserAgent"->new
ABresult
1X1
011
000
87 3 my $v = $self->version_string;
88 3 $self->{_user_agent}->agent("Net::OneSky [$v] ");
89 3 return $self->{_user_agent};
90 }
91  
92 sub version_string {
93 3 ▶ return $Net::OneSky::VERSION if (defined($Net::OneSky::VERSION) && $Net::OneSky::VERSION);
Subroutine version_string: called CC=4 SCAR=14.2
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if defined $Net::OneSky::VERSION and $Net::OneSky::VERSION 1 2 3
Condition66%2/3
defined $Net::OneSky::VERSION and $Net::OneSky::VERSION
!ll&&!rl&&r
201
MC/DC50%1/2
defined $Net::OneSky::VERSION and $Net::OneSky::VERSION
defined $Net::OneSky::VERSION $Net::OneSky::VERSION
Truth table66%2/3
A and B
A= defined $Net::OneSky::VERSIONB= $Net::OneSky::VERSION
ABresult
0X0
100
111
94  
95 2 my $prev_dir = _untaint_cwd();
96 2 my $dir = (fileparse(__FILE__))[1];
97  
98 2 eval {
99 2 my $out;
100 2 chdir($dir);
101 2 open3('</dev/null', $out, '>/dev/null', qw(git rev-parse --short HEAD));
102 0 $Net::OneSky::VERSION = <$out>;
103 0 chomp($Net::OneSky::VERSION);
104 };
105  
106 2 chdir($prev_dir);
107  
108 2 ▶ return $Net::OneSky::VERSION if $Net::OneSky::VERSION;
Branch: 1 branch
BranchTrueFalseTotal
if $Net::OneSky::VERSION 0 2 2
109  
110 2 $Net::OneSky::VERSION = 'version unknown';
111 }
112  
113  
114 sub authenticate {
115 3 ▶ my $self = shift;
Subroutine authenticate: called CC=1 SCAR=0.0
Pod documented
116 3 my $data = shift;
117  
118 # copy it
119 3 $data = [@$data];
120  
121 3 my $timestamp = timegm(gmtime(time));
122  
123 3 push(@$data,
124 api_key => $self->api_key,
125 timestamp => $timestamp,
126 dev_hash => md5_hex($timestamp . $self->api_secret)
127 );
128  
129 3 return $data;
130 }
131  
132 sub _untaint_cwd {
133 2 ▶ my $dir = getcwd;
Subroutine _untaint_cwd: called CC=5 SCAR=16.6
134 2 my $orig = $dir;
135  
136 2 ▶ my $valid = (-R $dir && -X $dir);
Condition33%1/3
-R $dir && -X $dir
!ll&&!rl&&r
002
MC/DC0%0/2
-R $dir && -X $dir
-R $dir -X $dir
Truth table33%1/3
A && B
A= -R $dirB= -X $dir
ABresult
0X0
100
111
137  
138 2 ▶ while ($valid && $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid and $dir) 10 2 12
Condition66%2/3
$valid and $dir
!ll&&!rl&&r
0210
MC/DC50%1/2
$valid and $dir
$valid $dir
Truth table66%2/3
A and B
A= $validB= $dir
ABresult
0X0
100
111
139 10 $valid = -X $dir;
140 10 ($dir = (fileparse($dir))[1]) =~ s{/$}{}; # skips /, but that should be fine.
141 }
142  
143 2 ▶ die 'chdir with elevated privileges!' unless $valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $valid 0 2 2
144  
145 2 $orig =~ /^(.*)$/;
146 2 $1;
147 }
148  
149  
150  
151 8 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
152  
153  
154  
155 # ABSTRACT: Simple interface to the OneSky API: http://developer.oneskyapp.com/
156 8 1;
157  
158 __END__
159  
160 =pod
161  
162 =encoding UTF-8
163  
164 =head1 NAME
165  
166 Net::OneSky - Simple interface to the OneSky API: http://developer.oneskyapp.com/
167  
168 =head1 VERSION
169  
170 version 0.0.2
171  
172 =head1 SYNOPSIS
173  
174 use Net::OneSky;
175  
176 my $client = Net::OneSky->new(api_key => $key, api_secret => $secret);
177  
178 my $response = $client->get('/1/locales');
179  
180 my $project = $client->project($project_id);
181  
182 my @languages = $project->locales
183 my @files = $project->list_files
184  
185 $project->upload_file($filename, $file_format, $locale);
186 my $file = $project->export_file($locale, $remote_file, $local_file_name, $block_until_finished)
187  
188 =head1 METHODS
189  
190 =head2 project($project_id)
191  
192 Returns a Net::OneSky::Project object for the given $project_id
193  
194 =head2 get($uri, $query_data)
195  
196 GET an authenticated API request.
197  
198 =head2 file_upload($uri, $post_data)
199  
200 POST an authenticated File upload request. $post_data should be a standard
201 format for a file-upload. More information
202 L<in the LWP Cookbook|http://search.cpan.org/~ether/HTTP-Message-6.11/lib/HTTP/Request/Common.pm#POST>
203  
204 =head2 authenticate($data)
205  
206 Returns a new data object with authentication params added.
207  
208 =head1 AUTHOR
209  
210 Erik Ogan <erik@change.org>
211  
212 =head1 COPYRIGHT AND LICENSE
213  
214 This software is copyright (c) 2016-2018 by Change.org.
215  
216 This is free software; you can redistribute it and/or modify it under
217 the same terms as the Perl 5 programming language system itself.
218  
219 =cut
« OneSky/Project.pm ↑ Summary