Fetching index works, need to make it handle the other index data

This commit is contained in:
ryan 2017-08-12 13:44:23 -07:00
parent 56ae09f2ad
commit 2a4c64d096
2 changed files with 88 additions and 0 deletions

76
lib/Cpan.pm Normal file
View file

@ -0,0 +1,76 @@
package Cpan;
use Net::Async::HTTP;
use Future;
use v5.20;
use strict;
use warnings;
use Data::Dumper;
my $http = Net::Async::HTTP->new();
my $loop = IO::Async::Loop->new();
$loop->add($http);
{
my $httpfuture;
my $parsefuture;
my $index;
my $rawindex;
# shortcut wrapper
sub f {$loop->new_future(@_)}
# I want this fully private to these subs.
my $fetch_index = sub {
return $httpfuture if $httpfuture;
my $url = "http://www.cpan.org/modules/02packages.details.txt";
$httpfuture = $http->GET($url);
$httpfuture->on_done(sub {
my $resp = shift;
die "Failed to fetch cpan index" if ($resp->code ne 200);
$rawindex = $resp->decoded_content();
})->on_fail(sub {die "Failed to fetch cpan index"});
return $httpfuture;
};
sub raw_index {
return f()->done($rawindex) if ($rawindex);
my $f = $fetch_index->();
my $nf = f();
$f->on_done(sub {$nf->done($rawindex)});
return $nf;
}
sub index {
return f()->done($index) if ($index);
my $f = raw_index();
$index = {};
my $nf = f();
$f->on_done(sub {
my $rawindex = shift();
$rawindex =~ s/\A.*?\r?\n\r?\n//s; # cut off the header
for my $line (split /\r?\n/, $rawindex) {
if ($line =~ /^(?<module>\S+)\s+(?<version>\S+)\s+(?<dist>\S+)\s*$/) {
$index->{$+{module}} = {version => $+{version}, dist => $+{dist}};
} else {
warn "Got invalid line, $line";
}
};
$nf->done($index);
});
return $nf
}
}
1;

12
test_http.pl Normal file
View file

@ -0,0 +1,12 @@
#!/usr/bin/perl
use strict;
use warnings;
use lib './lib';
use Cpan;
use IO::Async::Loop;
use Data::Dumper;
my $loop = IO::Async::Loop->new();
my $future = Cpan::index();
print Dumper($future->get());