Dancer2::Plugin::REST - A plugin for writing RESTful apps with Dancer2
version 0.24
This plugin helps you write a RESTful webservice with Dancer2.
package MyWebService; use Dancer2; use Dancer2::Plugin::REST; prepare_serializer_for_format; get '/user/:id.:format' => sub { User->find(params->{id}); }; get qr{^/user/(?<id>\d+)\.(?<format>\w+)} => sub { User->find(captures->{id}); }; # curl http://mywebservice/user/42.json { "id": 42, "name": "John Foo", email: "john.foo@example.com"} # curl http://mywebservice/user/42.yml -- id: 42 name: "John Foo" email: "john.foo@example.com"
The default format serializer hash which maps a given :format to a Dancer2::Serializer::* serializer. Unless overriden in the configuration, it defaults to:
:format
Dancer2::Serializer::*
serializers: json: JSON yml: YAML dump: Dumper
When this pragma is used, a before filter is set by the plugin to automatically change the serializer when a format is detected in the URI.
That means that each route you define with a :format param or captures token will trigger a serializer definition, if the format is known.
This lets you define all the REST actions you like as regular Dancer2 route handlers, without explicitly handling the outgoing data format.
Regexp routes will use the file-extension from captures->{'format'} to determine the serialization format.
This keyword lets you declare a resource your application will handle.
resource user => get => sub { # return user where id = params->{id} }, create => sub { # create a new user with params->{user} }, delete => sub { # delete user where id = params->{id} }, update => sub { # update user with params->{user} }; # this defines the following routes: # GET /user/:id # GET /user/:id.:format # POST /user # POST /user.:format # DELETE /user/:id # DELETE /user/:id.:format # PUT /user/:id # PUT /user/:id.:format
Some helpers are available. This helper will set an appropriate HTTP status for you.
status_ok({users => {...}});
Set the HTTP status to 200
status_created({users => {...}});
Set the HTTP status to 201
status_accepted({users => {...}});
Set the HTTP status to 202
status_bad_request("user foo can't be found");
Set the HTTP status to 400. This function as for argument a scalar that will be used under the key error.
status_not_found("users doesn't exists");
Set the HTTP status to 404. This function as for argument a scalar that will be used under the key error.
This module is released under the same terms as Perl itself.
This module has been written by Alexis Sukrieh <sukria@sukria.net> and Franck Cuny.
<sukria@sukria.net>
Dancer2 http://en.wikipedia.org/wiki/Representational_State_Transfer
Dancer Core Developers
This software is copyright (c) 2010 by Alexis Sukrieh.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Dancer2::Plugin::REST, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer2::Plugin::REST
CPAN shell
perl -MCPAN -e shell install Dancer2::Plugin::REST
For more information on module installation, please visit the detailed CPAN module installation guide.