File Coverage

blib/lib/Bintray/API/Version.pm
Criterion Covered Total %
statement 18 54 33.3
branch 0 14 0.0
condition n/a
subroutine 6 17 35.2
pod 0 11 0.0
total 24 96 25.0


line stmt bran cond sub pod time code
1             package Bintray::API::Version;
2              
3             #######################
4             # LOAD CORE MODULES
5             #######################
6 1     1   5 use strict;
  1         2  
  1         36  
7 1     1   5 use warnings FATAL => 'all';
  1         1  
  1         31  
8 1     1   4 use Carp qw(croak carp);
  1         2  
  1         59  
9              
10             #######################
11             # VERSION
12             #######################
13             our $VERSION = '0.02';
14              
15             #######################
16             # LOAD CPAN MODULES
17             #######################
18 1     1   13 use Params::Validate qw(validate_with :types);
  1         3  
  1         177  
19              
20 1         12 use Object::Tiny qw(
21             name
22             session
23             package
24 1     1   6 );
  1         1  
25              
26             #######################
27             # LOAD DIST MODULES
28             #######################
29 1     1   190 use Bintray::API::Session;
  1         1  
  1         6  
30              
31             #######################
32             # PUBLIC METHODS
33             #######################
34              
35             ## Constructor
36             sub new {
37 0     0 0   my ( $class, @args ) = @_;
38              
39 0           my %opts = validate_with(
40             params => [@args],
41             spec => {
42             session => {
43             type => OBJECT,
44             isa => 'Bintray::API::Session',
45             },
46             name => {
47             type => SCALAR,
48             },
49             package => {
50             type => OBJECT,
51             isa => 'Bintray::API::Package',
52             },
53             },
54             );
55              
56 0           return $class->SUPER::new(%opts);
57             } ## end sub new
58              
59             #######################
60             # API METHODS
61             #######################
62              
63             ## Info
64             sub info {
65 0     0 0   my ($self) = @_;
66 0           return $self->session()->talk(
67             path => join( '/',
68             'packages', $self->package->repo->subject->name,
69             $self->package->repo->name, $self->package->name,
70             'versions', $self->name,
71             ),
72             );
73             } ## end sub info
74              
75             ## Update Version
76             sub update {
77 0     0 0   my ( $self, @args ) = @_;
78              
79 0           my %opts = validate_with(
80             params => [@args],
81             spec => {
82             details => {
83             type => HASHREF,
84             },
85             },
86             );
87              
88             # Create JSON
89 0           my $json = $self->session()->json()->encode( $opts{details} );
90              
91             # POST
92 0           return $self->session()->talk(
93             method => 'PATCH',
94             path => join( '/',
95             'packages', $self->package->repo->subject->name,
96             $self->package->repo->name, $self->package->name,
97             'versions', $self->name,
98             ),
99             content => $json,
100             );
101             } ## end sub update
102              
103             ## Get Attributes
104             sub get_attributes {
105 0     0 0   my ( $self, @args ) = @_;
106              
107 0           my %opts = validate_with(
108             params => [@args],
109             spec => {
110             names => {
111             type => ARRAYREF,
112             default => [],
113             },
114             },
115             );
116              
117 0           return $self->session->talk(
118             path => join( '/',
119             'packages',
120             $self->package->repo->subject->name,
121             $self->package->repo->name,
122             $self->package->name, 'versions', $self->name, 'attributes', ), (
123             defined $opts{names}
124             ? (
125             query => [
126             {
127 0 0         names => join( ',', @{ $opts{names} } ),
128             },
129             ],
130             )
131             : (),
132             ),
133             );
134             } ## end sub get_attributes
135              
136             # Set Attributes
137             sub set_attributes {
138 0     0 0   my ( $self, @args ) = @_;
139              
140 0           my %opts = validate_with(
141             params => [@args],
142             spec => {
143             attributes => {
144             type => ARRAYREF,
145             },
146             update => {
147             type => BOOLEAN,
148             default => 0,
149             },
150             },
151             );
152              
153             # Create JSON
154 0           my $json = $self->session()->json()->encode( $opts{attributes} );
155              
156             # POST
157 0 0         return $self->session()->talk(
158             method => $opts{update} ? 'PATCH' : 'POST',
159             path => join( '/',
160             'packages',
161             $self->package->repo->subject->name,
162             $self->package->repo->name,
163             $self->package->name, 'versions', $self->name, 'attributes', ),
164             content => $json,
165             );
166             } ## end sub set_attributes
167              
168             ## Update Attributes
169 0     0 0   sub update_attributes { return shift->set_attributes( @_, update => 1, ); }
170              
171             ## Test WebHook
172             sub test_webhook {
173 0     0 0   my ($self) = @_;
174 0           return $self->session()->talk(
175             method => 'POST',
176             path => join( '/',
177             'webhooks', $self->package->repo->subject->name,
178             $self->package->repo->name, $self->package->name,
179             'versions', $self->name,
180             ),
181             wantheaders => 1,
182             );
183             } ## end sub test_webhook
184              
185             ## Upload
186             sub upload {
187 0     0 0   my ( $self, @args ) = @_;
188              
189 0           my %opts = validate_with(
190             params => [@args],
191             spec => {
192             file => {
193             type => SCALAR,
194             },
195             repo_path => {
196             type => SCALAR,
197             },
198             publish => {
199             type => BOOLEAN,
200             default => 0,
201             },
202             explode => {
203             type => BOOLEAN,
204             default => 0,
205             },
206             },
207             );
208              
209             # Read File contents
210 0           my $file_contents;
211 0 0         open( my $fh, '<:raw', $opts{file} )
212             or croak "ERROR: Failed to open $opts{file} to read!";
213 0           my $fs = -s $fh;
214 0           read( $fh, $file_contents, $fs );
215 0           close($fh);
216              
217             # Cleanup Repo Path
218 0           $opts{repo_path} =~ s{^\/}{}xi;
219              
220             # Upload
221 0 0         return $self->session->talk(
    0          
222             method => 'PUT',
223             path => join( '/',
224             'content', $self->package->repo->subject->name,
225             $self->package->repo->name, $self->package->name,
226             $self->name, $opts{repo_path},
227             ),
228             content => $file_contents,
229             params => [
230              
231             # Publish?
232             ( defined $opts{publish} ? { publish => $opts{publish} } : (), ),
233              
234             # Explode?
235             ( defined $opts{explode} ? { explode => $opts{explode} } : (), ),
236             ],
237             );
238             } ## end sub upload
239              
240             ## Publish files
241             sub publish {
242 0     0 0   my ( $self, @args ) = @_;
243              
244 0           my %opts = validate_with(
245             params => [@args],
246             spec => {
247             discard => {
248             type => BOOLEAN,
249             default => 0,
250             },
251             },
252             );
253              
254 0 0         return $self->session->talk(
255             method => 'POST',
256             path => join( '/',
257             'content', $self->package->repo->subject->name,
258             $self->package->repo->name, $self->package->name,
259             $self->name, 'publish',
260             ), (
261             # Check for discard
262             $opts{discard}
263             ? ( content => $self->session->json->encode( { discard => 'true' } ) )
264             : (),
265             ),
266             );
267             } ## end sub publish
268              
269             ## Discard files
270 0     0 0   sub discard { return shift->publish( @_, discard => 1, ); }
271              
272             ## Sign a Version
273             sub sign {
274 0     0 0   my ( $self, @args ) = @_;
275 0           my %opts = validate_with(
276             params => [@_],
277             spec => {
278             passphrase => {
279             type => SCALAR,
280             default => '',
281             },
282             },
283             );
284              
285 0 0         return $self->session()->talk(
286             method => 'POST',
287             path => join( '/',
288             'gpg', $self->package->repo->subject->name,
289             $self->package->repo->name, $self->package->name,
290             'versions', $self->name,
291             ), (
292             $opts{passphrase}
293             ? (
294             query => [
295             {
296             passphrase => $opts{passphrase},
297             },
298             ],
299             )
300             : (),
301             ),
302             );
303             } ## end sub sign
304              
305             #######################
306             1;
307              
308             __END__