SOAPjr::response - the SOAPjr response object


SOAPjr documentation Contained in the SOAPjr distribution.

Index


Code Index:

NAME

Top

SOAPjr::response - the SOAPjr response object

VERSION

Top

Version 1.1.2

SYNOPSIS

Top

    See perldoc SOAPjr for more info.

AUTHOR

Top

Rob Manson, <robman[at]cpan.org>

BUGS

Top

Please report any bugs or feature requests to bug-soapjr at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=SOAPjr. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

SUPPORT

Top

You can find documentation for this module with the perldoc command.

    perldoc SOAPjr




You can also look for information at:

* SOAPjr.org

http://SOAPjr.org

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=SOAPjr

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/SOAPjr

* CPAN Ratings

http://cpanratings.perl.org/d/SOAPjr

* Search CPAN

http://search.cpan.org/dist/SOAPjr/

ACKNOWLEDGEMENTS

Top

See http://SOAPjr.org/specs.html for further information on related RFC's and specifications.

COPYRIGHT & LICENSE

Top


SOAPjr documentation Contained in the SOAPjr distribution.
package SOAPjr::response;

use strict;
use warnings;

our $VERSION = "1.1.2";

use base qw(SOAPjr::message);
use Carp;

sub _init {
    my $self = shift;
    my $config= shift;
    $self = $self->SUPER::_init(@_);
    if ($config) {
        my $update_count = $self->set($config);
    } 
    return $self;
}

sub add_error {
    my $self = shift;
    my $error = shift;
    if ($error->{property} && $error->{error}->{code} && $error->{error}->{message}) {
        if (!$error->{context}) {
            $error->{context} = "BODY";
        }
        if (!$self->{_data}->{HEAD}->{errors}) {
            $self->{_data}->{HEAD}->{errors} = {};
        }
        $self->{_data}->{HEAD}->{errors}->{$error->{context}}->{$error->{property}} = $error->{error};
        return $self->output();
    } else {
        carp "property and an error with { code => NNN, message => xxx } is required for response::add_message()";
        return 0;
    }
}

sub output {
    my $self = shift;
    my $json;
    my $body = $self->get("BODY");
    if ($self->get("HEAD")->{errors}) {
        $self->set({ HEAD => { "result" => 0 } });
        $body = {};
    } else {
        $self->set({ HEAD => { "result" => 1 } });
    }
    if ($self->{json}->can("encode")) {
        # Modern-ish 2.x JSON API
        $json = $self->{json}->encode( { HEAD => $self->get("HEAD"), BODY => $body } );
    } elsif ($self->{json}->can("objToJson")) {
        # Olde Version 1.x JSON API
        $json = $self->{json}->objToJson( { HEAD => $self->get("HEAD"), BODY => $body } );
    } else {
        # TODO: handle unknown JSON API
    }
    return $json;
}

sub send {
    my $self = shift;
    print $self->output();
    return 1;
}

1;