blib/lib/POE/Component/IRC/Plugin/RSS/Headlines.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package POE::Component::IRC::Plugin::RSS::Headlines;
2 $POE::Component::IRC::Plugin::RSS::Headlines::VERSION = '1.10';
3 #ABSTRACT: A POE::Component::IRC plugin that provides RSS headline retrieval.
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 0 ▶use POE::Component::Client::HTTP;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use POE::Component::IRC::Plugin qw(:ALL);
10 use XML::RSS;
11 use HTTP::Request;
12  
13 sub new {
14 my $package = shift;
15 my %args = @_;
16 $args{lc $_} = delete $args{$_} for keys %args;
17 return bless \%args, $package;
18 }
19  
20 sub PCI_register {
21 my ($self,$irc) = @_;
22 $self->{irc} = $irc;
23 $irc->plugin_register( $self, 'SERVER', qw(spoof) );
24 unless ( $self->{http_alias} ) {
25 $self->{http_alias} = join('-', 'ua-rss-headlines', $irc->session_id() );
26 $self->{follow_redirects} ||= 2;
27 POE::Component::Client::HTTP->spawn(
28 Alias => $self->{http_alias},
29 Timeout => 30,
30 FollowRedirects => $self->{follow_redirects},
31 );
32 }
33 $self->{session_id} = POE::Session->create(
34 object_states => [
35 $self => [ qw(_shutdown _start _get_headline _response) ],
36 ],
37 )->ID();
38 $poe_kernel->state( 'get_headline', $self );
39 return 1;
40 }
41  
42 sub PCI_unregister {
43 my ($self,$irc) = splice @_, 0, 2;
44 $poe_kernel->state( 'get_headline' );
45 $poe_kernel->call( $self->{session_id} => '_shutdown' );
46 delete $self->{irc};
47 return 1;
48 }
49  
50 sub _start {
51 my ($kernel,$self) = @_[KERNEL,OBJECT];
52 $self->{session_id} = $_[SESSION]->ID();
53 $kernel->refcount_increment( $self->{session_id}, __PACKAGE__ );
54 undef;
55 }
56  
57 sub _shutdown {
58 my ($kernel,$self) = @_[KERNEL,OBJECT];
59 $kernel->alarm_remove_all();
60 $kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ );
61 $kernel->call( $self->{http_alias} => 'shutdown' );
62 undef;
63 }
64  
65 sub get_headline {
66 my ($kernel,$self,$session) = @_[KERNEL,OBJECT,SESSION];
67 $kernel->post( $self->{session_id}, '_get_headline', @_[ARG0..$#_] );
68 undef;
69 }
70  
71 sub _get_headline {
72 my ($kernel,$self) = @_[KERNEL,OBJECT];
73 my %args;
74 if ( ref $_[ARG0] eq 'HASH' ) {
75 %args = %{ $_[ARG0] };
76 } else {
77 %args = @_[ARG0..$#_];
78 }
79 $args{lc $_} = delete $args{$_} for grep { !/^_/ } keys %args;
80 return unless $args{url};
81 $args{irc_session} = $self->{irc}->session_id();
82 $kernel->post( $self->{http_alias}, 'request', '_response', HTTP::Request->new( GET => $args{url} ), \%args );
83 undef;
84 }
85  
86 sub _response {
87 my ($kernel,$self,$request,$response) = @_[KERNEL,OBJECT,ARG0,ARG1];
88 my $args = $request->[1];
89 my @params;
90 push @params, delete $args->{irc_session}, '__send_event';
91 my $result = $response->[0];
92 if ( $result->is_success ) {
93 my $str = $result->content;
94 my $rss = XML::RSS->new();
95 eval { $rss->parse($str); };
96 if ($@) {
97 push @params, 'irc_rssheadlines_error', $args, $@;
98 } else {
99 push @params, 'irc_rssheadlines_items', $args;
100 push @params, $_->{'title'} for @{ $rss->{'items'} };
101 }
102 } else {
103 push @params, 'irc_rssheadlines_error', $args, $result->status_line;
104 }
105 $kernel->post( @params );
106 undef;
107 }
108  
109 qq[Read all about it!];
110  
111 __END__
↑ Summary