blib/lib/POE/Component/CPANIDX.pm

filter: Statementstmt 100% 12 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 100% 16 / 16 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::CPANIDX;
2 $POE::Component::CPANIDX::VERSION = '0.12';
3 #ABSTRACT: A POE mechanism for querying the CPANIDX
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use POE qw(Component::Client::HTTP);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use YAML::Tiny;
10 use HTTP::Request::Common;
11 use File::Spec::Unix;
12  
13 my $cmds = {
14 mod => 1,
15 dist => 1,
16 auth => 1,
17 corelist => 1,
18 dists => 1,
19 timestamp => 0,
20 topten => 0,
21 mirrors => 0,
22 };
23  
24 # Stolen from POE::Wheel. This is static data, shared by all
25 my $current_id = 0;
26 my %active_identifiers;
27  
28 sub _allocate_identifier {
29 while (1) {
30 last unless exists $active_identifiers{ ++$current_id };
31 }
32 return $active_identifiers{$current_id} = $current_id;
33 }
34  
35 sub _free_identifier {
36 my $id = shift;
37 delete $active_identifiers{$id};
38 }
39  
40 sub spawn {
41 my $package = shift;
42 my %opts = @_;
43 $opts{lc $_} = delete $opts{$_} for keys %opts;
44 my $options = delete $opts{options};
45 my $self = bless \%opts, $package;
46 $self->{session_id} = POE::Session->create(
47 object_states => [
48 $self => { shutdown => '_shutdown',
49 query_idx => '_query_idx',
50 },
51 $self => [ qw(_start _query_idx _dispatch _http_request _http_response) ],
52 ],
53 heap => $self,
54 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
55 )->ID();
56 return $self;
57 }
58  
59 sub session_id {
60 return $_[0]->{session_id};
61 }
62  
63 sub shutdown {
64 my $self = shift;
65 $poe_kernel->call( $self->{session_id}, 'shutdown' );
66 }
67  
68 sub _start {
69 my ($kernel,$self) = @_[KERNEL,OBJECT];
70 $self->{session_id} = $_[SESSION]->ID();
71 if ( $self->{alias} ) {
72 $kernel->alias_set( $self->{alias} );
73 }
74 else {
75 $kernel->refcount_increment( $self->{session_id} => __PACKAGE__ );
76 }
77 $self->{_httpc} = 'httpc-' . $self->{session_id};
78 POE::Component::Client::HTTP->spawn(
79 Alias => $self->{_httpc},
80 FollowRedirects => 2,
81 );
82 return;
83 }
84  
85 sub _shutdown {
86 my ($kernel,$self) = @_[KERNEL,OBJECT];
87 $kernel->alias_remove( $_ ) for $kernel->alias_list();
88 $kernel->refcount_decrement( $self->{session_id} => __PACKAGE__ ) unless $self->{alias};
89 $self->{_shutdown} = 1;
90 $kernel->post( $self->{_httpc}, 'shutdown' );
91 undef;
92 }
93  
94 sub query_idx {
95 my $self = shift;
96 $poe_kernel->post( $self->{session_id}, '_query_idx', @_ );
97 }
98  
99 sub _query_idx {
100 my ($kernel,$self,$state) = @_[KERNEL,OBJECT,STATE];
101 my $sender = $_[SENDER]->ID();
102 return if $self->{_shutdown};
103 my $args;
104 if ( ref( $_[ARG0] ) eq 'HASH' ) {
105 $args = { %{ $_[ARG0] } };
106 } else {
107 $args = { @_[ARG0..$#_] };
108 }
109  
110 $args->{lc $_} = delete $args->{$_} for grep { $_ !~ /^_/ } keys %{ $args };
111  
112 unless ( $args->{event} ) {
113 warn "No 'event' specified for $state";
114 return;
115 }
116  
117 croak
118 "You must provide a valid 'url' of a CPANIDX site"
119 unless $args->{url} and URI->new($args->{url}) and URI->new($args->{url})->scheme eq 'http';
120  
121 $args->{cmd} = 'timestamp' unless $args->{cmd};
122 $args->{cmd} = lc $args->{cmd};
123  
124 my $arg = $cmds->{ $args->{cmd} };
125  
126 croak
127 "'cmd' that was specified is unknown"
128 unless defined $arg;
129  
130 croak
131 "'cmd' requires that you specify a 'search' term"
132 if $arg and !$args->{search};
133  
134 $args->{sender} = $sender;
135 $kernel->refcount_increment( $sender => __PACKAGE__ );
136 $kernel->yield( '_http_request', $args );
137  
138 return;
139 }
140  
141 sub _http_request {
142 my ($kernel,$self,$req) = @_[KERNEL,OBJECT,ARG0];
143 my $url = URI->new( $req->{url} );
144  
145 $url->path( File::Spec::Unix->catfile( $url->path, 'yaml', $req->{cmd}, ( $req->{search} ? $req->{search} : () ) ) );
146  
147 my $id = _allocate_identifier();
148  
149 $kernel->post(
150 $self->{_httpc},
151 'request',
152 '_http_response',
153 GET( $url->as_string ),
154 "$id",
155 );
156  
157 $self->{_requests}->{ $id } = $req;
158 return;
159 }
160  
161 sub _http_response {
162 my ($kernel,$self,$request_packet,$response_packet) = @_[KERNEL,OBJECT,ARG0,ARG1];
163 my $id = $request_packet->[1];
164 my $req = delete $self->{_requests}->{ $id };
165 _free_identifier( $id );
166 my $resp = $response_packet->[0];
167 if ( $resp->is_success ) {
168 my $data;
169 eval { $data = YAML::Tiny::Load( $resp->content ); };
170 unless ( $data ) {
171 $req->{error} = 'No valid YAML data was found';
172 $kernel->yield( '_dispatch', $req );
173 return;
174 }
175 $req->{data} = $data;
176 }
177 else {
178 $req->{error} = $resp->as_string;
179 }
180  
181 $kernel->yield( '_dispatch', $req );
182 return;
183 }
184  
185 sub _dispatch {
186 my ($kernel,$self,$input) = @_[KERNEL,OBJECT,ARG0];
187 my $session = delete $input->{sender};
188 my $event = delete $input->{event};
189 $kernel->post( $session, $event, $input );
190 $kernel->refcount_decrement( $session => __PACKAGE__ );
191 return;
192 }
193  
194 qq[CAPTCH!];
195  
196 __END__
↑ Summary