Skip to content

Commit

Permalink
some refactoring in Dancer::Handler and Dancer::Serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexis Sukrieh committed Mar 18, 2010
1 parent 1f5f885 commit 9b06340
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
13 changes: 7 additions & 6 deletions lib/Dancer/Handler.pm
Expand Up @@ -27,15 +27,16 @@ sub get_handler {
sub handle_request {
my ($self, $request) = @_;

Dancer::SharedData->request($request);

# if a serializer is set, and content is present in the body of the request,
# deserialize it
Dancer::Serializer->handle_request if setting('serializer');
# deserialize the request body if possible
$request = Dancer::Serializer->process_request($request) if setting('serializer');

# save the request object
Dancer::SharedData->request($request);

# read cookies from client
Dancer::Cookies->init;

# TODO : move that elsewhere
if (setting('auto_reload')) {
eval "use Module::Refresh";
if ($@) {
Expand Down Expand Up @@ -66,7 +67,7 @@ sub render_response {
my ($self, $response) = @_;

# serializing magick occurs here! (only if needed)
$response = Dancer::Serializer->sanitize_response($response)
$response = Dancer::Serializer->process_response($response)
if setting('serializer');

my $content = $response->{content};
Expand Down
45 changes: 23 additions & 22 deletions lib/Dancer/Serializer.pm
Expand Up @@ -12,7 +12,6 @@ use Dancer::SharedData;
my $_engine;
sub engine {$_engine}

# TODO : change the serializer according to $name
sub init {
my ( $class, $name, $config ) = @_;
$name ||= 'Json';
Expand All @@ -22,7 +21,7 @@ sub init {
# takes a response object, and look wether or not it should be
# serialized.
# returns an error object if the serializer fails
sub sanitize_response {
sub process_response {
my ($class, $response) = @_;
my $content = $response->{content};

Expand Down Expand Up @@ -50,27 +49,29 @@ sub sanitize_response {
return $response;
}

sub handle_request {

my $request = Dancer::SharedData->request;

return
unless Dancer::Serializer->engine->content_type eq
$request->content_type;

return
unless ( $request->method eq 'PUT'
|| $request->method eq 'POST' );

my $rdata = $request->body;
my $new_params = Dancer::Serializer->engine->deserialize($rdata);
if ( keys %{ $request->{params} } ) {
$request->{params} = { %{ $request->{params} }, %$new_params };
}
else {
$request->{params} = $new_params;
# deserialize input params in the request body, if matching the Serializer's
# content-type.
sub process_request {
my ($class, $request) = @_;

return $request unless engine->content_type eq $request->content_type;
return $request unless $request->is_put || $request->is_post;

my $old_params = $request->params('body');

# try to deserialize
my $new_params;
eval { $new_params = engine->deserialize($request->body) };
if ($@) {
warn "Unable to deserialize request body with ".ref(engine()." : \n$@");
return $request;
}
Dancer::SharedData->request($request);

(keys %$old_params)
? $request->_set_body_params({%$old_params, %$new_params})
: $request->_set_body_params($new_params);

return $request;
}


Expand Down

0 comments on commit 9b06340

Please sign in to comment.