Net::Trackback::Message - an object representing a Trackback message.


Net-Trackback documentation Contained in the Net-Trackback distribution.

Index


Code Index:


Net-Trackback documentation Contained in the Net-Trackback distribution.

# Copyright (c) 2003-2004 Timothy Appnel (cpan@timaoutloud.org)
# http://www.timaoutloud.org/
# This code is released under the Artistic License.
package Net::Trackback::Message;
use strict;
use base qw( Class::ErrorHandler );

use Net::Trackback qw( encode_xml );

sub new { 
    my $self = bless {}, $_[0];
    # Should we filter out unknown fields?
    $self->{__stash} = $_[1] if $_[1];
    $self;
}

sub parse {
    my($class,$xml) = @_;
    # should use xml parser but...
    my($code) = $xml =~ m!<error>(\d+)</error>!s;
    my($message) = $xml =~ m!<message>(.+?)</message>!s;
    defined $code ? 
        $class->new({code=>$code,message=>$message}) : 
            $class->error('Invalid message.');
}

sub to_hash { %{ $_[0]->{__stash} } }

sub to_xml {
    my $self = shift;
    my $code = $self->{__stash}->{code} || 0;
    my $msg = encode_xml($self->{__stash}->{msg}) || '';
    my $xml = <<MESSAGE;
<?xml version="1.0" encoding="iso-8859-1"?>
<response>
  <error>$code</error>
  <message>$msg</message>
</response>
MESSAGE
    $xml;
}

sub code { $_[0]->{__stash}->{code} = $_[1] if $_[1]; $_[0]->{__stash}->{code}; }
sub message { $_[0]->{__stash}->{msg} = $_[1] if $_[1]; $_[0]->{__stash}->{msg}; }
sub is_success { !$_[0]->{__stash}->{code} }
sub is_error { $_[0]->{__stash}->{code} }

1;

__END__