File Coverage

blib/lib/Parse/CPAN/Ratings.pm
Criterion Covered Total %
statement 31 32 96.8
branch 1 2 50.0
condition n/a
subroutine 9 9 100.0
pod 2 2 100.0
total 43 45 95.5


line stmt bran cond sub pod time code
1             package Parse::CPAN::Ratings;
2 1     1   1565 use Moose;
  1         544609  
  1         10  
3 1     1   8045 use MooseX::StrictConstructor;
  1         23048  
  1         7  
4 1     1   10466 use MooseX::Types::Path::Class;
  1         150276  
  1         14  
5 1     1   3657 use Parse::CPAN::Ratings::Rating;
  1         34890  
  1         64  
6 1     1   1023 use Parse::CSV;
  1         11501  
  1         100  
7             our $VERSION = '0.33';
8              
9             has 'filename' =>
10             ( is => 'ro', isa => 'Path::Class::File', required => 1, coerce => 1 );
11              
12             has 'db' => (
13             is => 'ro',
14             isa => 'HashRef[Parse::CPAN::Ratings::Rating]',
15             lazy_build => 1,
16             );
17              
18 1     1   13 no Moose;
  1         3  
  1         11  
19              
20             __PACKAGE__->meta->make_immutable;
21              
22             sub _build_db {
23 1     1   2 my $self = shift;
24 1         42 my $filename = $self->filename;
25 1         2 my %db;
26              
27 1         13 my $parser = Parse::CSV->new(
28             file => $filename->stringify,
29             fields => 'auto',
30             );
31 1         518 while ( my $rating = $parser->fetch ) {
32 99         11323 $db{ $rating->{distribution} } = Parse::CPAN::Ratings::Rating->new(
33             distribution => $rating->{distribution},
34             rating => $rating->{rating},
35             review_count => $rating->{review_count},
36             );
37             }
38 1 50       77 if ( $parser->errstr ) {
39 0         0 confess( "Error parsing CSV: " . $parser->errstr );
40             }
41 1         150 return \%db;
42             }
43              
44             sub rating {
45 2     2 1 505 my ( $self, $distribution ) = @_;
46 2         80 return $self->db->{$distribution};
47             }
48              
49             sub ratings {
50 1     1 1 287 my $self = shift;
51 1         2 return values %{ $self->db };
  1         48  
52             }
53              
54             __END__
55              
56             =head1 NAME
57              
58             Parse::CPAN::Ratings - Parse CPAN ratings
59              
60             =head1 SYNOPSIS
61              
62             my $ratings
63             = Parse::CPAN::Ratings->new( filename => 't/all_ratings_100.csv' );
64              
65             my $rating = $ratings->rating('Archive-Zip');
66             print $rating->distribution . "\n"; # Archive-Zip
67             print $rating->rating . "\n"; # 3.8
68             print $rating->review_count . "\n"; # 6
69              
70             my @all_ratings = $ratings->ratings;
71              
72             =head1 DESCRIPTION
73              
74             CPAN ratings is a web site where programmers can rate CPAN modules:
75              
76             http://cpanratings.perl.org/
77            
78             It provides a file containing the average ratings at:
79              
80             http://cpanratings.perl.org/csv/all_ratings.csv
81              
82             This module provides a simple interface to that file.
83              
84             =head1 METHODS
85              
86             =head2 rating
87              
88             Returns a L<Parse::CPAN::Ratings::Rating> object representing
89             the distribution:
90              
91             my $rating = $ratings->rating('Archive-Zip');
92             print $rating->distribution . "\n"; # Archive-Zip
93             print $rating->rating . "\n"; # 3.8
94             print $rating->review_count . "\n"; # 6
95              
96             =head2 ratings
97              
98             Returns a list of all L<Parse::CPAN::Ratings::Rating> objects.
99              
100             my @all_ratings = $ratings->ratings;
101              
102             =head1 SEE ALSO
103              
104             L<Parse::CPAN::Ratings::Rating>.
105              
106             =head1 AUTHOR
107              
108             Leon Brocard <acme@astray.com>.
109              
110             =head1 COPYRIGHT
111              
112             Copyright (C) 2009, Leon Brocard
113              
114             =head1 LICENSE
115              
116             This module is free software; you can redistribute it or modify it
117             under the same terms as Perl itself.