File Coverage

blib/lib/App/Sky/Manager.pm
Criterion Covered Total %
statement 66 69 95.6
branch 14 18 77.7
condition n/a
subroutine 17 17 100.0
pod 2 2 100.0
total 99 106 93.4


line stmt bran cond sub pod time code
1             package App::Sky::Manager;
2             $App::Sky::Manager::VERSION = '0.4.2';
3 1     1   88170 use strict;
  1         13  
  1         32  
4 1     1   6 use warnings;
  1         3  
  1         31  
5              
6              
7 1     1   5 use Carp ();
  1         2  
  1         13  
8              
9 1     1   537 use Moo;
  1         9756  
  1         5  
10 1     1   2043 use MooX 'late';
  1         10329  
  1         7  
11              
12 1     1   151888 use List::Util qw(first);
  1         3  
  1         115  
13              
14 1     1   645 use URI;
  1         4734  
  1         38  
15 1     1   7 use File::Basename qw(basename);
  1         3  
  1         88  
16              
17 1     1   476 use App::Sky::Module;
  1         3  
  1         44  
18              
19             # For defined-or - "//".
20 1     1   24 use 5.010;
  1         3  
21              
22             has config => ( isa => 'HashRef', is => 'ro', );
23              
24              
25             sub _calc_site_conf
26             {
27 15     15   28 my ( $self, $args ) = @_;
28              
29 15         32 my $config = $self->config;
30              
31 15         38 return $config->{sites}->{ $config->{default_site} };
32             }
33              
34             sub _calc_sect_name
35             {
36 6     6   12 my ( $self, $args, $sections ) = @_;
37              
38 6         9 my $bn = $args->{basename};
39              
40 6         12 my $sect_name = $args->{section};
41              
42 6 100       17 if ( !defined($sect_name) )
43             {
44 5 100       14 if ( $args->{is_dir} )
45             {
46 2         4 $sect_name = $self->_calc_site_conf($args)->{dirs_section};
47             }
48             else
49             {
50             $sect_name = (
51             first
52             {
53 8     8   20 my $re = $sections->{$_}->{basename_re};
54 8         223 $bn =~ /$re/;
55             }
56 3         24 ( keys(%$sections) )
57             );
58             }
59             }
60              
61 6 50       25 if ( !defined($sect_name) )
62             {
63 0         0 Carp::confess("Unknown section for basename '$bn'");
64             }
65              
66 6 50       17 if ( !exists( $sections->{$sect_name} ) )
67             {
68 0         0 Carp::confess("Section '$sect_name' does not exist.");
69             }
70              
71 6         12 return $sect_name;
72             }
73              
74             sub _calc_target_dir
75             {
76 7     7   15 my ( $self, $args ) = @_;
77              
78 7 100       18 if ( defined( $args->{target_dir} ) )
79             {
80 1         6 return $args->{target_dir};
81             }
82             else
83             {
84 6         19 my $sections = $self->_calc_site_conf($args)->{sections};
85              
86 6         16 my $sect_name = $self->_calc_sect_name( $args, $sections );
87              
88 6         50 return $sections->{$sect_name}->{target_dir};
89             }
90             }
91              
92             sub _perform_upload_generic
93             {
94 7     7   13 my ( $self, $is_dir, $args ) = @_;
95              
96             my $filenames = $args->{filenames}
97 7 50       25 or Carp::confess("Missing argument 'filenames'");
98              
99 7 50       19 if ( @$filenames != 1 )
100             {
101 0         0 Carp::confess("More than one file passed to 'filenames'");
102             }
103              
104 7         18 my $site_conf = $self->_calc_site_conf($args);
105              
106             my $backend = App::Sky::Module->new(
107             {
108             base_upload_cmd => $site_conf->{base_upload_cmd},
109             dest_upload_prefix => $site_conf->{dest_upload_prefix},
110             dest_upload_url_prefix => $site_conf->{dest_upload_url_prefix},
111             }
112 7         180 );
113              
114 7         2879 my $fn = $filenames->[0];
115 7         280 my $bn = basename($fn);
116              
117 7 100       30 my @dir = ( $is_dir ? ( is_dir => 1 ) : () );
118              
119             return $backend->get_upload_results(
120             {
121              
122             filenames => (
123             $is_dir
124 7 100       38 ? [ map { my $s = $_; $s =~ s#/+\z##ms; $s } @$filenames ]
  2         6  
  2         8  
  2         14  
125             : $filenames,
126             ),
127             target_dir => $self->_calc_target_dir(
128             {
129             %$args,
130             basename => $bn,
131             @dir,
132             }
133             ),
134             @dir,
135             }
136             );
137              
138             }
139              
140              
141             sub get_upload_results
142             {
143 5     5 1 26188 my ( $self, $args ) = @_;
144              
145 5         16 return $self->_perform_upload_generic( 0, $args );
146             }
147              
148              
149             sub get_recursive_upload_results
150             {
151 2     2 1 5651 my ( $self, $args ) = @_;
152              
153 2         7 return $self->_perform_upload_generic( 1, $args );
154             }
155              
156             1;
157              
158             __END__
159              
160             =pod
161              
162             =encoding UTF-8
163              
164             =head1 NAME
165              
166             App::Sky::Manager - manager for the configuration.
167              
168             =head1 VERSION
169              
170             version 0.4.2
171              
172             =head1 METHODS
173              
174             =head2 config
175              
176             The configuration of the app as passed through the configuration file.
177              
178             =head2 my $results = $sky->get_upload_results({ filenames => ["Shine4U.webm"], });
179              
180             Gives the recipe to execute for the upload commands.
181              
182             Accepts one argument that is a hash reference with these keys:
183              
184             =over 4
185              
186             =item * 'filenames'
187              
188             An array reference containing strings to upload. Currently only supports
189             one filename.
190              
191             =item * 'section'
192              
193             An optional section that will override the target section. If not specified,
194             the uploader will try to guess based on the file’s basename and the manager
195             configuration.
196              
197             =item * 'target_dir'
198              
199             Overrides the target directory for the upload, to ignore that dictated by
200             the sections. Should point to a string.
201              
202             =back
203              
204             Returns a L<App::Sky::Results> reference containing:
205              
206             =over 4
207              
208             =item * upload_cmd
209              
210             The upload command to execute (as an array reference of strings).
211              
212             =back
213              
214             =head2 my $results = $sky->get_recursive_upload_results({ filenames => ['/home/music/Music/mp3s/Basic Desire/'], });
215              
216             Gives the recipe to execute for the recursive upload commands.
217              
218             Accepts one argument that is a hash reference with these keys:
219              
220             =over 4
221              
222             =item * 'filenames'
223              
224             An array reference containing paths to directories. Currently only supports
225             one filename.
226              
227             =item * 'section'
228              
229             An optional section that will override the target section. If not specified,
230             the uploader will try to use the 'dirs_section' section.
231              
232             =item * 'target_dir'
233              
234             Overrides the target directory for the upload, to ignore that dictated by
235             the sections. Should point to a string.
236              
237             =back
238              
239             Returns a L<App::Sky::Results> reference containing:
240              
241             =over 4
242              
243             =item * upload_cmd
244              
245             The upload command to execute (as an array reference of strings).
246              
247             =back
248              
249             =for :stopwords cpan testmatrix url bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan
250              
251             =head1 SUPPORT
252              
253             =head2 Websites
254              
255             The following websites have more information about this module, and may be of help to you. As always,
256             in addition to those websites please use your favorite search engine to discover more resources.
257              
258             =over 4
259              
260             =item *
261              
262             MetaCPAN
263              
264             A modern, open-source CPAN search engine, useful to view POD in HTML format.
265              
266             L<https://metacpan.org/release/App-Sky>
267              
268             =item *
269              
270             RT: CPAN's Bug Tracker
271              
272             The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
273              
274             L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-Sky>
275              
276             =item *
277              
278             CPANTS
279              
280             The CPANTS is a website that analyzes the Kwalitee ( code metrics ) of a distribution.
281              
282             L<http://cpants.cpanauthors.org/dist/App-Sky>
283              
284             =item *
285              
286             CPAN Testers
287              
288             The CPAN Testers is a network of smoke testers who run automated tests on uploaded CPAN distributions.
289              
290             L<http://www.cpantesters.org/distro/A/App-Sky>
291              
292             =item *
293              
294             CPAN Testers Matrix
295              
296             The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
297              
298             L<http://matrix.cpantesters.org/?dist=App-Sky>
299              
300             =item *
301              
302             CPAN Testers Dependencies
303              
304             The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
305              
306             L<http://deps.cpantesters.org/?module=App::Sky>
307              
308             =back
309              
310             =head2 Bugs / Feature Requests
311              
312             Please report any bugs or feature requests by email to C<bug-app-sky at rt.cpan.org>, or through
313             the web interface at L<https://rt.cpan.org/Public/Bug/Report.html?Queue=App-Sky>. You will be automatically notified of any
314             progress on the request by the system.
315              
316             =head2 Source Code
317              
318             The code is open to the world, and available for you to hack on. Please feel free to browse it and play
319             with it, or whatever. If you want to contribute patches, please send me a diff or prod me to pull
320             from your repository :)
321              
322             L<https://github.com/shlomif/Sky-uploader>
323              
324             git clone git://github.com/shlomif/Sky-uploader.git
325              
326             =head1 AUTHOR
327              
328             Shlomi Fish <shlomif@cpan.org>
329              
330             =head1 BUGS
331              
332             Please report any bugs or feature requests on the bugtracker website
333             L<https://github.com/shlomif/Sky-uploader/issues>
334              
335             When submitting a bug or request, please include a test-file or a
336             patch to an existing test-file that illustrates the bug or desired
337             feature.
338              
339             =head1 COPYRIGHT AND LICENSE
340              
341             This software is Copyright (c) 2013 by Shlomi Fish.
342              
343             This is free software, licensed under:
344              
345             The MIT (X11) License
346              
347             =cut