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 CC 28 SCAR 19.7
CC
28
Cov
60%
_wheel
20.0
_retrieve
12.9
_child_closed
12.0
CRAP 7.2
↑ Summary
1 package POE::Component::WWW::XKCD::AsText;
2  
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = '0.003';
7  
8 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use WWW::XKCD::AsText;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use POE qw( Filter::Reference Filter::Line Wheel::Run );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶sub spawn {
Subroutine spawn: called CC=5 SCAR=16.5
Pod documented
13 6 my $package = shift;
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 6 ▶sub _start {
Subroutine _start: called CC=4 SCAR=14.1
56 6 my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
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 6 ▶sub _sig_child {
Subroutine _sig_child: called CC=1 SCAR=0.0
86 6 $poe_kernel->sig_handled;
87 }
88  
89 0 ▶sub session_id {
Subroutine session_id: not called CC=1 SCAR=6.9
Pod documented
90 0 return $_[0]->{session_id};
91 }
92  
93 3 ▶sub retrieve {
Subroutine retrieve: called CC=1 SCAR=0.0
Pod documented
94 3 my $self = shift;
95 3 $poe_kernel->post( $self->{session_id} => 'retrieve' => @_ );
96 }
97  
98 5 ▶sub _retrieve {
Subroutine _retrieve: called CC=9 SCAR=25.6
99 5 my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
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 4 ▶sub shutdown {
Subroutine shutdown: called CC=1 SCAR=0.0
Pod documented
153 4 my $self = shift;
154 4 $poe_kernel->call( $self->{session_id} => 'shutdown' => @_ );
155 }
156  
157 6 ▶sub _shutdown {
Subroutine _shutdown: called CC=4 SCAR=13.9
158 6 my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
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 0 ▶sub _child_closed {
Subroutine _child_closed: not called CC=3 SCAR=24.8
171 0 my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
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 6 ▶sub _child_error {
Subroutine _child_error: called CC=3 SCAR=11.3
184 6 my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
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 0 ▶sub _child_stderr {
Subroutine _child_stderr: not called CC=2 SCAR=17.9
196 0 my ( $kernel, $self ) = @_[ KERNEL, OBJECT ];
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 5 ▶sub _child_stdout {
Subroutine _child_stdout: called CC=1 SCAR=0.0
204 5 my ( $kernel, $self, $input ) = @_[ KERNEL, OBJECT, ARG0 ];
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 0 ▶sub _wheel {
Subroutine _wheel: not called CC=4 SCAR=30.0
216 0 my $obj_args = shift;
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 0 ▶sub _process_request {
Subroutine _process_request: not called CC=2 SCAR=17.9
242 0 my ( $xkcd, $req_ref ) = @_;
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 7 1;
257 __END__
↑ Summary