File Coverage

blib/lib/POE/Component/IRC/Plugin/WWW/CPANRatings/RSS.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package POE::Component::IRC::Plugin::WWW::CPANRatings::RSS;
2              
3 1     1   257002 use warnings;
  1         3  
  1         35  
4 1     1   6 use strict;
  1         2  
  1         52  
5              
6             our $VERSION = '0.0106';
7              
8 1     1   5 use POE qw/Component::WWW::CPANRatings::RSS/;
  1         7  
  1         8  
9             use POE::Component::IRC::Plugin qw( :ALL );
10             use utf8;
11              
12             sub new {
13             my $package = shift;
14             my %args = @_;
15             my $self = bless {}, $package;
16              
17             $self->{ +lc } = delete $args{ $_ }
18             for keys %args;
19              
20             $self->{rate} ||= 60;
21             $self->{ua} ||= { timeout => 30 };
22             $self->{file} = 'cpan_ratings.rss.storable'
23             unless defined $self->{file};
24              
25             $self->{format}
26             ||= 'rating: {:dist:} - {:rating:} - by {:creator:} [ {:link:} ]';
27              
28             $self->{channels} ||= [];
29             $self->{response_event} ||= 'irc_cpanratings';
30             $self->{auto} = 1
31             unless defined $self->{auto};
32              
33             $self->{utf} = 1
34             unless defined $self->{utf};
35              
36             $self->{max_ratings} = 5
37             unless defined $self->{max_ratings};
38              
39             return $self;
40             }
41              
42             sub PCI_register {
43             my ($self, $irc) = splice @_, 0, 2;
44              
45             $self->{irc} = $irc;
46              
47             $self->{poco} = POE::Component::WWW::CPANRatings::RSS->spawn(
48             debug => $self->{debug},
49             ua => $self->{ua},
50             );
51              
52             $self->{SESSION_ID} = POE::Session->create(
53             object_states => [
54             $self => [qw(_start _shutdown ratings)],
55             ],
56             )->ID();
57              
58             return 1;
59             }
60              
61             sub PCI_unregister {
62             my ($self, $irc) = splice @_, 0, 2;
63              
64             $poe_kernel->call( $self->{SESSION_ID} => '_shutdown' );
65             delete $self->{irc};
66             return 1;
67             }
68              
69             sub _start {
70             my ($kernel, $self) = @_[KERNEL, OBJECT];
71             $self->{SESSION_ID} = $_[SESSION]->ID();
72              
73             $kernel->refcount_increment( $self->{SESSION_ID}, __PACKAGE__ );
74              
75             $self->{poco}->fetch( {
76             event => 'ratings',
77             repeat => $self->{rate},
78             unique => 1,
79             file => $self->{file},
80             }
81             );
82              
83             return;
84             }
85              
86             sub ratings {
87             my ( $self, $in_ref ) = @_[ OBJECT, ARG0 ];
88             if ( $in_ref->{error} ) {
89             $self->{debug}
90             and warn "Got ratings error: $in_ref->{error}\n";
91             return;
92             }
93              
94             my @ratings = @{ $in_ref->{ratings} || [] };
95             for my $review ( splice @ratings, 0, $self->{max_ratings} ) {
96             my $text = $self->{format};
97             my $rating = $review->{rating};
98             if ( $self->{utf} and $rating ne 'N/A' ) {
99             $rating = $self->_make_utf_rating( $rating );
100             }
101             elsif ( $rating ne 'N/A' ) {
102             $rating = "$rating/5";
103             }
104             $text =~ s/{:dist:}/$review->{dist}/g;
105             $text =~ s/{:rating:}/$rating/g;
106             $text =~ s/{:creator:}/$review->{creator}/g;
107             $text =~ s/{:link:}/$review->{link}/g;
108              
109             if ( $self->{auto} ) {
110             $self->{irc}->yield( ctcp => $_ => 'ACTION ' . $text )
111             for @{ $self->{channels} };
112             }
113             }
114             my %event_output = %$in_ref;
115             delete $event_output{unique};
116             $event_output{rate} = delete $event_output{repeat};
117             $self->{irc}->send_event( $self->{response_event} => \%event_output );
118             }
119              
120             sub _shutdown {
121             my ($kernel, $self) = @_[KERNEL, OBJECT];
122             $self->{poco}->shutdown;
123             $kernel->alarm_remove_all();
124             $kernel->refcount_decrement( $self->{SESSION_ID}, __PACKAGE__ );
125             return;
126             }
127              
128             sub _make_utf_rating {
129             my ( $self, $rating ) = @_;
130              
131             my $out = '●' x int $rating;
132             $out .= '◐'
133             if $rating - int $rating;
134             $out .= '○' x (5 - length $out);
135              
136             return $out;
137             }
138              
139             1;
140             __END__