WWW::Pastebin::RafbNet::Create - create new pastes on http://rafb.net/


WWW-Pastebin-RafbNet-Create documentation Contained in the WWW-Pastebin-RafbNet-Create distribution.

Index


Code Index:

NAME

Top

WWW::Pastebin::RafbNet::Create - create new pastes on http://rafb.net/

SYNOPSIS

Top

    use WWW::Pastebin::RafbNet::Create;

    my $paster = WWW::Pastebin::RafbNet::Create->new;

    $paster->paste( $text )
        or die $paster->error;

    print "Your paste can be found on $paster\n";




DESCRIPTION

Top

The module provides means to create new pastes on http://rafb.net/ paste site.

The WWW::Rafb module offers a similiar functionality. However, it does not pass the test suite, and the author does not seem to care (last update was close to a year ago). As well, the module seems to have a bit of an "uncomfortable" interface, including not being able to paste text from a scalar easily.

CONSTRUCTOR

Top

new

    my $paster = WWW::Pastebin::RafbNet::Create->new;

    my $paster = WWW::Pastebin::RafbNet::Create->new(
        timeout => 10,
    );

    my $paster = WWW::Pastebin::RafbNet::Create->new(
        ua => LWP::UserAgent->new(
            timeout => 10,
            agent   => 'PasterUA',
        ),
    );

Constructs and returns a brand new yummy juicy WWW::Pastebin::RafbNet::Create object. Takes two arguments, both are optional. Possible arguments are as follows:

timeout

    ->new( timeout => 10 );

Optional. Specifies the timeout argument of LWP::UserAgent's constructor, which is used for pasting. Defaults to: 30 seconds.

ua

    ->new( ua => LWP::UserAgent->new( agent => 'Foos!' ) );

Optional. If the timeout argument is not enough for your needs of mutilating the LWP::UserAgent object used for pasting, feel free to specify the ua argument which takes an LWP::UserAgent object as a value. Note: the timeout argument to the constructor will not do anything if you specify the ua argument as well. Defaults to: plain boring default LWP::UserAgent object with timeout argument set to whatever WWW::Pastebin::RafbNet::Create's timeout argument is set to as well as agent argument is set to mimic Firefox.

METHODS

Top

paste

    $paster->paste( 'lotsa text' )
        or die $paster->error;

    $paster->paste(
        'lotsa text',
        nick => 'Zoffix',
        desc => 'some text',
        tabs => 8,
        lang => 'Perl',
    ) or die $paster->error;

Instructs the object to create a new paste on http://rafb.net/paste/. On success returns an URI object pointing to a newly created paste, but you don't have to store it, see uri() method which is also overloaded for this module. On failure returns either undef or an empty list depending on the context and the reason for error will be available via error() method.

Takes one mandatory argument, as well as several key/value optional arguments. The first argument is a scalar contaning the text you want to paste. The optional key/value arguments are as follows:

nick

    $paster->paste( 'text', nick => 'Zoffix' )

Optional. Takes a scalar contaning the nick of the poster. By default is not specified resulting in Anonymous as nick.

desc

    $paster->paste( 'text', desc => 'some description' )

Optional. Takes a scalar contaning the description of the paste. By default is not specified (no description).

tabs

    $paster->paste( 'text', tabs => '8' )

Optional. Takes a scalar contaning either no, 2, 3, 4, 5 6 or 8. Tells the pastebin to convert any tab characters to spaces, each tab should be replaced by spaces. The number of spaces per tab is specified as the value of tabs argument. The no value tells that no conversion should be done. Defaults to: no

lang

    $paster->paste( 'text', lang => 'Perl' )

Optional. Takes a scalar contaning a language "code" specifying the language of the paste (effectively turning appropriate syntax highlights on it). Defaults to: 'plain text'. Possible language codes are case-insensitive and are as follows, the left side represents the code to be used for lang argument and the right side is the language's name:

        c89                 => 'C (C89)',
        c                   => 'C (C99)',
        'c++'               => 'C++',
        'c#'                => 'C#',
        'java'              => 'Java',
        pascal              => 'Pascal',
        perl                => 'Perl',
        php                 => 'PHP',
        'pl/i'              => 'PL/I',
        python              => 'Python',
        ruby                => 'Ruby',
        sql                 => 'SQL',
        vb                  => 'Visual Basic',
        'plain text wrap'   => 'Word wrapped text',
        'plain text'        => 'Plain Text',

error

    $paster->paste( 'lotsa text' )
        or die $paster->error;

If paste() method fails it will return either undef or an empty list depending on the context and the reason for the error will be available via error() method. Takes no arguments, returns a human readable error message describing why paste() failed.

paste_uri

    printf "Paste is at: %s\n", $paster->paste_uri;

    # or

    print "Paste is at: $paster\n";

Must be called after a successfull call to paste(). Takes no arguments, returns a URI object pointing to a newly created paste. The module provides overload, thus instead of calling the paste_uri() method or storing the value of paster() method you could simply use WWW::Pastebin::RafbNet::Create object in a string.

response

    my $http_response_obj = $paster->response;

Must be called after a call to paste(). Takes no arguments, returns a HTTP::Response object obtained when a new was created. You can use this if you want to further investigate why paste() method failed.

timeout

    my $timeout = $paster->timeout;

Takes no arguments, returns whatever you've specified in the timeout argument in the constructor (new()) or its default if you didn't specify anything.

ua

    my $ua = $paster->ua;

    $paster->ua( LWP::UserAgent->new( timeout => 10, agent => 'MOOO!' );

Returns an LWP::UserAgent object used for pasting by the module. Takes one optional argument which should be an LWP::UserAgent object. If called with an argument the LWP::UserAgent object you specify will be used in any subsequent pasting.

SEE ALSO

Top

LWP::UserAgent, HTTP::Response

AUTHOR

Top

Zoffix Znet, <zoffix at cpan.org> (http://zoffix.com, http://haslayout.net)

BUGS

Top

Please report any bugs or feature requests to bug-www-pastebin-rafbnet-create at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Pastebin-RafbNet-Create. 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 WWW::Pastebin::RafbNet::Create

You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Pastebin-RafbNet-Create

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/WWW-Pastebin-RafbNet-Create

* CPAN Ratings

http://cpanratings.perl.org/d/WWW-Pastebin-RafbNet-Create

* Search CPAN

http://search.cpan.org/dist/WWW-Pastebin-RafbNet-Create

COPYRIGHT & LICENSE

Top


WWW-Pastebin-RafbNet-Create documentation Contained in the WWW-Pastebin-RafbNet-Create distribution.

package WWW::Pastebin::RafbNet::Create;

use warnings;
use strict;

our $VERSION = '0.001';

use Carp;
use URI;
use LWP::UserAgent;
use base 'Class::Data::Accessor';
__PACKAGE__->mk_classaccessors qw(
    paste_uri
    error
    response
    timeout
    ua
);

use overload q|""| => sub { shift->paste_uri };

sub new {
    my $class = shift;
    croak "Must have even number of arguments to the constructor"
        if @_ & 1;

    my %args = @_;
    
    unless ( $args{timeout} ) {
        $args{timeout} = 30;
    }
    unless ( $args{ua} ) {
        $args{ua} = LWP::UserAgent->new(
            timeout => $args{timeout},
            agent   => 'Mozilla/5.0 (X11; U; Linux x86_64; en-US;'
                        . ' rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy)'
                        . ' Firefox/2.0.0.12',

        );
    }

    my $self = bless {}, $class;
    $self->$_( $args{$_} ) for qw(ua timeout);
    return $self;
}

sub paste {
    my $self = shift;
    my %args = ( text => shift );

    return $self->_set_error(
        "Missing, undefined or empty first argument (the text to paste)"
    ) unless defined $args{text} and length $args{text};

    if ( @_ ) {
        croak "Must have ether one or even number of arguments to paste()"
            if @_ & 1;

        %args = ( @_, text => $args{text} );
        $args{ +lc } = delete $args{ $_ } for keys %args;

        %args = (
            lang        => 'plain text',
            tabs    => 'no',
            %args,
        );
        $args{cvt_tabs} = lc delete $args{tabs};
        $args{lang}     = lc delete $args{lang};

        return $self->_set_error( 'Missing or undefined `text` argument' )
            unless defined $args{text};

        return $self->_set_error('Invalid `lang` was specified')
            unless exists $self->_make_valid_languages->{ $args{lang} };

        return $self->_set_error('Invalid `tabs` was specified')
            unless exists $self->_make_valid_tabs->{ $args{cvt_tabs} };
    }
    else {
        @args{ qw( lang          cvt_tabs  desc  nick) }
        = (        'plain text', 'no',     '',   '' );
    }

    @args{ qw(lang cvt_tabs) } = (
        $self->_make_valid_languages->{ delete $args{lang} },
        $self->_make_valid_tabs->{ delete $args{cvt_tabs} },
    );

    $self->$_(undef) for qw(error paste_uri);

    my %form = ( %args, submit  => 'Paste', );
    my $uri = URI->new('http://rafb.net/paste/paste.php');

    my $response = $self->response( $self->ua->post( $uri, \%form ) );

    if ( $response->code == 302 ) {
        my $paste_uri = URI->new($response->header('Location'));
        if ( $paste_uri eq 'http://rafb.net/p/toofast.html' ) {
            return $self->_set_error('Pasting too fast (Flood protection)');
        }
        else {
            return $self->paste_uri( $paste_uri );
        }
    }
    else {
        return $self->_set_error(
            'Request failed: ' . $response->status_line
        );
    }
}

sub _set_error {
    my ( $self, $error ) = @_;
    $self->error( $error );
    return;
}

sub _make_valid_languages {
    return {
        c89     => 'C89',
        c       => 'C',
        'c++'   => 'C++',
        'c#'    => 'C#',
        'java'  => 'Java',
        pascal  => 'Pascal',
        perl    => 'Perl',
        php     => 'PHP',
        'pl/i'  => 'PL/I',
        python  => 'Python',
        ruby    => 'Ruby',
        sql     => 'SQL',
        vb      => 'VB',
        'plain text wrap'   => 'Plain Text Wrap',
        'plain text'        => 'Plain Text',
    };
}

sub _make_valid_tabs {
    return {
        'no' => 'No',
        map { $_ => $_ } 2..6, 8
    };
}

1;
__END__