From 2a4c64d096a8bba3c89d5aba24eddbcd7afed5f3 Mon Sep 17 00:00:00 2001 From: ryan Date: Sat, 12 Aug 2017 13:44:23 -0700 Subject: [PATCH] Fetching index works, need to make it handle the other index data --- lib/Cpan.pm | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++ test_http.pl | 12 +++++++++ 2 files changed, 88 insertions(+) create mode 100644 lib/Cpan.pm create mode 100644 test_http.pl diff --git a/lib/Cpan.pm b/lib/Cpan.pm new file mode 100644 index 0000000..6b5cf70 --- /dev/null +++ b/lib/Cpan.pm @@ -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 =~ /^(?\S+)\s+(?\S+)\s+(?\S+)\s*$/) { + $index->{$+{module}} = {version => $+{version}, dist => $+{dist}}; + } else { + warn "Got invalid line, $line"; + } + }; + + $nf->done($index); + }); + + return $nf + } +} + +1; diff --git a/test_http.pl b/test_http.pl new file mode 100644 index 0000000..e3089eb --- /dev/null +++ b/test_http.pl @@ -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());