| 1 | package POE::Component::MetaCPAN::Recent; | ||||||||||||||||||||||||||||
| 2 | 2 | $POE::Component::MetaCPAN::Recent::VERSION = '1.06'; | |||||||||||||||||||||||||||
| 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::SmokeBox::Recent::HTTP]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use HTTP::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use JSON::PP; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Time::Piece; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use Digest::SHA qw[sha256_hex]; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | sub spawn { | ||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=5 SCAR=16.4
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 1 | my $package = shift; | |||||||||||||||||||||||||||
| 18 | 1 | my %opts = @_; | |||||||||||||||||||||||||||
| 19 | 1 | $opts{lc $_} = delete $opts{$_} for keys %opts; | |||||||||||||||||||||||||||
| 20 | 1 | ▶ | croak "$package requires an 'event' argument\n" unless $opts{event}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | $opts{delay} = 180 unless $opts{delay}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 22 | 1 | my $options = delete $opts{options}; | |||||||||||||||||||||||||||
| 23 | 1 | my $self = bless \%opts, $package; | |||||||||||||||||||||||||||
| 24 | 1 | $self->{cache} = {}; | |||||||||||||||||||||||||||
| 25 | 1 | ▶ | $self->{session_id} = POE::Session->create( | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | object_states => [ | ||||||||||||||||||||||||||||
| 27 | $self => { | ||||||||||||||||||||||||||||
| 28 | shutdown => '_shutdown', | ||||||||||||||||||||||||||||
| 29 | http_sockerr => '_get_connect_error', | ||||||||||||||||||||||||||||
| 30 | http_timeout => '_get_connect_error', | ||||||||||||||||||||||||||||
| 31 | http_response => '_handle_recent', | ||||||||||||||||||||||||||||
| 32 | }, | ||||||||||||||||||||||||||||
| 33 | $self => [ qw(_start _get_recent _real_shutdown) ], | ||||||||||||||||||||||||||||
| 34 | ], | ||||||||||||||||||||||||||||
| 35 | heap => $self, | ||||||||||||||||||||||||||||
| 36 | ( ref($options) eq 'HASH' ? ( options => $options ) : () ), | ||||||||||||||||||||||||||||
| 37 | )->ID(); | ||||||||||||||||||||||||||||
| 38 | 1 | return $self; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 1 | ▶ | sub session_id { | ||||||||||||||||||||||||||
|
Subroutine
session_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 42 | 1 | return $_[0]->{session_id}; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub shutdown { | ||||||||||||||||||||||||||
|
Subroutine
shutdown: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 47 | 0 | $poe_kernel->post( $self->{session_id}, 'shutdown' ); | |||||||||||||||||||||||||||
| 48 | 0 | return; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | sub _shutdown { | ||||||||||||||||||||||||||
|
Subroutine
_shutdown: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 52 | 1 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||||||||||||||||||
| 53 | 1 | $self->{_shutdown} = 1; | |||||||||||||||||||||||||||
| 54 | 1 | ▶ | return if $self->{_http_requests}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 1 | $kernel->yield( '_real_shutdown' ); | |||||||||||||||||||||||||||
| 56 | 1 | return; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | ▶ | sub _real_shutdown { | ||||||||||||||||||||||||||
|
Subroutine
_real_shutdown: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 60 | 1 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||||||||||||||||||
| 61 | 1 | $kernel->alarm_remove_all(); | |||||||||||||||||||||||||||
| 62 | 1 | $kernel->alias_remove( $_ ) for $kernel->alias_list(); | |||||||||||||||||||||||||||
| 63 | 1 | $kernel->refcount_decrement( $self->{sender_id}, __PACKAGE__ ); | |||||||||||||||||||||||||||
| 64 | 1 | return; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 1 | ▶ | sub _start { | ||||||||||||||||||||||||||
|
Subroutine
_start: called CC=5 SCAR=19.2
| |||||||||||||||||||||||||||||
| 68 | 1 | my ($kernel,$session,$sender,$self) = @_[KERNEL,SESSION,SENDER,OBJECT]; | |||||||||||||||||||||||||||
| 69 | 1 | $self->{session_id} = $session->ID(); | |||||||||||||||||||||||||||
| 70 | 1 | ▶ | if ( $kernel == $sender and !$self->{session} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$kernel == $sender and not $self->{"session"}
MC/DC0%0/2
$kernel == $sender and not $self->{"session"}
$kernel == $sender
not $self->{"session"}
Truth table33%1/3
A and not(B)
A= $kernel == $senderB= not $self->{"session"}
| |||||||||||||||||||||||||||||
| 71 | 0 | croak "Not called from another POE session and 'session' wasn't set\n"; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | 1 | my $sender_id; | |||||||||||||||||||||||||||
| 74 | 1 | ▶ | if ( $self->{session} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | if ( my $ref = $kernel->alias_resolve( $self->{session} ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 0 | $sender_id = $ref->ID(); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 0 | croak "Could not resolve 'session' to a valid POE session\n"; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | else { | ||||||||||||||||||||||||||||
| 83 | 1 | $sender_id = $sender->ID(); | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | 1 | $kernel->refcount_increment( $sender_id, __PACKAGE__ ); | |||||||||||||||||||||||||||
| 86 | 1 | $self->{sender_id} = $sender_id; | |||||||||||||||||||||||||||
| 87 | 1 | $self->{timestamp} = 0; | |||||||||||||||||||||||||||
| 88 | # Start requesting | ||||||||||||||||||||||||||||
| 89 | 1 | $kernel->yield('_get_recent'); | |||||||||||||||||||||||||||
| 90 | 1 | return; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub _get_recent { | ||||||||||||||||||||||||||
|
Subroutine
_get_recent: called CC=4 SCAR=15.8
| |||||||||||||||||||||||||||||
| 94 | 1 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||||||||||||||||||
| 95 | 1 | $kernel->delay('_get_recent'); | |||||||||||||||||||||||||||
| 96 | 1 | ▶ | if ( $self->{shutdown} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | $kernel->yield('_real_shutdown'); | |||||||||||||||||||||||||||
| 98 | 0 | return; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | POE::Component::SmokeBox::Recent::HTTP->spawn( | ||||||||||||||||||||||||||||
| 101 | 1 | uri => URI->new( 'http://fastapi.metacpan.org/release/recent?type=l&page=1&page_size=100' ), | |||||||||||||||||||||||||||
| 102 | ); | ||||||||||||||||||||||||||||
| 103 | 1 | my $cachetime = time() - ( 60 * 60 * 2 ); | |||||||||||||||||||||||||||
| 104 | 1 | foreach my $digest ( keys %{ $self->{cache} } ) { | |||||||||||||||||||||||||||
| 105 | 0 | ▶ | delete $self->{cache}->{$digest} if $self->{cache}->{$digest} < $cachetime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 1 | $self->{_http_requests}++; | |||||||||||||||||||||||||||
| 108 | 1 | return; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 1 | ▶ | sub _handle_recent { | ||||||||||||||||||||||||||
|
Subroutine
_handle_recent: called CC=13 SCAR=31.4
| |||||||||||||||||||||||||||||
| 112 | 1 | my ($kernel,$self,$http_resp) = @_[KERNEL,OBJECT,ARG0]; | |||||||||||||||||||||||||||
| 113 | 1 | $self->{_http_requests}--; | |||||||||||||||||||||||||||
| 114 | 1 | ▶ | if ( $http_resp and $http_resp->code() == 200 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$http_resp and $http_resp->code == 200
MC/DC0%0/2
$http_resp and $http_resp->code == 200
$http_resp
$http_resp->code == 200
Truth table33%1/3
A and B
A= $http_respB= $http_resp->code == 200
| |||||||||||||||||||||||||||||
| 115 | 1 | my $recents = eval { decode_json( $http_resp->content() ) }; | |||||||||||||||||||||||||||
| 116 | SWITCH: { | ||||||||||||||||||||||||||||
| 117 | 1 | ▶ | last SWITCH unless $recents; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 1 | ▶ | last SWITCH unless $recents->{releases}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 1 | ▶ | last SWITCH unless ref $recents->{releases} eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 1 | my @uploads; | |||||||||||||||||||||||||||
| 121 | 1 | RELEASES: foreach my $recent ( @{ $recents->{releases} } ) { | |||||||||||||||||||||||||||
| 122 | 1 | my $ts = Time::Piece->strptime($recent->{date},"%Y-%m-%dT%H:%M:%S")->epoch; | |||||||||||||||||||||||||||
| 123 | 1 | ▶ | $self->{timestamp} = $ts unless $self->{timestamp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | last RELEASES if $ts <= $self->{timestamp}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $recent->{ts} = $ts; | |||||||||||||||||||||||||||
| 126 | 0 | my $digest = sha256_hex( join '/', $recent->{author}, $recent->{name} ); | |||||||||||||||||||||||||||
| 127 | 0 | ▶ | push @uploads, $recent unless $self->{cache}->{$digest}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 0 | $self->{cache}->{$digest} = $ts; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 1 | foreach my $upload ( reverse @uploads ) { | |||||||||||||||||||||||||||
| 131 | 0 | $self->{timestamp} = delete $upload->{ts}; | |||||||||||||||||||||||||||
| 132 | 0 | $kernel->post( $self->{sender_id}, $self->{event}, $upload ); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 1 | ▶ | $kernel->yield('_real_shutdown') if $self->{shutdown}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 1 | ▶ | $kernel->delay('_get_recent', $self->{delay}) unless $self->{shutdown}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | 1 | return; | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub _get_connect_error { | ||||||||||||||||||||||||||
|
Subroutine
_get_connect_error: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 142 | 0 | my ($kernel,$self) = @_[KERNEL,OBJECT]; | |||||||||||||||||||||||||||
| 143 | 0 | $self->{_http_requests}--; | |||||||||||||||||||||||||||
| 144 | 0 | $kernel->delay('_get_recent', $self->{delay}); | |||||||||||||||||||||||||||
| 145 | 0 | return; | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 2 | "Fooby Dooby Foo Bar"; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | __END__ | ||||||||||||||||||||||||||||