From e3363ea411a18b378ebc04d5f92063eea06d1877 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bolila?= Date: Sat, 28 Sep 2013 22:08:18 +0100 Subject: [PATCH 1/2] changes to work w/ Dancer2 0.09 --- lib/Dancer2/Plugin/REST.pm | 69 ++++++++++++++-------------- t/02_prepare_serializer_for_format.t | 6 +-- t/03_resource.t | 18 ++++++-- t/04_helpers.t | 22 ++++++--- t/04_plugin_settings.t | 6 +-- t/05_named_captures.t | 6 +-- 6 files changed, 71 insertions(+), 56 deletions(-) diff --git a/lib/Dancer2/Plugin/REST.pm b/lib/Dancer2/Plugin/REST.pm index cbc51e4..6e6b047 100644 --- a/lib/Dancer2/Plugin/REST.pm +++ b/lib/Dancer2/Plugin/REST.pm @@ -6,7 +6,7 @@ use warnings; use Carp 'croak'; -use Dancer2 ':syntax'; +use Dancer2; use Dancer2::Plugin; use Moo::Role; @@ -20,7 +20,7 @@ my $content_types = { }; register prepare_serializer_for_format => sub { - my $app = shift; + my $dsl = shift; my $conf = plugin_setting; my $serializers = ( @@ -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; + $dsl->hook( + 'before' => sub { + my $format = $dsl->params->{'format'}; + $format ||= $dsl->captures->{'format'} if $dsl->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 $dsl->send_error( + 'unsupported format requested: ' . $format, 404); + } + + $dsl->set(serializer => $serializer); + my $ct = $content_types->{$format} || setting('content_type'); + $dsl->content_type($ct); + } + ); }; register resource => sub { - my $self = shift; - - my ($resource, %triggers) = @_; + my ($self, $resource, %triggers) = plugin_args(@_); my %actions = ( get => 'get', @@ -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) { + $self->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; }; @@ -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; diff --git a/t/02_prepare_serializer_for_format.t b/t/02_prepare_serializer_for_format.t index 7383e91..09ac848 100644 --- a/t/02_prepare_serializer_for_format.t +++ b/t/02_prepare_serializer_for_format.t @@ -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 }; diff --git a/t/03_resource.t b/t/03_resource.t index a1ea655..06b830b 100644 --- a/t/03_resource.t +++ b/t/03_resource.t @@ -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"; @@ -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'; @@ -53,8 +54,10 @@ 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; @@ -62,18 +65,23 @@ plan tests => 8; } 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} }; } diff --git a/t/04_helpers.t b/t/04_helpers.t index 49c92c3..827e399 100644 --- a/t/04_helpers.t +++ b/t/04_helpers.t @@ -1,6 +1,5 @@ use strict; use warnings; -use Dancer2::ModuleLoader; use JSON; use Test::More import => ['!pass']; @@ -23,14 +22,18 @@ 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; @@ -38,18 +41,23 @@ plan tests => 16; } 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} }; } diff --git a/t/04_plugin_settings.t b/t/04_plugin_settings.t index ef9b0f7..5bb81eb 100644 --- a/t/04_plugin_settings.t +++ b/t/04_plugin_settings.t @@ -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); diff --git a/t/05_named_captures.t b/t/05_named_captures.t index a4a6036..7d80c6b 100644 --- a/t/05_named_captures.t +++ b/t/05_named_captures.t @@ -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 }; From 0a8f19b2719694d0709ac0fee1c6f6519e859cb7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Bolila?= Date: Sat, 28 Sep 2013 22:15:12 +0100 Subject: [PATCH 2/2] changes to work w/ Dancer2 0.09 --- lib/Dancer2/Plugin/REST.pm | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/Dancer2/Plugin/REST.pm b/lib/Dancer2/Plugin/REST.pm index 6e6b047..9f30c0c 100644 --- a/lib/Dancer2/Plugin/REST.pm +++ b/lib/Dancer2/Plugin/REST.pm @@ -20,7 +20,7 @@ my $content_types = { }; register prepare_serializer_for_format => sub { - my $dsl = shift; + my $app = shift; my $conf = plugin_setting; my $serializers = ( @@ -33,29 +33,29 @@ register prepare_serializer_for_format => sub { } ); - $dsl->hook( + $app->hook( 'before' => sub { - my $format = $dsl->params->{'format'}; - $format ||= $dsl->captures->{'format'} if $dsl->captures; + my $format = $app->params->{'format'}; + $format ||= $app->captures->{'format'} if $app->captures; return unless defined $format; my $serializer = $serializers->{$format}; unless ($serializer) { - return $dsl->send_error( + return $app->send_error( 'unsupported format requested: ' . $format, 404); } - $dsl->set(serializer => $serializer); + $app->set(serializer => $serializer); my $ct = $content_types->{$format} || setting('content_type'); - $dsl->content_type($ct); + $app->content_type($ct); } ); }; register resource => sub { - my ($self, $resource, %triggers) = plugin_args(@_); + my ($dsl, $resource, %triggers) = plugin_args(@_); my %actions = ( get => 'get', @@ -68,7 +68,7 @@ register resource => sub { unless defined $resource && grep { $triggers{$_} } keys %actions; while (my ($action, $code) = each %triggers) { - $self->app->add_route( + $dsl->app->add_route( method => $actions{$action}, regexp => $_, code => $code,