blib/lib/POE/Component/MetaCPAN/Recent.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 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package POE::Component::MetaCPAN::Recent;
2 $POE::Component::MetaCPAN::Recent::VERSION = '1.00';
3 #ABSTRACT: Obtain uploaded CPAN dists via MetaCPAN.
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use POE qw[Component::Client::HTTP];
Subroutine BEGIN: called CC=1 SCAR=0.0
9 use HTTP::Request;
10 use HTTP::Response;
11 use JSON::PP;
12 use Time::Piece;
13  
14 sub spawn {
15 my $package = shift;
16 my %opts = @_;
17 $opts{lc $_} = delete $opts{$_} for keys %opts;
18 croak "$package requires an 'event' argument\n" unless $opts{event};
19 $opts{delay} = 180 unless $opts{delay};
20 my $options = delete $opts{options};
21 my $self = bless \%opts, $package;
22 $self->{session_id} = POE::Session->create(
23 object_states => [
24 $self => { shutdown => '_shutdown', },
25 $self => [ qw(_start _get_recent _handle_recent _real_shutdown) ],
26 ],
27 heap => $self,
28 ( ref($options) eq 'HASH' ? ( options => $options ) : () ),
29 )->ID();
30 return $self;
31 }
32  
33 sub session_id {
34 return $_[0]->{session_id};
35 }
36  
37 sub shutdown {
38 my $self = shift;
39 $poe_kernel->post( $self->{session_id}, 'shutdown' );
40 return;
41 }
42  
43 sub _shutdown {
44 my ($kernel,$self) = @_[KERNEL,OBJECT];
45 $self->{_shutdown} = 1;
46 return if $self->{_http_requests};
47 $kernel->yield( '_real_shutdown' );
48 return;
49 }
50  
51 sub _real_shutdown {
52 my ($kernel,$self) = @_[KERNEL,OBJECT];
53 $kernel->alarm_remove_all();
54 $kernel->alias_remove( $_ ) for $kernel->alias_list();
55 $kernel->refcount_decrement( $self->{session_id}, __PACKAGE__ ) unless $self->{alias};
56 $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ );
57 $kernel->post( $self->{http_id}, 'shutdown' ) unless $self->{http_alias};
58 return;
59 }
60  
61 sub _start {
62 my ($kernel,$session,$sender,$self) = @_[KERNEL,SESSION,SENDER,OBJECT];
63 $self->{session_id} = $session->ID();
64 if ( $kernel == $sender and !$self->{session} ) {
65 croak "Not called from another POE session and 'session' wasn't set\n";
66 }
67 my $sender_id;
68 if ( $self->{session} ) {
69 if ( my $ref = $kernel->alias_resolve( $self->{session} ) ) {
70 $sender_id = $ref->ID();
71 }
72 else {
73 croak "Could not resolve 'session' to a valid POE session\n";
74 }
75 }
76 else {
77 $sender_id = $sender->ID();
78 }
79 $kernel->refcount_increment( $sender_id, __PACKAGE__ );
80 $self->{sender_id} = $sender_id;
81 if ( $self->{http_alias} ) {
82 my $http_ref = $kernel->alias_resolve( $self->{http_alias} );
83 $self->{http_id} = $http_ref->ID() if $http_ref;
84 }
85 unless ( $self->{http_id} ) {
86 $self->{http_id} = 'metacpanr' . $$ . $self->{session_id};
87 POE::Component::Client::HTTP->spawn(
88 Alias => $self->{http_id},
89 FollowRedirects => 2,
90 Timeout => 60,
91 Agent => 'Mozilla/5.0 (X11; U; Linux i686; en-US; '
92 . 'rv:1.1) Gecko/20020913 Debian/1.1-1',
93 );
94 }
95 $self->{timestamp} = time();
96 # Start requesting
97 $kernel->yield('_get_recent');
98 return;
99 }
100  
101 sub _get_recent {
102 my ($kernel,$self) = @_[KERNEL,OBJECT];
103 $kernel->delay('_get_recent');
104 $kernel->post(
105 $self->{http_id},
106 'request',
107 '_handle_recent',
108 HTTP::Request->new( GET => 'http://fastapi.metacpan.org/release/recent?type=l&page=1&page_size=100' ),
109 );
110 $self->{_http_requests}++;
111 return;
112 }
113  
114 sub _handle_recent {
115 my ($kernel,$self,$req,$res) = @_[KERNEL,OBJECT,ARG0,ARG1];
116 $self->{_http_requests}--;
117 my $http_resp = $res->[0];
118 if ( $http_resp and $http_resp->code() == 200 ) {
119 my $recents = eval { decode_json( $http_resp->content() ) };
120 SWITCH: {
121 last SWITCH unless $recents;
122 last SWITCH unless $recents->{releases};
123 last SWITCH unless ref $recents->{releases} eq 'ARRAY';
124 foreach my $recent ( @{ $recents->{releases} } ) {
125 my $ts = Time::Piece->strptime($recent->{date},"%Y-%m-%dT%H:%M:%S")->epoch;
126 last SWITCH if $ts < $self->{timestamp};
127 $kernel->post( $self->{sender_id}, $self->{event}, $recent );
128 }
129 }
130 }
131 $self->{timestamp} = time();
132 $kernel->delay('_get_recent', $self->{delay});
133 return;
134 }
135  
136 "Fooby Dooby Foo Bar";
137  
138 __END__
↑ Summary