forked from metacpan/metacpan-web
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move duplicate release-info api calls to role
- Loading branch information
Showing
3 changed files
with
107 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package MetaCPAN::Web::Role::ReleaseInfo; | ||
|
||
use Moose::Role; | ||
|
||
# TODO: are there other controllers that do (or should) include this? | ||
|
||
# TODO: should some of this be in a separate (instantiable) model | ||
# so you don't have to keep passing $data? | ||
# then wouldn't have to pass favorites back in. | ||
# Role/API/Aggregator?, Model/APIAggregator/ReleaseInfo? | ||
|
||
# add favorites and myfavorite data into $main hash | ||
sub add_favorites_data { | ||
my ( $self, $main, $favorites, $data ) = @_; | ||
$main->{myfavorite} = $favorites->{myfavorites}->{ $data->{distribution} }; | ||
$main->{favorites} = $favorites->{favorites}->{ $data->{distribution} }; | ||
return; | ||
} | ||
|
||
# TODO: should the api_requests be in the base controller role, | ||
# and then the default extras be defined in other roles? | ||
|
||
# pass in any api request condvars and combine them with these defaults | ||
sub api_requests { | ||
my ( $self, $c, $reqs, $data ) = @_; | ||
|
||
return { | ||
author => $c->model('API::Author')->get( $data->{author} ), | ||
|
||
favorites => $c->model('API::Favorite') | ||
->get( $c->user_exists ? $c->user->id : undef, $data->{distribution} ), | ||
|
||
rating => $c->model('API::Rating')->get( $data->{distribution} ), | ||
|
||
versions => $c->model('API::Release')->versions( $data->{distribution} ), | ||
|
||
%$reqs, | ||
}; | ||
} | ||
|
||
# organize the api results into simple variables for the template | ||
sub stash_api_results { | ||
my ( $self, $c, $reqs, $data ) = @_; | ||
|
||
$c->stash({ | ||
author => $reqs->{author}, | ||
#release => $release->{hits}->{hits}->[0]->{_source}, | ||
rating => $reqs->{rating}->{ratings}->{ $data->{distribution} }, | ||
versions => | ||
[ map { $_->{fields} } @{ $reqs->{versions}->{hits}->{hits} } ], | ||
}); | ||
} | ||
|
||
# call recv() on all values in the provided hashref | ||
sub recv_all { | ||
my ( $self, $condvars ) = @_; | ||
|
||
my @keys = keys %$condvars; | ||
|
||
my $recv = $condvars->{ $keys[0] }; | ||
$recv &= $condvars->{ $_ } | ||
for @keys[ 1 .. $#keys ]; | ||
|
||
# don't overwrite \%condars, seems like that should be controller's choice | ||
my %results; | ||
@results{ @keys } = $recv->recv; | ||
|
||
return \%results; | ||
}; | ||
|
||
1; |