Skip to content

Commit

Permalink
cpan ratings type / indexer / endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
monken committed Jun 8, 2011
1 parent f1ea88b commit ff7109e
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 62 deletions.
61 changes: 0 additions & 61 deletions elasticsearch/index_cpanratings.pl

This file was deleted.

32 changes: 32 additions & 0 deletions lib/MetaCPAN/Document/Rating.pm
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,32 @@
package MetaCPAN::Document::Rating;
use Moose;
use ElasticSearchX::Model::Document;
use ElasticSearchX::Model::Document::Types qw(:all);
use MooseX::Types::Structured qw(Dict Tuple Optional);
use MooseX::Types::Moose qw(Int Num Bool Str ArrayRef HashRef Undef);

has user => ( required => 1, is => 'ro', isa => Str );
has details =>
( required => 0, is => 'ro', isa => Dict [ documentation => Str ] );
has rating =>
( required => 1, is => 'ro', isa => Num, builder => '_build_rating' );
has distribution => ( required => 1, is => 'ro', isa => Str );
has release => ( required => 1, is => 'ro', isa => Str );
has author => ( required => 1, is => 'ro', isa => Str );
has date =>
( required => 1, isa => 'DateTime', default => sub { DateTime->now } );
has helpful => (
required => 1,
isa => ArrayRef [ Dict [ user => Str, value => Bool ] ],
default => sub { [] } );

sub _build_rating {
my $self = shift;
die "Provide details to calculate a rating";
my %details = %{ $self->details };
my $rating = 0;
$rating += $_ for ( values %details );
return $rating / scalar keys %details;
}

__PACKAGE__->meta->make_immutable;
29 changes: 29 additions & 0 deletions lib/MetaCPAN/Plack/Rating.pm
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,29 @@
package MetaCPAN::Plack::Rating;
use base 'MetaCPAN::Plack::Base';
use strict;
use warnings;

sub type { 'rating' }

sub handle {
my ( $self, $req ) = @_;
$self->get_source($req);
}

1;

__END__
=head1 METHODS
=head2 type
Returns C<rating>.
=head2 handle
Calls L<MetaCPAN::Plack::Base/get_source>.
=head1 SEE ALSO
L<MetaCPAN::Plack::Base>
61 changes: 61 additions & 0 deletions lib/MetaCPAN/Script/Ratings.pm
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,61 @@
package MetaCPAN::Script::Ratings;

use Moose;
with 'MooseX::Getopt';
use Log::Contextual qw( :log :dlog );
with 'MetaCPAN::Role::Common';
use File::Spec::Functions qw(catfile);
use File::Temp qw(tempdir);
use JSON ();
use Parse::CSV ();
use LWP::UserAgent ();

has ratings =>
( is => 'ro', default => 'http://cpanratings.perl.org/csv/all_ratings.csv' );

sub run {
my $self = shift;
$self->index_ratings;
$self->index->refresh;
}

sub index_ratings {
my $self = shift;
my $ua = LWP::UserAgent->new;
log_info { "Downloading " . $self->ratings };
my $target = catfile( tempdir( CLEANUP => 1 ), 'ratings.csv' );
$ua->mirror( $self->ratings, $target );

my $parser = Parse::CSV->new(
file => $target,
fields => 'auto', );

my $type = $self->index->type('rating');
while ( my $rating = $parser->fetch ) {
next unless ( $rating->{review_count} );
my $data = {
distribution => $rating->{distribution},
release => 'PLACEHOLDER',
author => 'PLACEHOLDER',
rating => $rating->{rating},
user => 'CPANRatings' };
for ( my $i = 0 ; $i < $rating->{review_count} ; $i++ ) {
$type->put( Dlog_trace { $_ } $data );
}
}
log_info { "done" };
}

1;

=pod
=head1 SYNOPSIS
$ bin/metacpan mirrors
=head1 SOURCE
L<http://www.cpan.org/indices/mirrors.json>
=cut
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Script/Server.pm
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sub build_app {
cpan => $self->cpan, cpan => $self->cpan,
remote => $self->remote, remote => $self->remote,
index => $index ); index => $index );
for ( qw(Author File Mirror Module for ( qw(Author File Mirror Module Rating
Pod Release Source Login User) ) Pod Release Source Login User) )
{ {
my $class = "MetaCPAN::Plack::" . $_; my $class = "MetaCPAN::Plack::" . $_;
Expand Down

0 comments on commit ff7109e

Please sign in to comment.