Skip to content
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

To work with version 0.09 of Dancer2 #4

Merged
merged 2 commits into from
Oct 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67 changes: 33 additions & 34 deletions lib/Dancer2/Plugin/REST.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use warnings;

use Carp 'croak';

use Dancer2 ':syntax';
use Dancer2;
use Dancer2::Plugin;

use Moo::Role;
Expand All @@ -33,27 +33,29 @@ register prepare_serializer_for_format => sub {
}
);

hook 'before' => sub {
my $format = params->{'format'};
$format ||= captures->{'format'} if captures;
return unless defined $format;
$app->hook(
'before' => sub {
my $format = $app->params->{'format'};
$format ||= $app->captures->{'format'} if $app->captures;

my $serializer = $serializers->{$format};
return unless defined $format;

unless( $serializer ) {
return send_error "unsupported format requested: " . $format, 404;
}
my $serializer = $serializers->{$format};

set serializer => $serializer;
my $ct = $content_types->{$format} || setting('content_type');
content_type $ct;
};
unless ($serializer) {
return $app->send_error(
'unsupported format requested: ' . $format, 404);
}

$app->set(serializer => $serializer);
my $ct = $content_types->{$format} || setting('content_type');
$app->content_type($ct);
}
);
};

register resource => sub {
my $self = shift;

my ($resource, %triggers) = @_;
my ($dsl, $resource, %triggers) = plugin_args(@_);

my %actions = (
get => 'get',
Expand All @@ -63,25 +65,25 @@ register resource => sub {
);

croak "resource should be given with triggers"
unless defined $resource
and grep { $triggers{$_} } keys %actions;

while( my( $action, $code ) = each %triggers ) {
$self->app->add_route(
method => $actions{$action},
regexp => $_,
code => $code,
) for map { sprintf $_, '/:id' x ($action ne 'create') }
"/${resource}%s.:format", "/${resource}%s";
unless defined $resource && grep { $triggers{$_} } keys %actions;

while (my ($action, $code) = each %triggers) {
$dsl->app->add_route(
method => $actions{$action},
regexp => $_,
code => $code,
)
for map { sprintf $_, '/:id' x ($action ne 'create') }
"/${resource}%s.:format", "/${resource}%s";
}
};

register send_entity => sub {
my ($entity, $http_code) = @_;
my ($dsl, $entity, $http_code) = plugin_args(@_);

$http_code ||= 200;

status($http_code);
$dsl->status($http_code);
$entity;
};

Expand Down Expand Up @@ -158,16 +160,13 @@ for my $code (keys %http_codes) {
$helper_name = "status_${helper_name}";

register $helper_name => sub {
shift;
my $dsl = shift;

send_entity(
( $code >= 400 ? {error => $_[0]} : $_[0] ),
$code
);
$dsl->send_entity(($code >= 400 ? {error => $_[0]} : $_[0]), $code);
};
}

register_plugin for_versions => [1,2];
register_plugin for_versions => [2];

1;

Expand Down
6 changes: 3 additions & 3 deletions t/02_prepare_serializer_for_format.t
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use strict;
use warnings;
use Dancer2::ModuleLoader;
use Class::Load 'try_load_class';
use Test::More import => ['!pass'];

plan skip_all => "JSON is needed for this test"
unless Dancer2::ModuleLoader->load('JSON');
unless try_load_class('JSON');
plan skip_all => "YAML is needed for this test"
unless Dancer2::ModuleLoader->load('YAML');
unless try_load_class('YAML');


my $data = { foo => 42 };
Expand Down
18 changes: 13 additions & 5 deletions t/03_resource.t
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use strict;
use warnings;
use Dancer2::ModuleLoader;
use Dancer2::Core::Request;
use Test::More import => ['!pass'];
use JSON;


# Dancer2::Test had a bug in version previous 1.3059_01 that prevent this test
# from running correctly.
my $dancer_version = eval "\$Dancer2::VERSION";
Expand Down Expand Up @@ -34,6 +34,7 @@ plan tests => 8;
use Dancer2::Plugin::REST;
use Test::More import => ['!pass'];

use Data::Dumper;
set show_errors => 1;
set serializer => 'JSON';
set logger => 'console';
Expand All @@ -53,27 +54,34 @@ plan tests => 8;
}

sub on_create_user {
my $ctx = shift;

my $id = ++$last_id;
my $user = params('body');
my $user = JSON::decode_json($ctx->request->body());
$user->{id} = $id;
$users->{$id} = $user;

{ user => $users->{$id} };
}

sub on_delete_user {
my $id = params->{'id'};
my $ctx = shift;

my $id = $ctx->request->params->{'id'};
my $deleted = $users->{$id};
delete $users->{$id};
{ user => $deleted };
}

sub on_update_user {
my $id = params->{'id'};
my $ctx = shift;

my $id = $ctx->request->params->{'id'};
my $user = $users->{$id};
return { user => undef } unless defined $user;

$users->{$id} = { %$user, %{params('body')} };
my $user_changed = JSON::decode_json($ctx->request->body());
$users->{$id} = { %$user, %$user_changed };
{ user => $users->{$id} };
}

Expand Down
22 changes: 15 additions & 7 deletions t/04_helpers.t
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use strict;
use warnings;
use Dancer2::ModuleLoader;
use JSON;
use Test::More import => ['!pass'];

Expand All @@ -23,33 +22,42 @@ plan tests => 16;
my $last_id = 0;

sub on_get_user {
my $id = params->{'id'};
my $ctx = shift;

my $id = $ctx->request->params->{'id'};
return status_bad_request('id is missing') if !defined $users->{$id};
status_ok( { user => $users->{$id} } );
}

sub on_create_user {
my $id = ++$last_id;
my $user = params('body');
my $ctx = shift;

my $id = ++$last_id;
my $user = JSON::decode_json($ctx->request->body());
$user->{id} = $id;
$users->{$id} = $user;

status_created( { user => $users->{$id} } );
}

sub on_delete_user {
my $id = params->{'id'};
my $ctx = shift;

my $id = $ctx->request->params->{'id'};
my $deleted = $users->{$id};
delete $users->{$id};
status_accepted( { user => $deleted } );
}

sub on_update_user {
my $id = params->{'id'};
my $ctx = shift;

my $id = $ctx->request->params->{'id'};
my $user = $users->{$id};
return status_not_found("user undef") unless defined $user;

$users->{$id} = { %$user, %{ params('body') } };
my $user_changed = JSON::decode_json($ctx->request->body());
$users->{$id} = { %$user, %$user_changed };
status_accepted { user => $users->{$id} };
}

Expand Down
6 changes: 3 additions & 3 deletions t/04_plugin_settings.t
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use strict;
use warnings;
use Dancer2::ModuleLoader;
use Class::Load 'try_load_class';
use Test::More import => ['!pass'];

plan skip_all => "JSON is needed for this test"
unless Dancer2::ModuleLoader->load('JSON');
unless try_load_class('JSON');
plan skip_all => "YAML is needed for this test"
unless Dancer2::ModuleLoader->load('YAML');
unless try_load_class('YAML');

my $data = { foo => 42 };
my $json = JSON::encode_json($data);
Expand Down
6 changes: 3 additions & 3 deletions t/05_named_captures.t
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use strict;
use warnings;
use Dancer2::ModuleLoader;
use Class::Load 'try_load_class';
use Test::More import => ['!pass'];

plan skip_all => "JSON is needed for this test"
unless Dancer2::ModuleLoader->load('JSON');
unless try_load_class('JSON');
plan skip_all => "YAML is needed for this test"
unless Dancer2::ModuleLoader->load('YAML');
unless try_load_class('YAML');


my $data = { foo => 42 };
Expand Down