Skip to content

Commit 1daf8e1

Browse files
committed
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.
1 parent d972488 commit 1daf8e1

File tree

5 files changed

+30
-49
lines changed

5 files changed

+30
-49
lines changed

lib/MetaCPAN/Server.pm

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use CatalystX::RoleApplicator;
99
use File::Temp qw( tempdir );
1010
use Plack::Middleware::ReverseProxy;
1111
use Plack::Middleware::ServerStatus::Lite;
12+
use Ref::Util qw( is_arrayref );
1213

1314
extends 'Catalyst';
1415

@@ -124,6 +125,27 @@ sub to_app {
124125
return $app;
125126
}
126127

128+
# a controller method to read a given parameter key which will be read
129+
# from either the URL (query parameter) or from the (JSON) deserialized
130+
# request body (not both, 'body' parameters take precedence).
131+
# the returned output is an arrayref containing the parameter values.
132+
sub read_param {
133+
my ( $c, $key ) = @_;
134+
135+
my $body_data = $c->req->body_data;
136+
my $params
137+
= $body_data
138+
? $body_data->{$key}
139+
: [ $c->req->param($key) ];
140+
141+
$params = [$params] unless is_arrayref($params);
142+
143+
$c->detach( '/bad_request', ["Missing param: $key"] )
144+
unless $params and @{$params};
145+
146+
return $params;
147+
}
148+
127149
1;
128150

129151
__END__

lib/MetaCPAN/Server/Controller/Author.pm

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,7 @@ sub qsearch : Path('search') : Args(0) {
7676
# /author/by_ids?id=PAUSE_ID1&id=PAUSE_ID2...
7777
sub by_ids : Path('by_ids') : Args(0) {
7878
my ( $self, $c ) = @_;
79-
my $body_data = $c->req->body_data;
80-
my $ids
81-
= $body_data
82-
? $body_data->{id}
83-
: [ $c->req->param('id') ];
84-
$c->detach( '/bad_request', ['No ids requested'] )
85-
unless $ids and @{$ids};
86-
my $data = $self->model($c)->raw->by_ids($ids);
79+
my $data = $self->model($c)->raw->by_ids( $c->read_param('id') );
8780

8881
$data
8982
? $c->stash($data)
@@ -105,16 +98,7 @@ sub by_user : Path('by_user') : Args(1) {
10598
# /author/by_user?user=USER_ID1&user=USER_ID2...
10699
sub by_users : Path('by_user') : Args(0) {
107100
my ( $self, $c ) = @_;
108-
109-
my $body_data = $c->req->body_data;
110-
my $users
111-
= $body_data
112-
? $body_data->{user}
113-
: [ $c->req->param('user') ];
114-
$c->detach( '/bad_request', ['No users requested'] )
115-
unless $users and @{$users};
116-
117-
my $data = $self->model($c)->raw->by_user($users);
101+
my $data = $self->model($c)->raw->by_user( $c->read_param('user') );
118102

119103
$data
120104
? $c->stash($data)

lib/MetaCPAN/Server/Controller/Favorite.pm

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,9 @@ sub leaderboard : Path('leaderboard') : Args(0) {
7373

7474
sub agg_by_distributions : Path('agg_by_distributions') : Args(0) {
7575
my ( $self, $c ) = @_;
76-
my $body_data = $c->req->body_data;
77-
78-
my $distributions
79-
= $body_data
80-
? $body_data->{distribution}
81-
: [ $c->req->param('distribution') ];
82-
$c->detach( '/bad_request', ['No distributions requested'] )
83-
unless $distributions and @{$distributions};
84-
85-
my $user
86-
= $body_data
87-
? $body_data->{user}
88-
: $c->req->param('user');
89-
90-
my $data = $self->model($c)
76+
my $distributions = $c->read_param('distribution');
77+
my $user = $c->read_param('user');
78+
my $data = $self->model($c)
9179
->raw->agg_by_distributions( $distributions, $user );
9280

9381
$data

lib/MetaCPAN/Server/Controller/Permission.pm

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ sub by_module : Path('by_module') : Args(1) {
2929

3030
sub by_modules : Path('by_module') : Args(0) {
3131
my ( $self, $c ) = @_;
32-
my $modules
33-
= $c->req->body_data
34-
? $c->req->body_data->{module}
35-
: [ $c->req->param('module') ];
36-
$c->detach( '/bad_request', ['No modules requested'] )
37-
unless $modules and @{$modules};
38-
39-
my $data = $self->model($c)->raw->by_modules($modules);
32+
my $data = $self->model($c)->raw->by_modules( $c->read_param('module') );
4033

4134
$data
4235
? $c->stash($data)

lib/MetaCPAN/Server/Controller/Rating.pm

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,8 @@ with 'MetaCPAN::Server::Role::JSONP';
1111

1212
sub by_distributions : Path('by_distributions') : Args(0) {
1313
my ( $self, $c ) = @_;
14-
my $distributions
15-
= $c->req->body_data
16-
? $c->req->body_data->{distribution}
17-
: [ $c->req->param('distribution') ];
18-
$c->detach( '/bad_request', ['No distributions requested'] )
19-
unless $distributions and @{$distributions};
20-
21-
my $data = $self->model($c)->raw->by_distributions($distributions);
14+
my $data = $self->model($c)
15+
->raw->by_distributions( $c->read_param('distribution') );
2216

2317
$data
2418
? $c->stash($data)

0 commit comments

Comments
 (0)