Skip to content

Commit

Permalink
Adding data to the API: part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Talina06 committed Jul 17, 2014
1 parent 079ffc2 commit 680c0fd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
32 changes: 32 additions & 0 deletions lib/MetaCPAN/Web/Controller/Account/Trust.pm
@@ -0,0 +1,32 @@
package MetaCPAN::Web::Controller::Account::Trust;

use Moose;
BEGIN { extends 'MetaCPAN::Web::Controller' }

sub add : Local {
my ( $self, $c ) = @_;
$c->detach('/forbidden') unless ( $c->req->method eq 'POST' );
my $model = $c->model('API::User');
my $data = $c->req->params;

This comment has been minimized.

Copy link
@oalders

oalders Jul 18, 2014

Member

my $data is actually not a particularly descriptive name as that could apply to any variable. By looking at the name, I don't get any deeper understanding of what the variable does. Having said that, we don't need this variable. Rather than passing a HashRef of all params to the model, we should only pass the fields which are required. This will make it easier to understand what is going on.

my $res;
if ( $data->{remove} ) {
$res = $model->remove_from_trustlist( $data, $c->token )->recv;

This comment has been minimized.

Copy link
@oalders

oalders Jul 18, 2014

Member

trustlist isn't a real word, so it's not so helpful as part of a method name. How about something like remove_from_trusted_authors()?

Or, even better might be just $model->trust() and $model->stop_trusting()

@rwstauner, what do you think?

}
else {
$res = $model->add_to_trustlist( $data, $c->token )->recv;
}
if ( $c->req->looks_like_browser ) {
$c->res->redirect(
$res->{error}
? $c->req->referer
: $c->uri_for('/account/turing/index')
);
}

This comment has been minimized.

Copy link
@oalders

oalders Jul 18, 2014

Member

I've seen this particular bit of code copy/pasted so many times that I really think we need to refactor some of this into its own method.

else {
$c->stash( { success => $res->{error} ? \0 : \1 } );
$c->res->code(400) if ( $res->{error} );
$c->detach( $c->view('JSON') );
}
}

__PACKAGE__->meta->make_immutable;
11 changes: 11 additions & 0 deletions lib/MetaCPAN/Web/Model/API/User.pm
Expand Up @@ -37,6 +37,17 @@ sub remove_favorite {
undef, { method => 'DELETE', token => $token } );
}

sub add_to_trustlist {
my ( $self, $data, $token ) = @_;
$self->request( '/user/trust/', $data, { token => $token } );
}

sub remove_from_trustlist {
my ( $self, $data, $token ) = @_;

This comment has been minimized.

Copy link
@oalders

oalders Jul 18, 2014

Member

Based on my comment above, this should become my ( $self, $module, $token );

$self->request( '/user/trust/' . $data->{module},
undef, { method => 'DELETE', token => $token } );
}

This comment has been minimized.

Copy link
@oalders

oalders Jul 18, 2014

Member

This will become:

$self->request( '/user/trust/' . $module, undef, { method => 'DELETE', token => $token } );

sub turing {
my ( $self, $challenge, $answer, $token ) = @_;
$self->request(
Expand Down
2 changes: 1 addition & 1 deletion root/inc/account-bar.html
Expand Up @@ -20,7 +20,7 @@
<li<% IF req.action == 'account/favorite/list' %> class="active"<% END %>>
<a href="/account/favorite/list">Favorites</a>
</li>
<li>
<li<% IF req.action == 'account/trust/list' %> class="active"<% END %>>
<a href="/account/trust">Trust List</a>
</li>
<li>
Expand Down
40 changes: 40 additions & 0 deletions root/static/js/cpan.js
Expand Up @@ -329,3 +329,43 @@ function favDistribution(form) {
return false;
}



function trustAuthor(form) {
form = $(form);
var data = form.serialize();
$.ajax({
type: 'POST',
url: form.attr('action'),
data: data,
success: function () {
var button = form.find('button');
button.toggleClass('active');
var count = 0;
if (button.hasClass('active')) {
count = 1;
button.text("Remove from List");
form.append('<input type="hidden" name="remove" value="1">');
if (!count){
button.toggleClass('highlight');
button.text("Add to List");
}
} else {
count=0;
form.find('input[name="remove"]').remove();
if (count === 0) {
button.toggleClass('highlight');
button.text("Add to List");
}
}
},
error: function () {
if (confirm("You have to complete a Captcha in order to add the author to the Trusted List.")) {
document.location.href = "/account/turing";
}
}
});
return false;
}


0 comments on commit 680c0fd

Please sign in to comment.