Skip to content

Commit

Permalink
list authors where the PAUSEID starts with a given letter
Browse files Browse the repository at this point in the history
  • Loading branch information
szabgab committed Oct 21, 2014
1 parent 9852bd4 commit d7c0500
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Makefile.PL
Expand Up @@ -8,7 +8,9 @@ my %conf = (
VERSION_FROM => 'lib/MetaCPAN/SCO.pm',
PREREQ_PM => {
'Cwd' => '0',
'Data::Dumper' => '0',
'File::Basename' => '0',
'List::MoreUtils' => '0',
'Plack::Request' => '0',
'Template' => '0',
'LWP::Simple' => '0',
Expand Down
43 changes: 42 additions & 1 deletion lib/MetaCPAN/SCO.pm
Expand Up @@ -4,8 +4,11 @@ use warnings;

use Carp ();
use Cwd qw(abs_path);
use Data::Dumper qw(Dumper);
use File::Basename qw(dirname);
use JSON qw(from_json);
use List::MoreUtils qw(natatime);
use LWP::Simple qw(get);
use Path::Tiny qw(path);
use Plack::Builder;
use Plack::Request;
Expand Down Expand Up @@ -33,7 +36,18 @@ sub run {
return template('feedback');
}
if ($request->path_info =~ m{^/author/?$}) {
return template('author');
my $query_string = $request->query_string;
return template('author') if not $query_string;
my $lead = substr $query_string, 0, 1;
my $authors = authors_starting_by(uc $lead);
if (@$authors) {
my $it = natatime 4, @$authors;
my @table;
while (my @vals = $it->()) {
push @table, \@vals;
}
return template('author', {authors => \@table});
}
}

my $reply = template('404');
Expand All @@ -48,6 +62,33 @@ sub run {
};
}

sub authors_starting_by {
my ($char) = @_;
# curl http://api.metacpan.org/v0/author/_search?size=10
# curl 'http://api.metacpan.org/v0/author/_search?q=author._id:S*&size=10'
# curl 'http://api.metacpan.org/v0/author/_search?size=10&fields=name'
# curl 'http://api.metacpan.org/v0/author/_search?q=author._id:S*&size=10&fields=name'
# or maybe use fetch to download and keep the full list locally:
# http://api.metacpan.org/v0/author/_search?pretty=true&q=*&size=100000 (from https://github.com/CPAN-API/cpan-api/wiki/API-docs )

my @authors = [];
if ($char =~ /[A-Z]/) {
eval {
my $json = get "http://api.metacpan.org/v0/author/_search?q=author._id:$char*&size=5000&fields=name";
my $data = from_json $json;
@authors =
sort { $a->{id} cmp $b->{id} }
map { { id => $_->{_id}, name => $_->{fields}{name} } } @{ $data->{hits}{hits} };
1;
} or do {
my $err = $@ // 'Unknown error';
warn $err if $err;
};
}
return \@authors;
}


sub template {
my ( $file, $vars ) = @_;
$vars //= {};
Expand Down
11 changes: 11 additions & 0 deletions tt/author.tt
Expand Up @@ -30,3 +30,14 @@
</tr></table>
</center><br />

<table width="100%">
<% FOR row IN authors %>
<tr class="<% IF loop.index % 2 %>s<% ELSE %>r<% END %>">
<% FOR a IN row %>
<td>
<a href="/~<% a.id %>/"><b><% a.id %></b></a><br/><small><% a.name %></small>
</td>
<% END %>
</tr>
<% END %>
</table>

0 comments on commit d7c0500

Please sign in to comment.