Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added controller method for improving/simplifying param reading
With a growing use of the same pattern of reading parameters
from either the URL or the body, this method allows reuse and
simplification of the route handlers code.
  • Loading branch information
mickeyn committed Jul 19, 2017
1 parent d972488 commit 1daf8e1
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 49 deletions.
22 changes: 22 additions & 0 deletions lib/MetaCPAN/Server.pm
Expand Up @@ -9,6 +9,7 @@ use CatalystX::RoleApplicator;
use File::Temp qw( tempdir );
use Plack::Middleware::ReverseProxy;
use Plack::Middleware::ServerStatus::Lite;
use Ref::Util qw( is_arrayref );

extends 'Catalyst';

Expand Down Expand Up @@ -124,6 +125,27 @@ sub to_app {
return $app;
}

# a controller method to read a given parameter key which will be read
# from either the URL (query parameter) or from the (JSON) deserialized
# request body (not both, 'body' parameters take precedence).
# the returned output is an arrayref containing the parameter values.
sub read_param {
my ( $c, $key ) = @_;

my $body_data = $c->req->body_data;
my $params
= $body_data
? $body_data->{$key}
: [ $c->req->param($key) ];

$params = [$params] unless is_arrayref($params);

$c->detach( '/bad_request', ["Missing param: $key"] )
unless $params and @{$params};

return $params;
}

1;

__END__
20 changes: 2 additions & 18 deletions lib/MetaCPAN/Server/Controller/Author.pm
Expand Up @@ -76,14 +76,7 @@ sub qsearch : Path('search') : Args(0) {
# /author/by_ids?id=PAUSE_ID1&id=PAUSE_ID2...
sub by_ids : Path('by_ids') : Args(0) {
my ( $self, $c ) = @_;
my $body_data = $c->req->body_data;
my $ids
= $body_data
? $body_data->{id}
: [ $c->req->param('id') ];
$c->detach( '/bad_request', ['No ids requested'] )
unless $ids and @{$ids};
my $data = $self->model($c)->raw->by_ids($ids);
my $data = $self->model($c)->raw->by_ids( $c->read_param('id') );

$data
? $c->stash($data)
Expand All @@ -105,16 +98,7 @@ sub by_user : Path('by_user') : Args(1) {
# /author/by_user?user=USER_ID1&user=USER_ID2...
sub by_users : Path('by_user') : Args(0) {
my ( $self, $c ) = @_;

my $body_data = $c->req->body_data;
my $users
= $body_data
? $body_data->{user}
: [ $c->req->param('user') ];
$c->detach( '/bad_request', ['No users requested'] )
unless $users and @{$users};

my $data = $self->model($c)->raw->by_user($users);
my $data = $self->model($c)->raw->by_user( $c->read_param('user') );

$data
? $c->stash($data)
Expand Down
18 changes: 3 additions & 15 deletions lib/MetaCPAN/Server/Controller/Favorite.pm
Expand Up @@ -73,21 +73,9 @@ sub leaderboard : Path('leaderboard') : Args(0) {

sub agg_by_distributions : Path('agg_by_distributions') : Args(0) {
my ( $self, $c ) = @_;
my $body_data = $c->req->body_data;

my $distributions
= $body_data
? $body_data->{distribution}
: [ $c->req->param('distribution') ];
$c->detach( '/bad_request', ['No distributions requested'] )
unless $distributions and @{$distributions};

my $user
= $body_data
? $body_data->{user}
: $c->req->param('user');

my $data = $self->model($c)
my $distributions = $c->read_param('distribution');
my $user = $c->read_param('user');
my $data = $self->model($c)
->raw->agg_by_distributions( $distributions, $user );

$data
Expand Down
9 changes: 1 addition & 8 deletions lib/MetaCPAN/Server/Controller/Permission.pm
Expand Up @@ -29,14 +29,7 @@ sub by_module : Path('by_module') : Args(1) {

sub by_modules : Path('by_module') : Args(0) {
my ( $self, $c ) = @_;
my $modules
= $c->req->body_data
? $c->req->body_data->{module}
: [ $c->req->param('module') ];
$c->detach( '/bad_request', ['No modules requested'] )
unless $modules and @{$modules};

my $data = $self->model($c)->raw->by_modules($modules);
my $data = $self->model($c)->raw->by_modules( $c->read_param('module') );

$data
? $c->stash($data)
Expand Down
10 changes: 2 additions & 8 deletions lib/MetaCPAN/Server/Controller/Rating.pm
Expand Up @@ -11,14 +11,8 @@ with 'MetaCPAN::Server::Role::JSONP';

sub by_distributions : Path('by_distributions') : Args(0) {
my ( $self, $c ) = @_;
my $distributions
= $c->req->body_data
? $c->req->body_data->{distribution}
: [ $c->req->param('distribution') ];
$c->detach( '/bad_request', ['No distributions requested'] )
unless $distributions and @{$distributions};

my $data = $self->model($c)->raw->by_distributions($distributions);
my $data = $self->model($c)
->raw->by_distributions( $c->read_param('distribution') );

$data
? $c->stash($data)
Expand Down

0 comments on commit 1daf8e1

Please sign in to comment.