-
Notifications
You must be signed in to change notification settings - Fork 195
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
Closed
Stargazers API #321
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8a31b85
Adding the API Document.
Talina06 3ed3983
Stargazers controller file.
Talina06 4b64435
Controller script for User.
Talina06 a4c0bdd
Testing script for stargazers.
Talina06 e51031b
Tidy all.
Talina06 816579b
Abiding by the Perl Critic standards.
Talina06 95c8d57
Small changes to the code.
Talina06 0907256
Using Try::Tiny.
Talina06 25ef822
Use Roles to implement starring.
Talina06 845c2ab
Rename MetaCPAN::Role Script and code cleanup.
Talina06 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 anyisa
here.There was a problem hiding this comment.
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 settingisa
where possible, despite the fact that it's missing from a lot of existing code.