blib/lib/POE/Component/Client/Feed.pm

filter: Statementstmt 100% 8 / 8 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package POE::Component::Client::Feed;
2 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 $POE::Component::Client::Feed::AUTHORITY = 'cpan:GETTY';
4 }
5 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $POE::Component::Client::Feed::VERSION = '0.901';
7 }
8 # ABSTRACT: Event based feed client
9  
10 1 ▶use MooseX::POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use POE qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 Component::Client::HTTP
14 1 );
15  
16 use HTTP::Request;
17 use XML::Feed;
18  
19 our $VERSION ||= '0.0development';
20  
21 has logger => (
22 isa => 'Object',
23 is => 'rw',
24 predicate => 'has_logger',
25 );
26  
27 has http_agent => (
28 is => 'ro',
29 isa => 'Str',
30 default => sub { __PACKAGE__.'/'.$VERSION },
31 required => 1,
32 );
33  
34 has alias => (
35 is => 'ro',
36 isa => 'Str',
37 required => 1,
38 default => sub { 'feed' },
39 );
40  
41 has http_alias => (
42 is => 'ro',
43 isa => 'Str',
44 required => 1,
45 lazy => 1,
46 default => sub {
47 my ( $self ) = @_;
48 $self->http_client;
49 return $self->_http_alias;
50 },
51 );
52  
53 has _http_alias => (
54 is => 'ro',
55 isa => 'Str',
56 lazy => 1,
57 default => sub {
58 my ( $self ) = @_;
59 return $self->alias.'_http';
60 },
61 );
62  
63 has http_timeout => (
64 is => 'ro',
65 isa => 'Int',
66 required => 1,
67 default => sub { 30 },
68 );
69  
70 has http_keepalive => (
71 isa => 'POE::Component::Client::Keepalive',
72 is => 'ro',
73 lazy => 1,
74 default => sub {
75 my $self = shift;
76 $self->logger->info($self->logger_prefix.'Startup POE::Component::Client::Keepalive') if $self->has_logger;
77 POE::Component::Client::Keepalive->new(
78 keep_alive => 20, # seconds to keep connections alive
79 max_open => 100, # max concurrent connections - total
80 max_per_host => 100, # max concurrent connections - per host
81 timeout => 10, # max time (seconds) to establish a new connection
82 )
83 },
84 );
85  
86 has http_followredirects => (
87 is => 'ro',
88 isa => 'Int',
89 required => 1,
90 default => sub { 5 },
91 );
92  
93 has http_client => (
94 is => 'ro',
95 lazy => 1,
96 default => sub {
97 my ( $self ) = @_;
98 $self->logger->info($self->logger_prefix.'Startup POE::Component::Client::HTTP') if $self->has_logger;
99 POE::Component::Client::HTTP->spawn(
100 Agent => $self->http_agent,
101 Alias => $self->_http_alias,
102 Timeout => $self->http_timeout,
103 ConnectionManager => $self->http_keepalive,
104 FollowRedirects => $self->http_followredirects,
105 );
106 },
107 );
108  
109 sub START {
110 my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
111 $kernel->alias_set($self->alias);
112 }
113  
114 event 'request' => sub {
115 my ( $self, $sender, $feed, $response_event, $tag ) = @_[ OBJECT, SENDER, ARG0..$#_ ];
116 $self->logger->debug($self->logger_prefix.'Request of feed '.$feed.' requested') if $self->has_logger;
117 $response_event = 'feed_received' if !$response_event;
118 my $request;
119 if (ref $feed) {
120 $request = $feed;
121 } else {
122 $request = HTTP::Request->new(GET => $feed);
123 }
124 POE::Kernel->post(
125 $self->http_alias,
126 'request',
127 'http_received',
128 $request,
129 [ $sender, $feed, $response_event, $tag ],
130 );
131 };
132  
133 event 'http_received' => sub {
134 my ( $self, @args ) = @_[ OBJECT, ARG0..$#_ ];
135 $self->logger->debug($self->logger_prefix.'HTTP Received') if $self->has_logger;
136 my $request_packet = $args[0];
137 my $response_packet = $args[1];
138 my $request_object = $request_packet->[0];
139 my $response_object = $response_packet->[0];
140 my ( $sender, $feed, $response_event, $tag ) = @{$request_packet->[1]};
141 $self->logger->debug($self->logger_prefix.'Received from '.$feed) if $self->has_logger;
142 my $content = $response_object->content;
143 my $xml_feed;
144 eval {
145 $xml_feed = XML::Feed->parse(\$content);
146 $xml_feed = XML::Feed->errstr if !$xml_feed;
147 };
148 $xml_feed = $@ if $@;
149 # i dont understand that really... need a case (Getty)
150 # if (ref $response_event) {
151 # $response_event->postback->($xml_feed);
152 # } else {
153 $self->logger->debug($self->logger_prefix.'Post result') if $self->has_logger;
154 POE::Kernel->post( $sender, $response_event, $request_object, $xml_feed, $tag );
155 # }
156 };
157  
158 sub logger_prefix {
159 my $self = shift;
160 __PACKAGE__.' ('.$self->get_session_id.') ';
161 }
162  
163 1;
164  
165  
166  
167 =pod
168  
169 =head1 NAME
170  
171 POE::Component::Client::Feed - Event based feed client
172  
173 =head1 VERSION
174  
175 version 0.901
176  
177 =head1 SYNOPSIS
178  
179 package MyServer;
180 use MooseX::POE;
181 use POE::Component::Client::Feed;
182  
183 has feed_client => (
184 is => 'ro',
185 default => sub {
186 POE::Component::Client::Feed->new();
187 }
188 );
189  
190 event feed_received => sub {
191 my ( $self, @args ) = @_[ OBJECT, ARG0..$#_ ];
192 my $http_request = $args[0];
193 my $xml_feed = $args[1];
194 my $tag = $args[2];
195 };
196  
197 sub START {
198 my ( $self ) = @_;
199 $self->feed_client->yield('request','http://news.perlfoundation.org/atom.xml','feed_received','tag');
200 }
201  
202 =head1 DESCRIPTION
203  
204 This POE Component gives you like L<POE::Component::Client::HTTP> an event based way of fetching from a feed. It is not made
205 for making consume a feed and only get events on new headlines, for this you can use L<POE::Component::FeedAggregator> which is
206 based on this module, or L<POE::Component::RSSAggregator>.
207  
208 =head1 SEE ALSO
209  
210 =over 4
211  
212 =item *
213  
214 L<POE::Component::FeedAggregator>
215  
216 =item *
217  
218 L<POE::Component::Client::HTTP>
219  
220 =item *
221  
222 L<XML::Feed>
223  
224 =item *
225  
226 L<MooseX::POE>
227  
228 =item *
229  
230 L<POE::Component::RSSAggregator>
231  
232 =back
233  
234 =head1 AUTHOR
235  
236 Torsten Raudssus <torsten@raudssus.de>
237  
238 =head1 COPYRIGHT AND LICENSE
239  
240 This software is copyright (c) 2010 by L<Raudssus Social Software|http://www.raudssus.de/>.
241  
242 This is free software; you can redistribute it and/or modify it under
243 the same terms as the Perl 5 programming language system itself.
244  
245 =cut
246  
247  
248 __END__
↑ Summary