Permalink
Browse files

Add a distribution document

  • Loading branch information...
1 parent e88b5e3 commit 5fe8ebb2ba5fe1fd9487766af1916702db7a51c8 @rafl rafl committed with oalders Jan 15, 2012
@@ -0,0 +1,12 @@
+package MetaCPAN::Document::Distribution;
+
+use Moose;
+use ElasticSearchX::Model::Document;
+use namespace::autoclean;
+
+has id => (is => 'ro', id => ['name']);
+has name => (is => 'ro', required => 1);
+
+__PACKAGE__->meta->make_immutable;
+
+1;
@@ -224,6 +224,7 @@ sub import_tarball {
|| $release->{abstract} eq 'null' );
$release = $cpan->type('release')->put( $release, { refresh => 1 } );
+ my $dist = $cpan->type('distribution')->put({ name => $d->dist }, { refresh => 1 });
my @files;
my @list = $at->files;
@@ -0,0 +1,25 @@
+package MetaCPAN::Server::Controller::Distribution;
+
+use Moose;
+use namespace::autoclean;
+
+BEGIN { extends 'MetaCPAN::Server::Controller' }
+with 'MetaCPAN::Server::Role::JSONP';
+
+sub index : Chained('/') : PathPart('distribution') : CaptureArgs(0) {
+}
+
+sub get : Chained('index') : PathPart('') : Args(1) {
+ my ( $self, $c, $name ) = @_;
+ eval {
+ $c->stash(
+ $c->model('CPAN::Distribution')->inflate(0)->get({
+ name => $name,
+ })->{_source},
+ );
+ } or $c->detach('/not_found');
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
@@ -0,0 +1,32 @@
+use strict;
+use warnings;
+use Test::More;
+use MetaCPAN::Server::Test;
+
+my @tests = (
+ [ '/distribution' => 200 ],
+ [ '/distribution/Moose' => 200 ],
+ [ '/distribution/DOESNEXIST' => 404 ],
+);
+
+test_psgi app, sub {
+ my $cb = shift;
+ for my $test (@tests) {
+ my ($k, $v) = @{ $test };
+ ok( my $res = $cb->( GET $k), "GET $k" );
+ is( $res->code, $v, "code $v" );
+ is( $res->header('content-type'),
+ 'application/json; charset=utf-8',
+ 'Content-type'
+ );
+ ok( my $json = eval { decode_json( $res->content ) }, 'valid json' );
+ if ( $k eq '/distribution' ) {
+ ok( $json->{hits}->{total}, 'got total count' );
+ }
+ elsif ( $v eq 200 ) {
+ ok( $json->{name} eq 'Moose', 'Moose' );
+ }
+ }
+};
+
+done_testing;

0 comments on commit 5fe8ebb

Please sign in to comment.