-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cpan ratings type / indexer / endpoint
- Loading branch information
Showing
5 changed files
with
123 additions
and
62 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters