NAME

MPMinus::RAMST - RAMST Ain't an MVC SKEL Transaction

VERSION

Version 1.02

SYNOPSIS

    use base qw/MPMinus::RAMST/;

    __PACKAGE__->register_handler( # GET /
        handler => "index",
        method  => "GET",
        path    => "/",
        query   => undef,
        deep    => 0,
        requires=> [qw/
                ADMIN USER TEST
            /],
        attrs   => {
                foo => 'test',
                bar => 1,
                baz => undef,
            },
        description => "Index",
        code    => sub {
        my $self = shift;
        my $name = shift;
        my $r = shift;
        my $q = $self->get("q");
        my $usr = $self->get("usr");
        #my $req_data = $self->get("req_data");
        #my $res_data = $self->get("res_data");

        $self->set( res_data => "Output scalar value" );
        $self->code( 200 );
        return 1; # Or 0 only!!
    });

    my $server = __PACKAGE__->new(
                request  => $r, # Apache2::RequestRec object
                location => "/rest",
                blank    => {
                        param1 => "init value"
                    },
                dvars    => { # Valid dir variables
                        key1 => "default value",
                        key2 => 0,
                    },
            );

    $server->run_handler( $m );

    $server->cleanup;

DESCRIPTION

Base class for not "MVC SKEL Transaction" model implementation

register_handler

    __PACKAGE__->register_handler( # GET /
        handler => "index",
        method  => "GET",
        path    => "/",
        query   => undef,
        deep    => 0,
        requires=> [qw/
                ADMIN USER TEST
            /],
        attrs   => {
                foo => 'test',
                bar => 1,
                baz => undef,
            },
        description => "Index",
        code    => sub {
        my $self = shift;
        my $name = shift;
        my $m = shift;
        my $r = $m->r;
        my $q = $self->get("q");
        my $usr = $self->get("usr");
        #my $req_data = $self->get("req_data");
        #my $res_data = $self->get("res_data");

        $self->set( res_data => "Output scalar value" );
        $self->code( 200 );
        return 1; # Or 0 only!!
    });

This is non class method! Sets new handler for trapping the request

attrs

Sets attributes of the handler as hashref

Default: {}

In the future, you can get the attribute values using the get_attr("attr_name") method

code

Sets callback function

This callback function returns bool status of the operation

deep

Enables deeply scanning of path for handler lookup. If this param is set to true then the mechanism of the deeply lookuping will be enabled. For example:

For registered path /foo with enabled deep lookuping will be matched any another incoming path that begins from /foo prefix: /foo, /foo/bar, /foo/bar/baz and etc.

description

Sets the description of handler

Default: none

handler

Sets the name of handler

Default: noname

method

Sets the method for trapping. Supported: GET, POST, PUT, DELETE.

Default: GET

path

Sets the path for trapping

Default: /

Note: This is second (tailed) part of the full path - it's suffix. Path not contents the base location string, e.g. without "/rest" prefix. If you specify "/test/null", then the real path will be trapped "/rest/test/null" as path

query

Sets the query string for trapping

Default: undef

    Example #1: action=foo

Catch if the "action" parameter in query string equals "foo"

    Example #2: action=foo&object=bar

Catch if the "action" parameter in query string equals "foo" and "object" parameter equals "bar"

    Example #3: action=

Catch if the "action" parameter in query string equals any value

requires

Array-ref structure that contains list of roles or any data for authorization

Default: []

new

    my $server = __PACKAGE__->new(
                prefix   => "MyTestApp",
                request  => $r, # Apache2::RequestRec object
                location => "/rest",
                blank    => {
                        param1 => "init value"
                    },
                dvars    => { # Valid dir variables
                        key1 => "default value",
                        key2 => 0,
                    },
            );

Constructor. Creates server instance by base location prefix (base attribute)

blank

The "blank" defines initial working structure for input and output variable data

dvars

The key-value structure, that defines list of dir_config variables and default values for each

location, base

Location is a named part of the "Location" section in Apache's configuration file

prefix

Prefix string. Default: current invocant class name

request

Optional value of the Apache2::RequestRec object

You can set and get data from the working structure using the "set" and "get" methods

cleanup

    $server->cleanup;

Cleans the all working data and sets it to blank structure

code

    my $code = $server->code;
    my $code = $server->code( 500 );

Gets/Sets response HTTP code

Default: 200 (OK)

data

    my $data = $server->data;
    $server->data({
            param1 => "new value",
        });

Gets/Sets working structure

Default: $self->{blank}

error

    my $error = $server->error;

Returns current error string

    my $status = $server->error( "new error string" );

Sets error string and returns status of the server

    my $status = $server->error( 500, "new error string" );

Sets response code and error string and returns status of the server

get

    my $value = $server->get("param1");

Returns parameter "param1" from working structure

get_attr

    my $value = $server->get_attr("foo");

Returns attribute "foo" from attributes of the current handler

get_dvar

    my $value = $server->get_dvar("key");

Returns dir config variable "key"

get_svar

    my $value = $server->get_svar("key");

Returns system variable "key"

init_svars

    $server->init_svars;

Takes current method, path and query string from request heads and sets system "key" value for definition the current handler

location

    my $location = $server->location;

Returns base location of current server instance. Default: "" in root (default) context "/"

lookup_handler

    my $handler = $server->lookup_handler;
    my $handler = $server->lookup_handler("handler_name");

Returns $handler structure from hash of registered handlers; or undef if handler is not registered

lookup_method

    $server->lookup_method("GET"); # returns 1
    $server->lookup_method("OPTIONS"); # returns 0

Checks the availability of the method by its name and returns the status

prefix

    my $prefix = $server->prefix;

Returns prefix of current server instance. Default: invocant class name

requires

    my $requires = $server->requires; # [1, 2, 7]

Returns list as "array-ref" of requires for authorization from attributes of the current handler

run_handler

    $server->run_handler( $m ); # $m - this is MPMinus main object

Runs the callback function from current handler with $m parameter

Note: any number of parameters can be specified, all of them will be receive in the callback function

Returns: server status

set

    $server->get("param1", "new value");

Sets new value to parameter "param1" in working structure and returns status of the operation

set_dvar

    $server->set_dvar("key", "new value");

Sets new value to dir config variable "key"

set_svar

    $server->set_svar("key", "new value");

Sets new value to system variable "key"

status

    my $status = $server->status;
    my $status = $server->status( $new_status );

Gets/Sets status of the server

HISTORY

See CHANGES file

DEPENDENCIES

mod_perl2, MPMinus

TO DO

See TODO file

BUGS

* none noted

SEE ALSO

MPMinus, Apache2::REST

AUTHOR

Serż Minus (Sergey Lepenkov) http://www.serzik.com <abalama@cpan.org>

COPYRIGHT

Copyright (C) 1998-2019 D&D Corporation. All Rights Reserved

LICENSE

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

See LICENSE file and https://dev.perl.org/licenses/