Net::Trackback::Ping - an object representing a Trackback ping.


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::Ping;
use strict;
use base qw( Class::ErrorHandler );

my %fields;
map { $fields{$_}=1 } 
    qw( title excerpt url blog_name timestamp ping_url id);

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

sub parse {   
    my $class = shift;
    my $q = shift;
    my $tb_id = $q->param('tb_id');
    unless ($tb_id) {
        if ( my $pi = $q->path_info() ) {
            ( $tb_id = $pi ) =~ s!^/!!;
        }
    }
    return $class->error('No Trackback ID (tb_id)') unless $tb_id;
    $tb_id =~ tr/a-zA-Z0-9/_/cs;
    return $class->error('No URL (url)') unless $q->param('url');
    my $self = $class->new();
    $self->{__stash} =
        { map { $_ => scalar $q->param($_) } 
            keys %fields };
    $self->{__stash}->{id} = $tb_id;
    $self->{__stash}->{title} ||= $self->{__stash}->{url};
    $self->{__stash}->{timestamp} = time;
    $self;
}

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

sub to_urlencoded { 
    my $self = shift;
    my $stash = $self->{__stash};
    my $str;
    foreach (grep { $stash->{$_} } keys %fields) {
        next if ($_ eq 'ping_url' || $_ eq 'timestamp');  
        $str .= '&' if $str;
        (my $val = $stash->{$_})
            =~s!([^a-zA-Z0-9_.-])!uc sprintf "%%%02x",ord($1)!eg;
        $str .= "$_=$val";
    }
    $str;
}

DESTROY { }

use vars qw( $AUTOLOAD );
sub AUTOLOAD {
    (my $var = $AUTOLOAD) =~ s!.+::!!;
    no strict 'refs';
    die "$var is not a recognized method."
        unless ( $fields{$var} );
    *$AUTOLOAD = sub {
        $_[0]->{__stash}->{$var} = $_[1] if $_[1];
        $_[0]->{__stash}->{$var};
    };
    goto &$AUTOLOAD;
}

1;

__END__