The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Controller::REST - Mojolicious Controller for RESTful operations

VERSION

version 0.006

SYNOPSIS

    # In Mojolicious Controller
    use Mojo::Base 'Mojolicious::Controller::REST';
    
    $self->data( hello => 'world' )->message('Something went wrong');
    
    # renders json response as:
    
    {
        "data":
        {
            "hello": "world"
        },
        "messages":
        [
            {
                "severity": "info",
                "text": "Something went wrong"
            }
        ]
    }

DESCRIPTION

Mojolicious::Controller::REST helps with JSON rendering in RESTful applications. It follows and ensures the output of the method in controller adheres to the following output format as JSON:

    {
        "data":
        {
            "<key1>": "<value1>",
            "<key2>": "<value2>",
            ...
        },
        "messages":
        [
            {
                "severity": "<warn|info>",
                "text": "<message1>"
            },
            {
                "severity": "<warn|info>",
                "text": "<message2>"
            },
            ...
        ]
    }

Mojolicious::Controller::REST extends Mojolicious::Controller and adds below methods

METHODS

data

Sets the data element in 'data' array in JSON output. Returns controller object so that other method calls can be chained.

message

Sets an individual message in 'messages' array in JSON output. Returns controller object so that other method calls can be chained.

A custom severity value can be used by calling message as:

    $self->message('Something went wrong', 'fatal');

    # renders json response as:
    
    {
        "messages":
        [
            {
                "text": "Something went wrong",
                "severity": "fatal"
            }
        ]
    }

message_warn

Similar to message, but with severity = 'warn'. Returns controller object so that other method calls can be chained.

status

Set the status of response. Returns controller object so that other methods can be chained.

AUTHOR

Abhishek Shende <abhishekisnot@gmail.com>

COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Abhishek Shende.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.