blib/lib/POE/Component/WWW/XKCD/AsText.pm

filter: Statementstmt 65.8% 77 / 117 Branchbran 43.4% 20 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 70.0% 14 / 20 Podpod 100% 4 / 4 totaltotal 61.5% 115 / 187 SCAR 43.8
CC
28
Cov
60%
_wheel
20.0
_retrieve
12.9
_child_closed
12.0
CRAP 80.0
↑ Summary Build »
1 package POE::Component::WWW::XKCD::AsText;
2  
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = '0.002';
7  
8 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use WWW::XKCD::AsText;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use POE qw( Filter::Reference Filter::Line Wheel::Run );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub spawn {
13 6 ▶ my $package = shift;
Subroutine spawn: called CC=5 SCAR=16.5
Pod documented
14 6 ▶ croak "$package requires an even number of arguments"
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 6 6
15 if @_ & 1;
16  
17 6 my %params = @_;
18  
19 6 $params{ lc $_ } = delete $params{ $_ } for keys %params;
20  
21 delete $params{options}
22 6 ▶ unless ref $params{options} eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $params{"options"} eq "HASH" 6 0 6
23  
24 $params{obj_args} = {
25 timeout => delete( $params{timeout} ),
26 6 ua => delete( $params{ua} ),
27 };
28  
29 6 my $self = bless \%params, $package;
30  
31 $self->{session_id} = POE::Session->create(
32 object_states => [
33 $self => {
34 retrieve => '_retrieve',
35 shutdown => '_shutdown',
36 },
37 $self => [
38 qw(
39 _child_error
40 _child_closed
41 _child_stdout
42 _child_stderr
43 _sig_child
44 _start
45 )
46 ]
47 ],
48 6 ▶ ( defined $params{options} ? ( options => $params{options} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined $params{'options'} ? : 0 6 6
49 )->ID();
50  
51 6 return $self;
52 }
53  
54  
55 sub _start {
56 6 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _start: called CC=4 SCAR=14.1
57 6 $self->{session_id} = $_[SESSION]->ID();
58  
59 6 ▶ if ( $self->{alias} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'alias'}) { } 2 4 6
60 2 $kernel->alias_set( $self->{alias} );
61 }
62 else {
63 4 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
64 }
65  
66 $self->{wheel} = POE::Wheel::Run->new(
67 0 ▶ Program => sub{ _wheel( $self->{obj_args} ); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 6 ▶ ErrorEvent => '_child_error',
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 6 6
69 CloseEvent => '_child_close',
70 StdoutEvent => '_child_stdout',
71 StderrEvent => '_child_stderr',
72 StdioFilter => POE::Filter::Reference->new,
73 StderrFilter => POE::Filter::Line->new,
74 ( $^O eq 'MSWin32' ? ( CloseOnCall => 0 ) : ( CloseOnCall => 1 ) )
75 );
76  
77 $kernel->yield('shutdown')
78 6 ▶ unless $self->{wheel};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"wheel"} 0 6 6
79  
80 6 $kernel->sig_child( $self->{wheel}->PID(), '_sig_child' );
81  
82 6 undef;
83 }
84  
85 sub _sig_child {
86 6 ▶ $poe_kernel->sig_handled;
Subroutine _sig_child: called CC=1 SCAR=0.0
87 }
88  
89 sub session_id {
90 0 ▶ return $_[0]->{session_id};
Subroutine session_id: not called CC=1 SCAR=6.9
Pod documented
91 }
92  
93 sub retrieve {
94 3 ▶ my $self = shift;
Subroutine retrieve: called CC=1 SCAR=0.0
Pod documented
95 3 $poe_kernel->post( $self->{session_id} => 'retrieve' => @_ );
96 }
97  
98 sub _retrieve {
99 5 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _retrieve: called CC=9 SCAR=25.6
100 5 my $sender = $_[SENDER]->ID;
101  
102 return
103 5 ▶ if $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"shutdown"} 0 5 5
104  
105 5 my $args;
106 5 ▶ if ( ref $_[ARG0] eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[10] eq 'HASH') { } 5 0 5
107 5 $args = { %{ $_[ARG0] } };
108 }
109 else {
110 0 carp "First parameter must be a hashref, trying to adjust...";
111 0 $args = { @_[ARG0 .. $#_] };
112 }
113 $args->{ lc $_ } = delete $args->{ $_ }
114 5 for grep { !/^_/ } keys %$args;
115  
116 5 ▶ unless ( $args->{event} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"event"}) 0 5 5
117 0 carp "Missing 'event' parameter to retrieve";
118 0 return;
119 }
120 5 ▶ unless ( $args->{id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args->{"id"}) 0 5 5
121 0 carp "Missing 'id' parameter to retrieve";
122 0 return;
123 }
124  
125 5 $args->{id} =~ s/\s+//g;
126  
127 5 ▶ if ( $args->{id} =~ /\D/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"id"} =~ /\D/) 0 5 5
128 0 carp "ID number must be a NUMBER";
129 0 return;
130 }
131  
132 5 ▶ if ( $args->{session} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'session'}) { } 2 3 5
133 2 ▶ if ( my $ref = $kernel->alias_resolve( $args->{session} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $kernel->alias_resolve($args->{'session'})) { } 2 0 2
134 2 $args->{sender} = $ref->ID;
135 }
136 else {
137 0 carp "Could not resolve 'session' parameter to a valid"
138 . " POE session";
139 0 return;
140 }
141 }
142 else {
143 3 $args->{sender} = $sender;
144 }
145  
146 5 $kernel->refcount_increment( $args->{sender} => __PACKAGE__ );
147 5 $self->{wheel}->put( $args );
148  
149 5 undef;
150 }
151  
152 sub shutdown {
153 4 ▶ my $self = shift;
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
154 4 $poe_kernel->call( $self->{session_id} => 'shutdown' => @_ );
155 }
156  
157 sub _shutdown {
158 6 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _shutdown: called CC=4 SCAR=13.9
159 6 $kernel->alarm_remove_all;
160 6 $kernel->alias_remove( $_ ) for $kernel->alias_list;
161 $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ )
162 6 ▶ unless $self->{alias};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"alias"} 4 2 6
163  
164 6 $self->{shutdown} = 1;
165  
166 $self->{wheel}->shutdown_stdin
167 6 ▶ if $self->{wheel};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"wheel"} 6 0 6
168 }
169  
170 sub _child_closed {
171 0 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _child_closed: not called CC=3 SCAR=24.8
172  
173 carp "_child_closed called (@_[ARG0..$#_])\n"
174 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
175  
176 0 delete $self->{wheel};
177 $kernel->yield('shutdown')
178 0 ▶ unless $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 0 0
179  
180 0 undef;
181 }
182  
183 sub _child_error {
184 6 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _child_error: called CC=3 SCAR=11.3
185 carp "_child_error called (@_[ARG0..$#_])\n"
186 6 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 6 0 6
187  
188 6 delete $self->{wheel};
189 $kernel->yield('shutdown')
190 6 ▶ unless $self->{shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"shutdown"} 0 6 6
191  
192 6 undef;
193 }
194  
195 sub _child_stderr {
196 0 ▶ my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
Subroutine _child_stderr: not called CC=2 SCAR=17.9
197 carp "_child_stderr: $_[ARG0]\n"
198 0 ▶ if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
199  
200 0 undef;
201 }
202  
203 sub _child_stdout {
204 5 ▶ my ( $kernel, $self, $input ) = @_[ KERNEL, OBJECT, ARG0 ];
Subroutine _child_stdout: called CC=1 SCAR=0.0
205  
206 5 my $session = delete $input->{sender};
207 5 my $event = delete $input->{event};
208  
209 5 $kernel->post( $session, $event, $input );
210 5 $kernel->refcount_decrement( $session => __PACKAGE__ );
211  
212 5 undef;
213 }
214  
215 sub _wheel {
216 0 ▶ my $obj_args = shift;
Subroutine _wheel: not called CC=4 SCAR=30.0
217  
218 0 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
219 0 binmode STDIN;
220 0 binmode STDOUT;
221 }
222  
223 0 my $raw;
224 0 my $size = 4096;
225 0 my $filter = POE::Filter::Reference->new;
226  
227 0 my $xkcd = WWW::XKCD::AsText->new( %$obj_args );
228  
229 0 ▶ while ( sysread STDIN, $raw, $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if (sysread STDIN, $raw, $size) 0 0 0
230 0 my $requests = $filter->get( [ $raw ] );
231 0 foreach my $req_ref ( @$requests ) {
232  
233 0 _process_request( $xkcd, $req_ref ); # changes $req_ref
234  
235 0 my $response = $filter->put( [ $req_ref ] );
236 0 print STDOUT @$response;
237 }
238 }
239 }
240  
241 sub _process_request {
242 0 ▶ my ( $xkcd, $req_ref ) = @_;
Subroutine _process_request: not called CC=2 SCAR=17.9
243 0 my $text = $xkcd->retrieve( $req_ref->{id} );
244  
245 0 ▶ if ( defined $text ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $text) { } 0 0 0
246 0 @$req_ref{ qw(uri text) } = ( $xkcd->uri, $text );
247 }
248 else {
249 0 $req_ref->{error} = $xkcd->error;
250 }
251  
252 0 undef;
253 }
254  
255  
256 6 1;
257 __END__
258  
259 =encoding utf8
260  
261  
262 =head1 NAME
263  
264 POE::Component::WWW::XKCD::AsText - non-blocking wrapper around WWW::XKCD::AsText
265  
266 =head1 SYNOPSIS
267  
268 use strict;
269 use warnings;
270  
271 use POE qw(Component::WWW::XKCD::AsText);
272  
273 my $poco = POE::Component::WWW::XKCD::AsText->spawn;
274  
275 POE::Session->create(
276 package_states => [ main => [qw(_start retrieved )] ],
277 );
278  
279 $poe_kernel->run;
280  
281 sub _start {
282 $poco->retrieve( {
283 id => 333,
284 event => 'retrieved',
285 }
286 );
287 }
288  
289 sub retrieved {
290 my $in = $_[ARG0];
291  
292 if ( $in->{error} ) {
293 print "Error: $in->{error}\n";
294 }
295 else {
296 printf "The comic on %s is:\n%s\n",
297 @$in{ qw(uri text) };
298 }
299  
300 $poco->shutdown;
301 }
302  
303 Using event based interface is also possible of course.
304  
305 =head1 DESCRIPTION
306  
307 The module is a non-blocking wrapper around L<WWW::XKCD::AsText>
308 which provides interface to retrieve comics on L<http://xkcd.com> in text
309 format (transcriptions)
310  
311 =head1 CONSTRUCTOR
312  
313 =head2 spawn
314  
315 my $poco = POE::Component::WWW::XKCD::AsText->spawn;
316  
317 POE::Component::WWW::XKCD::AsText->spawn(
318 alias => 'xkcd',
319 timeout => 10,
320 # or: ua => LWP::UserAgent->new( timeout => 10, agent => 'blah),
321 options => {
322 debug => 1,
323 trace => 1,
324 # POE::Session arguments for the component
325 },
326 debug => 1, # output some debug info
327 );
328  
329 The C<spawn> method returns a
330 POE::Component::WWW::XKCD::AsText object. It takes a few arguments,
331 I<all of which are optional>. The possible arguments are as follows:
332  
333 =head3 alias
334  
335 POE::Component::WWW::XKCD::AsText->spawn( alias => 'xkcd' );
336  
337 B<Optional>. Specifies a POE Kernel alias for the component.
338  
339 =head3 timeout
340  
341 ->spawn( timeout => 10 );
342  
343 B<Optional>. Specifies the timeout argument of L<LWP::UserAgent>'s
344 constructor, which is used for retrieving. B<Defaults to>: C<30> seconds.
345  
346 =head3 ua
347  
348 ->spawn( ua => LWP::UserAgent->new( agent => 'Foos!' ) );
349  
350 B<Optional>. If the C<timeout> argument is not enough for your needs of
351 mutilating the L<LWP::UserAgent> object used for retrieving, feel
352 free to specify the C<ua> argument which takes an L<LWP::UserAgent> object
353 as a value. B<Note:> the C<timeout> argument to the constructor will not do
354 anything if you specify the C<ua> argument as well. B<Defaults to:> plain
355 boring default L<LWP::UserAgent> object with C<timeout> argument set to
356 whatever POE::Component::WWW::XKCD::AsText's C<timeout> argument is
357 set to as well as C<agent> argument is set to mimic Firefox.
358  
359 =head3 options
360  
361 my $poco = POE::Component::WWW::XKCD::AsText->spawn(
362 options => {
363 trace => 1,
364 default => 1,
365 },
366 );
367  
368 B<Optional>.
369 A hashref of POE Session options to pass to the component's session.
370  
371 =head3 debug
372  
373 my $poco = POE::Component::WWW::XKCD::AsText->spawn(
374 debug => 1
375 );
376  
377 When set to a true value turns on output of debug messages. B<Defaults to:>
378 C<0>.
379  
380 =head1 METHODS
381  
382 =head2 retrieve
383  
384 $poco->retrieve( {
385 event => 'event_for_output',
386 id => 333,
387 _blah => 'pooh!',
388 session => 'other',
389 }
390 );
391  
392 Takes a hashref as an argument, does not return a sensible return value.
393 See C<retrieve> event's description for more information.
394  
395 =head2 session_id
396  
397 my $poco_id = $poco->session_id;
398  
399 Takes no arguments. Returns component's session ID.
400  
401 =head2 shutdown
402  
403 $poco->shutdown;
404  
405 Takes no arguments. Shuts down the component.
406  
407 =head1 ACCEPTED EVENTS
408  
409 =head2 retrieve
410  
411 $poe_kernel->post( xkcd => retrieve => {
412 event => 'event_for_output',
413 id => 333,
414 _blah => 'pooh!',
415 session => 'other',
416 }
417 );
418  
419 =head3 event
420  
421 { event => 'results_event', }
422  
423 B<Mandatory>. Specifies the name of the event to emit when results are
424 ready. See OUTPUT section for more information.
425  
426 =head3 id
427  
428 { id => 333 }
429  
430 B<Mandatory>. As a value takes an ID of the comic. In other words, if you
431 want to read the comic on L<http://xkcd.com/333/> you would specify C<333>
432 as an C<id> argument's value.
433  
434 =head3 session
435  
436 { session => 'other' }
437  
438 { session => $other_session_reference }
439  
440 { session => $other_session_ID }
441  
442 B<Optional>. Takes either an alias, reference or an ID of an alternative
443 session to send output to.
444  
445 =head3 user defined
446  
447 {
448 _user => 'random',
449 _another => 'more',
450 }
451  
452 B<Optional>. Any keys starting with C<_> (underscore) will not affect the
453 component and will be passed back in the result intact.
454  
455 =head2 shutdown
456  
457 $poe_kernel->post( xkcd => 'shutdown' );
458  
459 Takes no arguments. Tells the component to shut itself down.
460  
461 =head1 OUTPUT
462  
463 $VAR1 = {
464 'text' => 'comic\'s text here',
465 'uri' => bless( do{\(my $o = 'http://xkcd.com/333/')}, 'URI::http' ),
466 };
467  
468 The event handler set up to handle the event which you've specified in
469 the C<event> argument to C<retrieve()> method/event will recieve input
470 in the C<$_[ARG0]> in a form of a hashref. The possible keys/value of
471 that hashref are as follows:
472  
473 =head2 text
474  
475 { 'text' => 'comic\'s text here' }
476  
477 The C<text> key will contain the transcriptions of the comic.
478  
479 =head2 error
480  
481 { 'error' => 'No text for comic' }
482  
483 If there was some problem while retrieving text for the comic, or text
484 for the comic does not exist yet (e.g. comic is very new and transcription
485 has not been submitted yet) the C<error> key will be present and will
486 contain the description of the error.
487  
488 =head2 uri
489  
490 { 'uri' => bless( do{\(my $o = 'http://xkcd.com/333/')}, 'URI::http' ) }
491  
492 The C<uri> key will contain the L<URI> object pointing to the page
493 of the paste you've retrieved.
494  
495 =head2 user defined
496  
497 { '_blah' => 'foos' }
498  
499 Any arguments beginning with C<_> (underscore) passed into the C<retrieve()>
500 event/method will be present intact in the result.
501  
502 =head1 SEE ALSO
503  
504 L<POE>, L<LWP::UserAgent>, L<WWW::XKCD::AsText>
505  
506 =head1 AUTHOR
507  
508 Zoffix Znet, C<< <zoffix at cpan.org> >>
509 (L<http://zoffix.com>, L<http://haslayout.net>)
510  
511 =head1 AUTHOR
512  
513 Zoffix Znet, C<< <zoffix at cpan.org> >>
514 (L<http://zoffix.com>, L<http://haslayout.net>)
515  
516 =head1 BUGS
517  
518 Please report any bugs or feature requests to C<bug-poe-component-www-xkcd-astext at rt.cpan.org>, or through
519 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=POE-Component-WWW-XKCD-AsText>. I will be notified, and then you'll
520 automatically be notified of progress on your bug as I make changes.
521  
522 =head1 SUPPORT
523  
524 You can find documentation for this module with the perldoc command.
525  
526 perldoc POE::Component::WWW::XKCD::AsText
527  
528 You can also look for information at:
529  
530 =over 4
531  
532 =item * RT: CPAN's request tracker
533  
534 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=POE-Component-WWW-XKCD-AsText>
535  
536 =item * AnnoCPAN: Annotated CPAN documentation
537  
538 L<http://annocpan.org/dist/POE-Component-WWW-XKCD-AsText>
539  
540 =item * CPAN Ratings
541  
542 L<http://cpanratings.perl.org/d/POE-Component-WWW-XKCD-AsText>
543  
544 =item * Search CPAN
545  
546 L<http://search.cpan.org/dist/POE-Component-WWW-XKCD-AsText>
547  
548 =back
549  
550 =head1 COPYRIGHT & LICENSE
551  
552 Copyright 2008 Zoffix Znet, all rights reserved.
553  
554 This program is free software; you can redistribute it and/or modify it
555 under the same terms as Perl itself.
556  
557 =cut
↑ Summary Build »