Skip to content

Commit

Permalink
enable jsonp in most controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
monken committed Sep 11, 2011
1 parent 62a3c22 commit 59ba587
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 3 deletions.
5 changes: 4 additions & 1 deletion lib/MetaCPAN/Server/Controller.pm
Expand Up @@ -33,11 +33,14 @@ sub all : Chained('index') : PathPart('') : Args(0) {
sub search : Path('_search') : ActionClass('Deserialize') {
my ( $self, $c ) = @_;
my $req = $c->req;
# shallow copy
my $params = {%{$req->params}};
delete $params->{callback};
eval {
$c->stash(
$c->model('CPAN')->es->request(
{ method => $req->method,
qs => $req->parameters,
qs => $params,
cmd => join( '/', '', 'cpan', $self->type, '_search' ),
data => $req->data
}
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Author.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::Author;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Chained('/') : PathPart('author') : CaptureArgs(0) {
}
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Diff.pm
Expand Up @@ -2,6 +2,7 @@ package MetaCPAN::Server::Controller::Diff;
use Moose;
use MetaCPAN::Server::Diff;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Chained('/') : PathPart('diff') : CaptureArgs(0) {
}
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Favorite.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::Favorite;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Chained('/') : PathPart('favorite') : CaptureArgs(0) {
}
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/File.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::File;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Chained('/') : PathPart('file') : CaptureArgs(0) {
}
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Mirror.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::Mirror;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Chained('/') : PathPart('mirror') : CaptureArgs(0) {
}
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Pod.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::Pod;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Chained('/') : PathPart('pod') : CaptureArgs(0) {
}
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Rating.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::Rating;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Chained('/') : PathPart('rating') : CaptureArgs(0) {
}
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Release.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::Release;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Chained('/') : PathPart('release') : CaptureArgs(0) {
}
Expand Down
10 changes: 9 additions & 1 deletion lib/MetaCPAN/Server/Controller/Root.pm
Expand Up @@ -16,7 +16,15 @@ sub not_found : Private {
}

sub end : ActionClass('RenderView') {
my ($self, $c) = @_;
my ( $self, $c ) = @_;
if ( $c->controller->enable_jsonp ) {

# call default view unless body has been set
$c->forward( $c->view ) unless ( $c->res->body );
$c->forward( $c->view('JSONP') );
$c->res->content_type('text/javascript')
if ( $c->req->params->{callback} );
}
}

1;
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Scroll.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::Scroll;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';

sub index : Path('/_search/scroll') {
my ( $self, $c ) = @_;
Expand Down
1 change: 1 addition & 0 deletions lib/MetaCPAN/Server/Controller/Source.pm
@@ -1,6 +1,7 @@
package MetaCPAN::Server::Controller::Source;
use Moose;
BEGIN { extends 'MetaCPAN::Server::Controller' }
with 'MetaCPAN::Server::Role::JSONP';
use Plack::App::Directory;

sub index : Chained('/') : PathPart('source') : CaptureArgs(0) {
Expand Down
4 changes: 4 additions & 0 deletions lib/MetaCPAN/Server/Role/JSONP.pm
@@ -0,0 +1,4 @@
package MetaCPAN::Server::Role::JSONP;
use Moose::Role;
has enable_jsonp => ( is => 'ro', default => 1 );
1;
14 changes: 14 additions & 0 deletions lib/MetaCPAN/Server/View/JSONP.pm
@@ -0,0 +1,14 @@
package MetaCPAN::Server::View::JSONP;
use Moose;
extends 'Catalyst::View';

sub process {
my ($self, $c) = @_;
return 1 unless(my $cb = $c->req->params->{callback});
$c->res->body(
"$cb(" . $c->res->body . ");"
);
return 1;
}

1;
9 changes: 8 additions & 1 deletion t/server/controller/author.t
Expand Up @@ -26,7 +26,14 @@ test_psgi app, sub {
if ( $k eq '/author/_mapping' );
}

ok( my $res = $cb->(
my $res = $cb->( GET '/author/MO?callback=jsonp'), "GET jsonp";
is( $res->header('content-type'),
'text/javascript; charset=UTF-8',
'Content-type'
);
like( $res->content, qr/^jsonp\(.*\);$/ms, 'includes jsonp callback' );

ok( $res = $cb->(
POST '/author/_search',
#'Content-type' => 'application/json',
Content => '{"query":{"match_all":{}},"size":0}'
Expand Down
7 changes: 7 additions & 0 deletions t/server/controller/pod.t
Expand Up @@ -37,6 +37,13 @@ test_psgi app, sub {
);
}

ok( $res = $cb->( GET "$k?callback=foo"), "GET $k with callback" );
is( $res->code, $v, "code $v" );
is( $res->header('content-type'),
'text/javascript; charset=UTF-8',
'Content-type'
);
like($res->content, qr/^foo\(/, 'callback included');
}
};

Expand Down

0 comments on commit 59ba587

Please sign in to comment.