File Coverage

blib/lib/POE/Component/RSS.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1             package POE::Component::RSS;
2              
3             =head1 NAME
4              
5             POE::Component::RSS - Event based RSS parsing
6              
7             =head1 SYNOPSIS
8              
9             use POE qw(Component::RSS);
10              
11             POE::Component::RSS->spawn();
12              
13             $kernel->post(
14             'rss',
15             'parse' => {
16             Item => 'item_state',
17             },
18             $rss_string
19             );
20              
21             =head1 DESCRIPTION
22              
23             POE::Component::RSS is an event based RSS parsing module. It wraps
24             XML::RSS and provides a POE based framework for accessing the information
25             provided.
26              
27             =head1 INTERFACE
28              
29             =cut
30              
31 1     1   37721 use warnings;
  1         2  
  1         37  
32 1     1   6 use strict;
  1         1  
  1         35  
33              
34 1     1   505 use POE;
  0            
  0            
35             use Carp qw(croak);
36              
37             use XML::RSS;
38             use Params::Validate qw(validate_with SCALAR);
39              
40             our $VERSION = '3.01';
41              
42             =head2 spawn
43              
44             RSS parser components are not normal objects, but are instead 'spawned'
45             as separate sessions. This is done with PoCo::RSS's 'spawn' method, which
46             takes one named parameter:
47              
48             =over 4
49              
50             =item C<< Alias => $alias_name >>
51              
52             'Alias' sets the name by which the session is known. If no alias
53             is given, the component defaults to 'rss'. It's possible to spawn
54             several RSS components with different names.
55              
56             =back
57              
58             =cut
59              
60             sub spawn { #{{{
61             my $class = shift;
62              
63             my %params = validate_with(
64             params => \@_,
65             spec => {
66             Alias => {
67             type => SCALAR,
68             default => 'rss',
69             optional => 1,
70             },
71             },
72             );
73              
74             my $alias = delete $params{'Alias'};
75              
76             POE::Session->create(
77             inline_states => {
78             _start => \&rss_start,
79             _stop => sub {},
80             parse => \&got_parse,
81             },
82             args => [ $alias ],
83             );
84              
85             return;
86             } #}}}
87              
88              
89             =begin devel
90              
91             =head2 got_parse
92              
93             Accepts parse requests, runs the documents through XML::Parser and generates
94             events. Unfortunately, this call is entirely blocking and, if given a sizeable
95             document, could take a while to return.
96              
97             =cut
98              
99             sub got_parse { #{{{
100             my ($return_states, $rss_string, $rss_identity_tag) = @_[ARG0, ARG1, ARG2];
101              
102             my @rss_tag;
103              
104             if (defined($rss_identity_tag)) {
105             @rss_tag = ($rss_identity_tag);
106             } else {
107             @rss_tag = ();
108             }
109              
110             my $rss_parser = XML::RSS->new();
111            
112             $rss_parser->parse($rss_string);
113            
114             if (exists $return_states->{'Start'}) {
115             $_[KERNEL]->post(
116             $_[SENDER],
117             $return_states->{'Start'},
118             @rss_tag
119             );
120             }
121              
122             if (exists $return_states->{'Item'}) {
123             foreach my $item (@{$rss_parser->{'items'}}) {
124             # $item->{'title'}
125             # $item->{'link'}
126             $_[KERNEL]->post(
127             $_[SENDER],
128             $return_states->{'Item'},
129             @rss_tag,
130             $item
131             );
132             }
133             }
134              
135             if (exists $return_states->{'Channel'}) {
136             $_[KERNEL]->post(
137             $_[SENDER],
138             $return_states->{'Channel'},
139             @rss_tag,
140             $rss_parser->{'channel'}
141             );
142             }
143              
144             if (exists $return_states->{'Image'}) {
145             $_[KERNEL]->post(
146             $_[SENDER],
147             $return_states->{'Image'},
148             @rss_tag,
149             $rss_parser->{'image'}
150             );
151             }
152              
153             if (exists $return_states->{'Textinput'}) {
154             $_[KERNEL]->post(
155             $_[SENDER],
156             $return_states->{'Textinput'},
157             @rss_tag,
158             $rss_parser->{'textinput'}
159             );
160             }
161            
162             if (exists $return_states->{'Stop'}) {
163             $_[KERNEL]->post(
164             $_[SENDER],
165             $return_states->{'Stop'},
166             @rss_tag
167             );
168             }
169              
170             return;
171              
172             } #}}}
173              
174              
175             =head2 rss_start
176              
177             Just sets our alias
178              
179             =cut
180              
181             sub rss_start { #{{{
182             $_[KERNEL]->alias_set($_[ARG0]);
183             } #}}}
184              
185             1;
186             __END__