File Coverage

blib/lib/CPAN/Testers/API/Controller/Upload.pm
Criterion Covered Total %
statement 58 58 100.0
branch 19 20 95.0
condition n/a
subroutine 7 7 100.0
pod 2 2 100.0
total 86 87 98.8


line stmt bran cond sub pod time code
1             package CPAN::Testers::API::Controller::Upload;
2             our $VERSION = '0.020';
3             # ABSTRACT: API for uploads to CPAN
4              
5             #pod =head1 DESCRIPTION
6             #pod
7             #pod =head1 SEE ALSO
8             #pod
9             #pod =over
10             #pod
11             #pod =item L<CPAN::Testers::Schema::Result::Upload>
12             #pod
13             #pod =item L<Mojolicious::Controller>
14             #pod
15             #pod =back
16             #pod
17             #pod =cut
18              
19 1     1   6187 use Mojo::Base 'Mojolicious::Controller';
  1         3  
  1         7  
20 1     1   183 use CPAN::Testers::API::Base;
  1         3  
  1         11  
21 1     1   30 use Mojo::UserAgent;
  1         2  
  1         11  
22              
23             #pod =method get
24             #pod
25             #pod ### Requests:
26             #pod GET /v1/upload
27             #pod GET /v1/upload?since=2016-01-01T12:34:00Z
28             #pod GET /v1/upload/dist/My-Dist
29             #pod GET /v1/upload/dist/My-Dist?since=2016-01-01T12:34:00Z
30             #pod GET /v1/upload/author/PREACTION
31             #pod GET /v1/upload/author/PREACTION?since=2016-01-01T12:34:00Z
32             #pod
33             #pod ### Response:
34             #pod 200 OK
35             #pod Content-Type: application/json
36             #pod
37             #pod [
38             #pod {
39             #pod "dist": "My-Dist",
40             #pod "version": "1.000",
41             #pod "author": "PREACTION",
42             #pod "filename": "My-Dist-1.000.tar.gz",
43             #pod "released": "2016-08-12T04:02:34Z",
44             #pod }
45             #pod ]
46             #pod
47             #pod Get CPAN upload data. Results can be limited by distribution (with the
48             #pod C<dist> key in the stash), by author (with the C<author> key in the
49             #pod stash), and by date (with the C<since> query parameter).
50             #pod
51             #pod =cut
52              
53 18     18 1 57973 sub get( $c ) {
  18         70  
  18         63  
54 18 100       152 $c->openapi->valid_input or return;
55              
56 16         21834 my $rs = $c->schema->resultset( 'Upload' );
57 16         7920 $rs = $rs->search(
58             { },
59             {
60             order_by => 'released',
61             columns => [qw( dist version author filename released )],
62             }
63             );
64              
65 16 100       6964 if ( my $since = $c->param( 'since' ) ) {
66 6         863 $rs = $rs->since( $since );
67             }
68              
69 16         7079 my @results;
70 16 100       75 if ( my $dist = $c->validation->param( 'dist' ) ) {
    100          
71 6         187 $rs = $rs->by_dist( $dist );
72 6         1770 @results = $rs->all;
73 6 100       16138 if ( !@results ) {
74 2         29 return $c->render_error( 404, sprintf 'Distribution "%s" not found', $dist );
75             }
76             }
77             elsif ( my $author = $c->validation->param( 'author' ) ) {
78 6         372 @results = $rs->by_author( $author )->all;
79 6 100       22745 if ( !@results ) {
80 2         206 return $c->render_error( 404, sprintf 'Author "%s" not found', $author );
81             }
82             }
83             else {
84 4         262 @results = $rs->all;
85             }
86              
87 12         13597 my @formatted = map { +{
88 22         17904 dist => $_->dist,
89             version => $_->version,
90             author => $_->author,
91             filename => $_->filename,
92             released => $_->released . "",
93             } } @results;
94              
95 12         19985 return $c->render(
96             openapi => \@formatted,
97             );
98             }
99              
100             #pod =method feed
101             #pod
102             #pod Get a feed for uploads to CPAN. This feed returns the same information as
103             #pod the regular API, but as they come in.
104             #pod
105             #pod =cut
106              
107 6     6 1 23402 sub feed( $c ) {
  6         21  
  6         13  
108 6         72 $c->inactivity_timeout( 60000 );
109 6 100       1434 my $path = $c->stash( 'dist' ) ? '/upload/dist/' . $c->stash( 'dist' )
    100          
110             : $c->stash( 'author' ) ? '/upload/author/' . $c->stash( 'author' )
111             : '/upload/dist' # Default to all dists
112             ;
113              
114 6         254 my $ua = Mojo::UserAgent->new( inactivity_timeout => 6000 );
115             $ua->websocket(
116 6         17 $c->app->config->{broker} . '/sub' . $path,
117 6     6   12 sub( $ua, $tx ) {
  6         81276  
  6         16  
118 6         38 $c->stash( tx => $tx );
119 6         14 $tx->on(finish => sub( $tx, $code ) {
120             # Broker closed connection, so close connection with
121             # client, unless that's what we're already doing
122 6 100       32 $c->finish if !$c->stash( 'finished' );
123 6         195 });
124              
125 8         17 $tx->on( message => sub( $tx, $msg ) {
126 8         45 $c->send( $msg );
127 6         75 } );
128             }
129 6         77 );
130              
131 6         8234 $c->stash( ua => $ua );
132 6     6   14 $c->on( finish => sub( $c, $tx ) {
  6         26726  
  6         18  
  6         15  
133             # Client closed connection, so close connection with broker
134 6 50       34 if ( my $tx = $c->stash( 'tx' ) ) {
135 6         187 $c->stash( finished => 1 );
136 6         173 $tx->finish;
137             }
138 6         209 } );
139 6         2079 $c->rendered( 101 );
140             }
141              
142             1;
143              
144             __END__
145              
146             =pod
147              
148             =head1 NAME
149              
150             CPAN::Testers::API::Controller::Upload - API for uploads to CPAN
151              
152             =head1 VERSION
153              
154             version 0.020
155              
156             =head1 DESCRIPTION
157              
158             =head1 METHODS
159              
160             =head2 get
161              
162             ### Requests:
163             GET /v1/upload
164             GET /v1/upload?since=2016-01-01T12:34:00Z
165             GET /v1/upload/dist/My-Dist
166             GET /v1/upload/dist/My-Dist?since=2016-01-01T12:34:00Z
167             GET /v1/upload/author/PREACTION
168             GET /v1/upload/author/PREACTION?since=2016-01-01T12:34:00Z
169              
170             ### Response:
171             200 OK
172             Content-Type: application/json
173              
174             [
175             {
176             "dist": "My-Dist",
177             "version": "1.000",
178             "author": "PREACTION",
179             "filename": "My-Dist-1.000.tar.gz",
180             "released": "2016-08-12T04:02:34Z",
181             }
182             ]
183              
184             Get CPAN upload data. Results can be limited by distribution (with the
185             C<dist> key in the stash), by author (with the C<author> key in the
186             stash), and by date (with the C<since> query parameter).
187              
188             =head2 feed
189              
190             Get a feed for uploads to CPAN. This feed returns the same information as
191             the regular API, but as they come in.
192              
193             =head1 SEE ALSO
194              
195             =over
196              
197             =item L<CPAN::Testers::Schema::Result::Upload>
198              
199             =item L<Mojolicious::Controller>
200              
201             =back
202              
203             =head1 AUTHOR
204              
205             Doug Bell <preaction@cpan.org>
206              
207             =head1 COPYRIGHT AND LICENSE
208              
209             This software is copyright (c) 2016 by Doug Bell.
210              
211             This is free software; you can redistribute it and/or modify it under
212             the same terms as the Perl 5 programming language system itself.
213              
214             =cut