Skip to content

Stargazers API #321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/MetaCPAN/Document/Stargazer.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package MetaCPAN::Document::Stargazer;

use strict;
use warnings;

use Moose;
use ElasticSearchX::Model::Document;
use DateTime;
use MetaCPAN::Types qw(:all);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we using any of these Types here? They export method which we can use when setting isa for an attribute, but I don't see any isa here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having said that, I think at the very least we should be able to say isa => Str for most of these attributes. We should get in the habit of setting isa where possible, despite the fact that it's missing from a lot of existing code.


has id => (
is => 'ro',
id => [qw(user module)],
isa => 'NonEmptySimpleStr',
);

has [qw(author release user module)] => (
is => 'ro',
required => 1,
);

has date => (
is => 'ro',
required => 1,
isa => 'DateTime',
default => sub { DateTime->now },
);

has timestamp => (
is => 'ro',
timestamp => { path => 'date', store => 1 },
);

__PACKAGE__->meta->make_immutable;
30 changes: 30 additions & 0 deletions lib/MetaCPAN/Server/Controller/Stargazer.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package MetaCPAN::Server::Controller::Stargazer;

use strict;
use warnings;

use Moose;
use Try::Tiny;

BEGIN { extends 'MetaCPAN::Server::Controller' }

with 'MetaCPAN::Server::Role::JSONP';

sub find : Path('') : Args(2) {
my ( $self, $c, $user, $module ) = @_;
try {
my $stargazer = $self->model($c)->raw->get(
{
user => $user,
module => $module,
}
);
$c->stash( $stargazer->{_source} || $stargazer->{fields} );
}
catch {
$c->detach( '/not_found', [$_] );
};
}

__PACKAGE__->meta->make_immutable;
1;
25 changes: 25 additions & 0 deletions lib/MetaCPAN/Server/Controller/User/Stargazer.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package MetaCPAN::Server::Controller::User::Stargazer;

use strict;
use warnings;

use Moose;

BEGIN { extends 'Catalyst::Controller::REST' }

with 'MetaCPAN::Server::Role::Starring';

sub auto : Private {
my ( $self, $c ) = @_;
unless ( $c->user->looks_human ) {
$self->status_forbidden( $c,
message => 'please complete the turing test' );
return 0;
}
return 1;
}

sub index : Path : ActionClass('REST') {
}

1;
42 changes: 42 additions & 0 deletions lib/MetaCPAN/Server/Role/Starring.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package MetaCPAN::Server::Role::Starring;

use strict;
use warnings;

use Moose::Role;

sub index_POST {
my ( $self, $c ) = @_;
my $req = $c->req;
my $star = $c->model('CPAN::Stargazer')->put(
{
author => $req->data->{author},
module => $req->data->{module},
release => $req->data->{release},
user => $c->user->id,
},
{ refresh => 1 }
);
$self->status_created(
$c,
location => $c->uri_for(
join( q{/}, '/stargazer', $star->user, $star->module )
),
entity => $star->meta->get_data($star)
);
}

sub index_DELETE {
my ( $self, $c, $module ) = @_;
my $star = $c->model('CPAN::Stargazer')
->get( { user => $c->user->id, module => $module } );
if ($star) {
$star->delete( { refresh => 1 } );
$self->status_ok( $c, entity => $star->meta->get_data($star) );
}
else {
$self->status_not_found( $c, message => 'Entity could not be found' );
}
}

1;
66 changes: 66 additions & 0 deletions t/server/controller/user/stargazer.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use strict;
use warnings;

use MetaCPAN::Server::Test;
use Test::More;

test_psgi app, sub {
my $cb = shift;

ok( my $user = $cb->( GET '/user?access_token=testing' ), 'get user' );
is( $user->code, 200, 'code 200' );
ok( $user = decode_json( $user->content ), 'decode json' );

ok(
my $res = $cb->(
POST '/user/stargazer?access_token=testing',
Content => encode_json(
{
module => 'Moose::Cookbook',
release => 'Moose-2.1209',
author => 'DOY',
}
)
),
'POST stargazer'
);
is( $res->code, 201, 'status created' );
ok( my $location = $res->header('location'), 'location header set' );
ok( $res = $cb->( GET $location ), 'GET $location' );
is( $res->code, 200, 'found' );
my $json = decode_json( $res->content );
is( $json->{user}, $user->{id}, 'user is ' . $user->{id} );
ok(
$res = $cb->(
DELETE '/user/stargazer/Moose::Cookbook?access_token=testing'
),
'DELETE /user/stargazer/MO/Moose::Cookbook'
);
is( $res->code, 200, 'status ok' );
ok( $res = $cb->( GET '$location?access_token=testing' ),
'GET $location' );
is( $res->code, 404, 'not found' );

ok( $user = $cb->( GET '/user?access_token=bot' ), 'get bot' );
is( $user->code, 200, 'code 200' );
ok( $user = decode_json( $user->content ), 'decode json' );
ok( !$user->{looks_human}, 'user looks like a bot' );
ok(
$res = $cb->(
POST '/user/stargazer?access_token=bot',
Content => encode_json(
{
module => 'Moose::Cookbook',
release => 'Moose-2.1209',
author => 'DOY',
}
)
),
'POST stargazer'
);
ok( decode_json( $res->content ), 'decode response' );
is( $res->code, 403, 'forbidden' );

};

done_testing;