| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Meta CPAN API - ~/lib/Net/API/CPAN/Activity.pm | ||
| 3 | ## Version v0.1.0 | ||
| 4 | ## Copyright(c) 2023 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2023/09/12 | ||
| 7 | ## Modified 2023/09/12 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## | ||
| 11 | ## This program is free software; you can redistribute it and/or modify it | ||
| 12 | ## under the same terms as Perl itself. | ||
| 13 | ##---------------------------------------------------------------------------- | ||
| 14 | package Net::API::CPAN::Activity; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | use parent qw( Net::API::CPAN::Generic ); | ||
| 20 | use vars qw( $VERSION ); | ||
| 21 | use DateTime; | ||
| 22 | our $VERSION = 'v0.1.0'; | ||
| 23 | }; | ||
| 24 | |||
| 25 | use strict; | ||
| 26 | use warnings; | ||
| 27 | |||
| 28 | sub init | ||
| 29 | { | ||
| 30 | my $self = shift( @_ ); | ||
| 31 | $self->{activity} = [] unless( exists( $self->{activity} ) ); | ||
| 32 | $self->{_init_strict_use_sub} = 1; | ||
| 33 | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||
| 34 | return( $self ); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub activity | ||
| 38 | { | ||
| 39 | my $self = shift( @_ ); | ||
| 40 | if( @_ ) | ||
| 41 | { | ||
| 42 | my $data = shift( @_ ); | ||
| 43 | my $months; | ||
| 44 | if( $self->_is_array( $data ) ) | ||
| 45 | { | ||
| 46 | $months = $data; | ||
| 47 | } | ||
| 48 | elsif( ref( $data ) eq 'HASH' && | ||
| 49 | exists( $data->{activity} ) && | ||
| 50 | $self->_is_array( $data->{activity} ) ) | ||
| 51 | { | ||
| 52 | $months = $data->{activity}; | ||
| 53 | } | ||
| 54 | |||
| 55 | if( !defined( $months ) ) | ||
| 56 | { | ||
| 57 | return( $self->error( "No data was provided. I was expecting either an array of integers, or an hash with a 'activity' property pointing to that array of integers." ) ); | ||
| 58 | } | ||
| 59 | elsif( scalar( @$months ) != 24 ) | ||
| 60 | { | ||
| 61 | warn( "The data provided contains ", scalar( @$months ), " elements versus the 24 that were expected." ) if( $self->_is_warnings_enabled( 'Net::API::CPAN' ) ); | ||
| 62 | } | ||
| 63 | # A copy from MetaCPAN::Query::Release->activity() | ||
| 64 | # <https://github.com/metacpan/metacpan-api/blob/db7c3a90925ec85e6ae6a9f6dd64677305feac8d/lib/MetaCPAN/Query/Release.pm#L303> | ||
| 65 | my $start = DateTime->now->truncate( to => 'month' )->subtract( months => 23 ); | ||
| 66 | # from the furthest to most recent month | ||
| 67 | # 0 being the start, and 23 being our current month | ||
| 68 | my $a = $self->new_array; | ||
| 69 | my $hash = $self->new_hash; | ||
| 70 | # Allow reference as hash keys | ||
| 71 | $hash->key_object(1); | ||
| 72 | for( 0..23 ) | ||
| 73 | { | ||
| 74 | my $dt = $start->clone->add( months => $_ ); | ||
| 75 | $a->push({ dt => $dt, value => $months->[$_] }); | ||
| 76 | $hash->{ $dt } = $months->[$_]; | ||
| 77 | } | ||
| 78 | $self->{activity} = $a; | ||
| 79 | $self->{activities} = $hash; | ||
| 80 | } | ||
| 81 | return( $self->_set_get_array_as_object( 'activity' ) ); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub activities { return( shift->_set_get_hash_as_mix_object( 'activities', @_ ) ); } | ||
| 85 | |||
| 86 | 1; | ||
| 87 | # NOTE: POD | ||
| 88 | __END__ | ||
| 89 | |||
| 90 | =encoding utf-8 | ||
| 91 | |||
| 92 | =head1 NAME | ||
| 93 | |||
| 94 | Net::API::CPAN::Activity - Meta CPAN API | ||
| 95 | |||
| 96 | =head1 SYNOPSIS | ||
| 97 | |||
| 98 | use Net::API::CPAN::Activity; | ||
| 99 | my $obj = Net::API::CPAN::Activity->new( | ||
| 100 | activity => [8, 6, 6, 8, 9, 3, 7, 15, 4, 7, 4, 7, 13, 3, 1, 1, 4, 1, 1, 2, 4, 3, 4, 1] | ||
| 101 | ) || die( Net::API::CPAN::Activity->error, "\n" ); | ||
| 102 | |||
| 103 | =head1 VERSION | ||
| 104 | |||
| 105 | v0.1.0 | ||
| 106 | |||
| 107 | =head1 DESCRIPTION | ||
| 108 | |||
| 109 | This class represent a release activity. | ||
| 110 | |||
| 111 | =head1 CONSTRUCTOR | ||
| 112 | |||
| 113 | =head2 new | ||
| 114 | |||
| 115 | This instantiates a new C<Net::API::CPAN::Activity> object and returns it. It takes the following arguments: | ||
| 116 | |||
| 117 | =over 4 | ||
| 118 | |||
| 119 | =item * C<activity> | ||
| 120 | |||
| 121 | An array reference of 24 integer. | ||
| 122 | |||
| 123 | Upon providing those data, this will create an hash of 24 L<DateTime> objects representing each of the month from 23 months ago until the current month. This hash can be accessed with L<activities|/activities> | ||
| 124 | |||
| 125 | =back | ||
| 126 | |||
| 127 | =head1 METHODS | ||
| 128 | |||
| 129 | =head2 activity | ||
| 130 | |||
| 131 | $obj->activity( [8, 6, 6, 8, 9, 3, 7, 15, 4, 7, 4, 7, 13, 3, 1, 1, 4, 1, 1, 2, 4, 3, 4, 1] ) || | ||
| 132 | die( $obj->error ); | ||
| 133 | my $array = $obj->activity; | ||
| 134 | |||
| 135 | As a mutator, this takes an array reference of 24 integers, each representing the aggregate number of release for the interval that was specified when making the API query. | ||
| 136 | |||
| 137 | Upon setting those data, this will create an hash of 24 L<DateTime> objects representing each of the month from 23 months ago until the current month. This hash can be accessed with L<activities|/activities> | ||
| 138 | |||
| 139 | It returns an L<array object|Module::Generic::Array> of hash references having the keys C<dt> for the L<DateTime> object and C<value> for the integer representing the aggregate value. | ||
| 140 | |||
| 141 | my $array = $obj->activity; | ||
| 142 | foreach my $hash ( @$array ) | ||
| 143 | { | ||
| 144 | say "Date: ", $hash->{dt}, ", value: ", $hash->{value}; | ||
| 145 | } | ||
| 146 | |||
| 147 | =head2 activities | ||
| 148 | |||
| 149 | Sets or get the hash or key-value pairs of L<DateTime> object to aggregate value. | ||
| 150 | |||
| 151 | You could do then something like: | ||
| 152 | |||
| 153 | my $ref = $obj->activities; | ||
| 154 | foreach my $dt ( sort( keys( %$ref ) ) ) | ||
| 155 | { | ||
| 156 | say $dt, ": ", $ref->{ $dt }; | ||
| 157 | } | ||
| 158 | |||
| 159 | =head1 AUTHOR | ||
| 160 | |||
| 161 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 162 | |||
| 163 | =head1 SEE ALSO | ||
| 164 | |||
| 165 | L<Net::API::CPAN>, L<Net::API::CPAN::Author>, L<Net::API::CPAN::Changes>, L<Net::API::CPAN::Changes::Release>, L<Net::API::CPAN::Contributor>, L<Net::API::CPAN::Cover>, L<Net::API::CPAN::Diff>, L<Net::API::CPAN::Distribution>, L<Net::API::CPAN::DownloadUrl>, L<Net::API::CPAN::Favorite>, L<Net::API::CPAN::File>, L<Net::API::CPAN::Module>, L<Net::API::CPAN::Package>, L<Net::API::CPAN::Permission>, L<Net::API::CPAN::Rating>, L<Net::API::CPAN::Release> | ||
| 166 | |||
| 167 | =head1 COPYRIGHT & LICENSE | ||
| 168 | |||
| 169 | Copyright(c) 2023 DEGUEST Pte. Ltd. | ||
| 170 | |||
| 171 | All rights reserved | ||
| 172 | |||
| 173 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 174 | |||
| 175 | =cut |