MojoX-JSON-RPC

 view release on metacpan or  search on metacpan

t/basic.t  view on Meta::CPAN

#!/usr/bin/env perl

use strict;
use warnings;

use File::Basename 'dirname';
use File::Spec;

use lib join '/', File::Spec->splitdir( dirname(__FILE__) ), 'lib';
use lib join '/', File::Spec->splitdir( dirname(__FILE__) ), '..', 'lib';

#-------------------------------------------------------------------

# Define custom service
package MyService;

use Mojo::Base 'MojoX::JSON::RPC::Service';

sub multiply {
    my ( $self, @params ) = @_;
    my $m = 1;
    $m *= $_ for @params;

    return $m;
}

sub echo {
    my ( $self, @params ) = @_;

    return $params[0];
}

sub echo_key {
    my ( $self, $param ) = @_;

    return exists $param->{key} ? $param->{key} : '?';
}

sub register {
    my ($self) = @_;
    return 'register can be used';
}

sub _rpcs {
    my ($self) = @_;
    return '_rpcs can be used';
}

sub substract {
    my ( $self, $res, @params ) = @_;
    $res = 0 if !defined $res;
    $res -= $_ for @params;
    return $res;
}

sub update {
    my ( $self, @params ) = @_;

    # notification only

    return;
}

sub foobar {
    my ($self) = @_;

    # notification only

    return;
}

__PACKAGE__->register_rpc_method_names(
    'multiply', 'echo',      'echo_key', 'register',
    '_rpcs',    'substract', 'update',   'foobar'
);

#-------------------------------------------------------------------

# Mojolicious app for testing
package MojoxJsonRpc;

use Mojo::Base 'Mojolicious';

use MojoX::JSON::RPC::Service;

# This method will run once at server start
sub startup {
    my $self = shift;

    $self->secrets(['Testing!']);

    # Load our test plugin
    my $svc = MojoX::JSON::RPC::Service->new->register(
        'sum',
        sub {
            my @params = @_;
            my $sum    = 0;
            $sum += $_ for @params;
            return $sum;
        }
        )->register(
        'remote_address',
        sub {
            my $tx = shift;
            return $tx->remote_address;
        },
        { with_mojo_tx => 1 }
        )->register(
        'substract',
        sub {
            my $params = shift;

            return $params->{minuend} - $params->{subtrahend};
        },
        )->register(
        'subtract',



( run in 0.628 second using v1.01-cache-2.11-cpan-df04353d9ac )