Skip to content

Commit

Permalink
Get rid of the JSON decoding hack in the controller.
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar committed May 2, 2016
1 parent f953858 commit 43b8300
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 27 deletions.
1 change: 1 addition & 0 deletions cpanfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ requires 'Catalyst::Model::Adaptor';
requires 'Catalyst::Plugin::ConfigLoader';
requires 'Catalyst::Runtime';
requires 'Catalyst::View::JSON';
requires 'CatalystX::RoleApplicator';
requires 'Config::General';
requires 'DBM::Deep';
requires 'Plack::Middleware::CrossOrigin';
Expand Down
45 changes: 45 additions & 0 deletions cpanfile.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,16 @@ DISTRIBUTIONS
Test::More 0
YAML 0
perl 5.008001
CatalystX-RoleApplicator-0.005
pathname: H/HD/HDP/CatalystX-RoleApplicator-0.005.tar.gz
provides:
CatalystX::RoleApplicator 0.005
requirements:
Catalyst::Runtime 5.7
Class::MOP 0.80
ExtUtils::MakeMaker 0
Moose 0.73
MooseX::RelatedClassRoles 0.003
Class-Accessor-0.34
pathname: K/KA/KASEI/Class-Accessor-0.34.tar.gz
provides:
Expand Down Expand Up @@ -571,6 +581,13 @@ DISTRIBUTIONS
ExtUtils::MakeMaker 0
Sub::Exporter::Progressive 0.001011
perl 5.006
Devel-Hide-0.0009
pathname: F/FE/FERREIRA/Devel-Hide-0.0009.tar.gz
provides:
Devel::Hide 0.0009
requirements:
ExtUtils::MakeMaker 0
Test::More 0
Devel-OverloadInfo-0.004
pathname: I/IL/ILMARI/Devel-OverloadInfo-0.004.tar.gz
provides:
Expand Down Expand Up @@ -1730,6 +1747,15 @@ DISTRIBUTIONS
perl 5.006
strict 0
warnings 0
MooseX-RelatedClassRoles-0.004
pathname: H/HD/HDP/MooseX-RelatedClassRoles-0.004.tar.gz
provides:
MooseX::RelatedClassRoles 0.004
requirements:
Class::MOP 0.80
ExtUtils::MakeMaker 0
Moose 0.73
MooseX::Role::Parameterized 0.04
MooseX-Role-Parameterized-1.08
pathname: E/ET/ETHER/MooseX-Role-Parameterized-1.08.tar.gz
provides:
Expand Down Expand Up @@ -2368,6 +2394,25 @@ DISTRIBUTIONS
File::Temp 0.12
Scalar::Util 0
Test::LeakTrace 0
Term-Size-Any-0.002
pathname: F/FE/FERREIRA/Term-Size-Any-0.002.tar.gz
provides:
Term::Size::Any 0.002
requirements:
Devel::Hide 0
ExtUtils::MakeMaker 0
Module::Load::Conditional 0
Term::Size::Perl 0
Test::More 0
Term-Size-Perl-0.029
pathname: F/FE/FERREIRA/Term-Size-Perl-0.029.tar.gz
provides:
Term::Size::Perl 0.029
requirements:
Exporter 0
ExtUtils::CBuilder 0
ExtUtils::MakeMaker 0
Test::More 0
Test-Deep-1.120
pathname: R/RJ/RJBS/Test-Deep-1.120.tar.gz
provides:
Expand Down
4 changes: 3 additions & 1 deletion lib/Todo/Backend.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ use Moose;
use namespace::autoclean;

use Catalyst::Runtime 5.80;

use Catalyst qw(
ConfigLoader
);
use CatalystX::RoleApplicator;

extends 'Catalyst';

our $VERSION = '0.01';

__PACKAGE__->apply_request_class_roles( qw(Todo::Backend::TraitFor::Request::JSON) );

# Configure the application.
#
# Note that settings in todo_backend.conf (or other external
Expand Down
27 changes: 2 additions & 25 deletions lib/Todo/Backend/Controller/Root.pm
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package Todo::Backend::Controller::Root;

use Moose;
use JSON::MaybeXS;
use HTTP::Status qw(HTTP_NOT_FOUND HTTP_CREATED HTTP_NO_CONTENT);
use namespace::autoclean;

Expand All @@ -13,22 +12,6 @@ BEGIN { extends 'Catalyst::Controller' }
#
__PACKAGE__->config( namespace => '' );

=head2 json
=cut

has json => (
is => 'ro',
lazy => 1,
builder => '_build_json',
);

sub _build_json {
my $self = shift;

return JSON::MaybeXS->new( utf8 => 1 );
}

=encoding utf-8
=head1 NAME
Expand All @@ -45,12 +28,6 @@ Todo::Backend::Controller::Root - Root Controller for Todo::Backend

sub base : Chained('/') PathPart('') CaptureArgs(0) {
my ( $self, $c ) = @_;

## quite an ugly hack to get JSON decoder to work for PATCH
## unfortunately body_data accessor only works for POST and PUT methods
if ( $c->request->body ) {
$c->stash->{params} = $c->req->data_handlers->{'application/json'}->( $c->request->body, $c->req );
}
}

=head2 list_items
Expand Down Expand Up @@ -103,7 +80,7 @@ Create a new todo item.
sub create_item : Chained('base') PathPart('') Args(0) POST {
my ( $self, $c ) = @_;

$c->stash->{json} = $c->model->add( $c->stash->{params} );
$c->stash->{json} = $c->model->add( $c->req->json );
$c->res->status( HTTP_CREATED );
}

Expand All @@ -129,7 +106,7 @@ Modify todo item.
sub edit_item : Chained('base') PathPart('') Args(1) PATCH {
my ( $self, $c, $item_id ) = @_;

$c->stash->{json} = $c->model->edit( $item_id, $c->stash->{params} );
$c->stash->{json} = $c->model->edit( $item_id, $c->req->json );
}

=head2 end
Expand Down
31 changes: 31 additions & 0 deletions lib/Todo/Backend/TraitFor/Request/JSON.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Todo::Backend::TraitFor::Request::JSON;

use Moose::Role;
use namespace::autoclean;

=head2 json
If we have JSON request, decodes it and stores it in the attribute.
=cut

has json => (
is => 'ro',
isa => 'HashRef|ArrayRef|Undef',
lazy => 1,
builder => '_build_json',
);

sub _build_json {
my $self = shift;

my $content_type = $self->content_type || return {};

if ( $content_type =~ m{^application/json}i ) {
return $self->data_handlers->{'application/json'}->( $self->body, $self );
}

return {};
}

1;
2 changes: 1 addition & 1 deletion t/01app.t
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ subtest 'tracking todo order' => sub {
my ( $method, $uri, $data ) = @_;
$json ||= JSON::MaybeXS->new( utf8 => 1 );

my $header = [];
my $header = [ 'Content-Type', 'application/json' ];
my $content = $data ? $json->encode( $data ) : q{};

my $res = request( HTTP::Request->new( uc( $method ), $uri, $header, $content ) );
Expand Down

0 comments on commit 43b8300

Please sign in to comment.