Skip to content

Commit

Permalink
Merge leejo/ratings_exports_additions
Browse files Browse the repository at this point in the history
  • Loading branch information
abh committed Mar 11, 2016
2 parents 8ead30a + d5f6e61 commit bda3872
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
68 changes: 68 additions & 0 deletions lib/CPANRatings/Control.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ use Moose;
extends qw(Combust::Control Combust::Control::Bitcard::DBIC Combust::Control::StaticFiles);
use LWP::Simple qw(get);
use CPANRatings::Schema;
use CPANRatings::Model::SearchCPAN;
use Digest::SHA qw(sha1_hex);
use Encode qw();
use Combust::Constant qw(OK);
use PerlOrg::Template::Filters;
use XML::RSS;
use JSON;
use DateTime::Format::ISO8601;
use DateTime::Format::W3CDTF;
use DateTime;

has schema => (
isa => 'CPANRatings::Schema',
Expand Down Expand Up @@ -63,6 +67,70 @@ sub _calc_auth_token {
return '1-' . sha1_hex('8wae4ko - this is very secret!' . $cookie);
}

sub as_json {
my ($self, $reviews, $mode, $id) = @_;

my $data = {
$mode => $id,
reviews => [],
};

# default to one year being outdated...
my $outdated = DateTime->now->subtract( years => 1 );

if ( $mode eq 'distribution' ) {
# go by date of latest version
my $search = CPANRatings::Model::SearchCPAN->new;
if ( my $releases = $search->get_releases( $id ) ) {
my $last_release_date = DateTime::Format::ISO8601->parse_datetime(
$releases->[0]->{released}
);

# if last release date was within the last year, then use the last
# release date as the cutoff point
$outdated = $last_release_date
if $last_release_date > $outdated;
}
}

my %sum;

while (my $review = $reviews->next) {

my $is_outdated = ( $review->updated < $outdated ) ? 1 : 0;
my $review_for_data = {
review => $review->review,
version => $review->version_reviewed,
status => $review->status,
rating => int( $review->rating_overall ),
user => $review->user_name,
date => $review->updated->iso8601,
outdated => $is_outdated ? JSON::true : JSON::false,
helpful => $review->helpful_score > 0 ? JSON::true : JSON::false,
};

$sum{all} += $review->helpful_score > 0 ? int( $review->rating_overall ) : 0;
$sum{recent} += ! $is_outdated && $review->helpful_score > 0
? int( $review->rating_overall ) : 0;

push( @{ $data->{reviews} },$review_for_data );
}

if ( my @reviews = @{ $data->{reviews} } ) {

my @all = grep { $_->{helpful} } @reviews;
my @recent = grep { $_->{helpful} && !$_->{outdated} } @reviews;

$data->{ratings} = {
all => @all ? sprintf( "%.1f",$sum{all} / @all ) : undef,
recent => @recent ? sprintf( "%.1f",$sum{recent} / @recent ) : undef,
};
} else {
$data->{ratings} = {};
}

return JSON->new->utf8->encode( $data );
}

sub as_rss {
my ($self, $reviews, $mode, $id) = @_;
Expand Down
10 changes: 7 additions & 3 deletions lib/CPANRatings/Control/Show.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ sub render {
my $self = shift;

my ( $mode, $id, $format ) =
( $self->request->path =~ m!^/([ad]|user|dist)/([^/]+?)(?:\.(html|rss))?$! );
( $self->request->path =~ m!^/([ad]|user|dist)/([^/]+?)(?:\.(html|rss|json))?$! );
return 404 unless $mode and $id;

$format = $self->req_param('format') || $format || 'html';
$format = 'html' unless $format eq "rss";
$format = 'html' unless $format =~ /^(rss|json)$/;

if ( $mode eq 'a' ) {
my $user = $self->schema->user->find($id) or return NOT_FOUND;
Expand Down Expand Up @@ -67,7 +67,7 @@ sub render {
my $reviews = $self->schema->review->search(
{ $mode => $id,
status => 'active',
helpful_score => { '>', 0 }
( $format eq 'json' ? () : ( helpful_score => { '>', 0 } ) ),
},
{ order_by => { -desc => 'updated' } }
);
Expand All @@ -92,6 +92,10 @@ sub render {
my $output = $self->as_rss( $self->tpl_param('reviews'), $mode, $mode_element );
return OK, $output, 'application/rss+xml';
}
elsif ( $format eq "json" ) {
my $output = $self->as_json( $self->tpl_param('reviews'), $mode, $mode_element );
return OK, $output, 'application/json';
}

return OK, 'huh? unknown output format', 'text/plain';
}
Expand Down

0 comments on commit bda3872

Please sign in to comment.