| 1 | package POE::Component::WWW::Shorten; | ||||||||||||
| 2 | 2 | $POE::Component::WWW::Shorten::VERSION = '1.22'; | |||||||||||
| 3 | #ABSTRACT: A non-blocking POE wrapper around WWW::Shorten. | ||||||||||||
| 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 0.38 qw(Wheel::Run Filter::Line Filter::Reference); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 2 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | sub spawn { | ||||||||||
|
Subroutine
spawn: called CC=7 SCAR=20.2
Pod
documented
| |||||||||||||
| 11 | 1 | my $package = shift; | |||||||||||
| 12 | 1 | ▶ | croak "$package requires an even number of parameters" if @_ & 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 13 | 1 | my %parms = @_; | |||||||||||
| 14 | |||||||||||||
| 15 | 1 | $parms{ lc $_ } = delete $parms{$_} for keys %parms; | |||||||||||
| 16 | |||||||||||||
| 17 | 1 | ▶ | delete $parms{'options'} unless ref ( $parms{'options'} ) eq 'HASH'; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | 1 | ▶ | my $type = delete $parms{'type'} || 'TinyURL'; | ||||||||||
|
Condition50%1/2
delete $parms{'type'} || 'TinyURL'
MC/DC0%0/1
delete $parms{'type'} || 'TinyURL'
delete $parms{'type'}
Truth table50%1/2
A
A= delete $parms{'type'}
| |||||||||||||
| 19 | |||||||||||||
| 20 | 1 | eval { | |||||||||||
| 21 | 1 | require WWW::Shorten; | |||||||||||
| 22 | 1 | import WWW::Shorten $type; | |||||||||||
| 23 | }; | ||||||||||||
| 24 | 1 | ▶ | die "Problem loading WWW::Shorten \'$type\', please check\n" if $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 25 | |||||||||||||
| 26 | 1 | my $self = bless \%parms, $package; | |||||||||||
| 27 | |||||||||||||
| 28 | $self->{session_id} = POE::Session->create( | ||||||||||||
| 29 | object_states => [ | ||||||||||||
| 30 | $self => { shorten => '_shorten', | ||||||||||||
| 31 | shutdown => '_shutdown', | ||||||||||||
| 32 | }, | ||||||||||||
| 33 | $self => [ qw(_child_error _child_closed _child_stdout _child_stderr _sig_chld _start _stop) ], | ||||||||||||
| 34 | ], | ||||||||||||
| 35 | 1 | ▶ | ( defined ( $parms{'options'} ) ? ( options => $parms{'options'} ) : () ), | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 36 | )->ID(); | ||||||||||||
| 37 | |||||||||||||
| 38 | 1 | return $self; | |||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | 1 | ▶ | sub _start { | ||||||||||
|
Subroutine
_start: called CC=4 SCAR=14.6
| |||||||||||||
| 42 | 1 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||
| 43 | 1 | $self->{session_id} = $_[SESSION]->ID(); | |||||||||||
| 44 | |||||||||||||
| 45 | 1 | ▶ | if ( $self->{alias} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 46 | 0 | $kernel->alias_set( $self->{alias} ); | |||||||||||
| 47 | } | ||||||||||||
| 48 | else { | ||||||||||||
| 49 | 1 | $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ ); | |||||||||||
| 50 | } | ||||||||||||
| 51 | |||||||||||||
| 52 | 1 | ▶ | $self->{wheel} = POE::Wheel::Run->new( | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 53 | Program => \&_shorten_wheel, | ||||||||||||
| 54 | ErrorEvent => '_child_error', | ||||||||||||
| 55 | CloseEvent => '_child_closed', | ||||||||||||
| 56 | StdoutEvent => '_child_stdout', | ||||||||||||
| 57 | StderrEvent => '_child_stderr', | ||||||||||||
| 58 | StdioFilter => POE::Filter::Reference->new(), | ||||||||||||
| 59 | StderrFilter => POE::Filter::Line->new(), | ||||||||||||
| 60 | ( $^O eq 'MSWin32' ? ( CloseOnCall => 0 ) : ( CloseOnCall => 1 ) ), | ||||||||||||
| 61 | ); | ||||||||||||
| 62 | |||||||||||||
| 63 | 1 | ▶ | $kernel->yield( 'shutdown' ) unless $self->{wheel}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 64 | 1 | $kernel->sig_child( $self->{wheel}->PID, '_sig_chld' ); | |||||||||||
| 65 | 1 | undef; | |||||||||||
| 66 | } | ||||||||||||
| 67 | |||||||||||||
| 68 | 1 | ▶ | sub _stop { | ||||||||||
|
Subroutine
_stop: called CC=1 SCAR=0.0
| |||||||||||||
| 69 | 1 | return; | |||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | 1 | ▶ | sub _sig_chld { | ||||||||||
|
Subroutine
_sig_chld: called CC=1 SCAR=0.0
| |||||||||||||
| 73 | 1 | $poe_kernel->sig_handled(); | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 0 | ▶ | sub session_id { | ||||||||||
|
Subroutine
session_id: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 77 | 0 | return $_[0]->{session_id}; | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | 0 | ▶ | sub shorten { | ||||||||||
|
Subroutine
shorten: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 81 | 0 | my $self = shift; | |||||||||||
| 82 | 0 | $poe_kernel->post( $self->{session_id} => 'shorten' => @_ ); | |||||||||||
| 83 | } | ||||||||||||
| 84 | |||||||||||||
| 85 | 0 | ▶ | sub _shorten { | ||||||||||
|
Subroutine
_shorten: not called CC=9 SCAR=45.0
| |||||||||||||
| 86 | 0 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||
| 87 | 0 | my $sender = $_[SENDER]->ID(); | |||||||||||
| 88 | |||||||||||||
| 89 | 0 | ▶ | return if $self->{shutdown}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | my $args; | |||||||||||
| 91 | 0 | ▶ | if ( ref( $_[ARG0] ) eq 'HASH' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | 0 | $args = { %{ $_[ARG0] } }; | |||||||||||
| 93 | } else { | ||||||||||||
| 94 | 0 | warn "first parameter must be a hashref, trying to adjust. " | |||||||||||
| 95 | ."(fix this to get rid of this message)"; | ||||||||||||
| 96 | 0 | $args = { @_[ARG0..$#_] }; | |||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | 0 | $args->{lc $_} = delete $args->{$_} for grep { $_ !~ /^_/ } keys %{ $args }; | |||||||||||
| 100 | |||||||||||||
| 101 | 0 | ▶ | unless ( $args->{event} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 0 | warn "where am i supposed to send the output?"; | |||||||||||
| 103 | 0 | return; | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 0 | ▶ | unless ( $args->{url} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 107 | 0 | warn "No 'url' specified"; | |||||||||||
| 108 | 0 | return; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | 0 | ▶ | if ( $args->{session} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 112 | 0 | ▶ | if ( my $ref = $kernel->alias_resolve( $args->{session} ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 0 | $args->{sender} = $ref->ID(); | |||||||||||
| 114 | } | ||||||||||||
| 115 | else { | ||||||||||||
| 116 | 0 | warn "Could not resolve 'session' to a valid POE session\n"; | |||||||||||
| 117 | 0 | return; | |||||||||||
| 118 | } | ||||||||||||
| 119 | } | ||||||||||||
| 120 | else { | ||||||||||||
| 121 | 0 | $args->{sender} = $sender; | |||||||||||
| 122 | } | ||||||||||||
| 123 | |||||||||||||
| 124 | 0 | ▶ | $args->{params} = $self->{params} ? $self->{params} : []; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 125 | |||||||||||||
| 126 | 0 | $kernel->refcount_increment( $args->{sender} => __PACKAGE__ ); | |||||||||||
| 127 | 0 | $self->{wheel}->put( $args ); | |||||||||||
| 128 | 0 | undef; | |||||||||||
| 129 | } | ||||||||||||
| 130 | |||||||||||||
| 131 | 1 | ▶ | sub shutdown { | ||||||||||
|
Subroutine
shutdown: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 132 | 1 | my $self = shift; | |||||||||||
| 133 | 1 | $poe_kernel->call( $self->{session_id} => 'shutdown' => @_ ); | |||||||||||
| 134 | } | ||||||||||||
| 135 | |||||||||||||
| 136 | 1 | ▶ | sub _shutdown { | ||||||||||
|
Subroutine
_shutdown: called CC=4 SCAR=14.1
| |||||||||||||
| 137 | 1 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||
| 138 | 1 | $kernel->alarm_remove_all(); | |||||||||||
| 139 | 1 | $kernel->alias_remove( $_ ) for $kernel->alias_list(); | |||||||||||
| 140 | 1 | ▶ | $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 141 | 1 | $self->{shutdown} = 1; | |||||||||||
| 142 | 1 | ▶ | $self->{wheel}->shutdown_stdin if $self->{wheel}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 1 | undef; | |||||||||||
| 144 | } | ||||||||||||
| 145 | |||||||||||||
| 146 | 0 | ▶ | sub _child_closed { | ||||||||||
|
Subroutine
_child_closed: not called CC=2 SCAR=17.9
| |||||||||||||
| 147 | 0 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||
| 148 | 0 | delete $self->{wheel}; | |||||||||||
| 149 | 0 | ▶ | $kernel->yield( 'shutdown' ) unless $self->{shutdown}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | 0 | undef; | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | 1 | ▶ | sub _child_error { | ||||||||||
|
Subroutine
_child_error: called CC=2 SCAR=7.0
| |||||||||||||
| 154 | 1 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||
| 155 | 1 | delete $self->{wheel}; | |||||||||||
| 156 | 1 | ▶ | $kernel->yield( 'shutdown' ) unless $self->{shutdown}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 157 | 1 | undef; | |||||||||||
| 158 | } | ||||||||||||
| 159 | |||||||||||||
| 160 | 0 | ▶ | sub _child_stderr { | ||||||||||
|
Subroutine
_child_stderr: not called CC=2 SCAR=17.9
| |||||||||||||
| 161 | 0 | my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0]; | |||||||||||
| 162 | 0 | ▶ | warn "$input\n" if $self->{debug}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | 0 | undef; | |||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | 0 | ▶ | sub _child_stdout { | ||||||||||
|
Subroutine
_child_stdout: not called CC=1 SCAR=6.9
| |||||||||||||
| 167 | 0 | my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0]; | |||||||||||
| 168 | 0 | my $session = delete $input->{sender}; | |||||||||||
| 169 | 0 | my $event = delete $input->{event}; | |||||||||||
| 170 | |||||||||||||
| 171 | 0 | $kernel->post( $session, $event, $input ); | |||||||||||
| 172 | 0 | $kernel->refcount_decrement( $session => __PACKAGE__ ); | |||||||||||
| 173 | 0 | undef; | |||||||||||
| 174 | } | ||||||||||||
| 175 | |||||||||||||
| 176 | 0 | ▶ | sub _shorten_wheel { | ||||||||||
|
Subroutine
_shorten_wheel: not called CC=4 SCAR=30.0
| |||||||||||||
| 177 | 0 | ▶ | if ( $^O eq 'MSWin32' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 178 | 0 | binmode(STDIN); binmode(STDOUT); | |||||||||||
| 179 | } | ||||||||||||
| 180 | 0 | my $raw; | |||||||||||
| 181 | 0 | my $size = 4096; | |||||||||||
| 182 | 0 | my $filter = POE::Filter::Reference->new(); | |||||||||||
| 183 | |||||||||||||
| 184 | 0 | ▶ | while ( sysread ( STDIN, $raw, $size ) ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 185 | 0 | my $requests = $filter->get( [ $raw ] ); | |||||||||||
| 186 | 0 | foreach my $req ( @{ $requests } ) { | |||||||||||
| 187 | 0 | $req->{short} = makeashorterlink( $req->{url}, @{$req->{params}} ); | |||||||||||
| 188 | 0 | my $response = $filter->put( [ $req ] ); | |||||||||||
| 189 | 0 | print STDOUT @$response; | |||||||||||
| 190 | } | ||||||||||||
| 191 | } | ||||||||||||
| 192 | } | ||||||||||||
| 193 | |||||||||||||
| 194 | 2 | 'snip'; | |||||||||||
| 195 | |||||||||||||
| 196 | __END__ | ||||||||||||