File Coverage

blib/lib/CPAN/Recent/Uploads/Retriever.pm
Criterion Covered Total %
statement 40 40 100.0
branch 4 8 50.0
condition 4 11 36.3
subroutine 10 10 100.0
pod 1 1 100.0
total 59 70 84.2


line stmt bran cond sub pod time code
1             package CPAN::Recent::Uploads::Retriever;
2             $CPAN::Recent::Uploads::Retriever::VERSION = '0.12';
3             #ABSTRACT: Retrieves recentfiles from a CPAN mirror
4              
5 1     1   8 use strict;
  1         5  
  1         33  
6 1     1   6 use warnings;
  1         3  
  1         26  
7 1     1   6 use Carp;
  1         4  
  1         56  
8 1     1   582 use URI;
  1         10504  
  1         138  
9 1     1   1062 use LWP::UserAgent;
  1         48856  
  1         51  
10 1     1   14 use File::Spec::Unix;
  1         3  
  1         408  
11              
12             my @times = qw(1h 6h 1d 1W 1M 1Q 1Y);
13              
14             sub retrieve {
15 1     1 1 4 my $class = shift;
16 1         10 my %opts = @_;
17 1         31 $opts{lc $_} = delete $opts{$_} for keys %opts;
18 1         7 my $self = bless \%opts, $class;
19 1   50     10 $self->{uri} = URI->new( $self->{mirror} || 'http://www.cpan.org/' );
20             croak "Unknown scheme\n"
21             unless $self->{uri} and $self->{uri}->scheme and
22 1 50 33     148 $self->{uri}->scheme =~ /^(http|ftp)$/i;
      33        
23             $self->{time} = '6h'
24             unless $self->{time}
25 1 50 33     266 and grep { $_ eq $self->{time} } @times;
  7         29  
26 1         14 $self->{uri}->path( File::Spec::Unix->catfile( $self->{uri}->path, 'authors', 'RECENT-' . $self->{time} . '.yaml' ) );
27 1         129 return $self->_fetch();
28             }
29              
30             sub _fetch {
31 1     1   4 my $self = shift;
32 1 50   1   13 open my $fooh, '>', \$self->{foo} or die "$!\n";
  1         3  
  1         11  
  1         65  
33 1         1207 my $ua = LWP::UserAgent->new();
34 1     2   3774 my $resp = $ua->get( $self->{uri}->as_string, ':content_cb' => sub { my $data = shift; print {$fooh} $data; } );
  2         119649  
  2         6  
  2         23  
35 1         508 close $fooh;
36 1 50       6 return $self->{foo} if $resp->is_success;
37             }
38              
39             q[Woof];
40              
41             __END__