WWW::BashOrg - simple module to obtain quotes from http://bash.org/


WWW-BashOrg documentation Contained in the WWW-BashOrg distribution.

Index


Code Index:

NAME

Top

WWW::BashOrg - simple module to obtain quotes from http://bash.org/

SYNOPSIS

Top

    #!/usr/bin/env perl

    use strict;
    use warnings;
    use WWW::BashOrg;

    die "Usage: perl $0 quote_number\n"
        unless @ARGV;

    my $b = WWW::BashOrg->new;

    $b->get_quote(shift)
        or die $b->error . "\n";

    print "$b\n";

DESCRIPTION

Top

A simple a module to obtain either a random quote or a quote by number from http://bash.org/.

CONSTRUCTOR

Top

new

    my $b = WWW::BashOrg->new;

    my $b = WWW::BashOrg->new(
        ua  => LWP::UserAgent->new(
            agent   => 'Opera 9.5',
            timeout => 30,
        )
    );

Returns a newly baked WWW::BashOrg object. All arguments are options, so far only one argument is available:

ua

    my $b = WWW::BashOrg->new(
        ua  => LWP::UserAgent->new(
            agent   => 'Opera 9.5',
            timeout => 30,
        )
    );

Optional. Takes an LWP::UserAgent object as a value. This object will be used for fetching quotes from http://bash.org/. Defaults to:

    LWP::UserAgent->new(
        agent   => 'Opera 9.5',
        timeout => 30,
    )

METHODS

Top

get_quote

    my $quote = $b->get_quote('202477')
        or die $b->error;

Takes one mandatory argument - the number of the quote to fetch. Returns a string with the quote that was requested. If an error occurs, returns undef and the reason for failure can be obtained using error() method.

random

    my $quote = $b->random
        or die $b->error;

Takes no argumnets. Returns a random quote. If an error occurs, returns undef and the reason for failure can be obtained using error() method.

error

    my $quote = $b->random
        or die $b->error;

If an error occurs during execution of random() or get_quote() method will return the reason for failure.

quote

    my $last_quote = $b->quote;

    my $last_quote = "$b";

Takes no arguments. Must be called after a successfull call to either random() or get_quote(). Returns the same return value as last random() or get_quote() returned. This method is overloaded thus you can interpolate WWW::Bashorg in a string to obtain the quote.

ua

    my $old_ua = $b->ua;

    $b->ua(
        LWP::UserAgent->new( timeout => 20 ),
    );

Returns current LWP::UserAgent object that is used for fetching quotes. Takes one option argument that must be an LWP::UserAgent object (or compatible) - this object will be used for any future requests.

AUTHOR

Top

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

BUGS

Top

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

You can also look for information at:

* RT: CPAN's request tracker

http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-BashOrg

* AnnoCPAN: Annotated CPAN documentation

http://annocpan.org/dist/WWW-BashOrg

* CPAN Ratings

http://cpanratings.perl.org/d/WWW-BashOrg

* Search CPAN

http://search.cpan.org/dist/WWW-BashOrg/

COPYRIGHT & LICENSE

Top


WWW-BashOrg documentation Contained in the WWW-BashOrg distribution.

package WWW::BashOrg;

use warnings;
use strict;

our $VERSION = '0.0103';
use LWP::UserAgent;
use HTML::TokeParser::Simple;
use HTML::Entities;
use overload q|""| => sub { shift->quote };
use base 'Class::Data::Accessor';

__PACKAGE__->mk_classaccessors qw/
    ua
    error
    quote
/;

sub new {
    my $class = shift;
    my %args = @_;

    $args{ua} = LWP::UserAgent->new(
        agent   => 'Opera 9.5',
        timeout => 30,
    ) unless defined $args{ua};

    my $self = bless {}, $class;

    $self->$_( $args{ $_ } ) for keys %args;

    return $self;
}

sub get_quote {
    my ( $self, $num ) = @_;

    $self->quote( undef );
    $self->error( undef );

    unless ( length $num and $num =~ /^\d+$/ ) {
        $self->error('Invalid quote number');
        return;
    }

    my $res = $self->{ua}->get("http://bash.org/?quote=$num");
    unless ( $res->is_success ) {
        $self->error("Network error: " . $res->status_line );
        return;
    }

    my $quote = $self->_parse_quote( $res->decoded_content );
    unless ( defined $quote ) {
        $self->error('Quote not found');
        return;
    }

    return $self->quote( $quote );
}

sub random {
    my $self = shift;

    $self->quote( undef );
    $self->error( undef );

    my $res = $self->{ua}->get("http://bash.org/?random1");
    unless ( $res->is_success ) {
        $self->error("Network error: " . $res->status_line );
        return;
    }

    my $quote = $self->_parse_quote( $res->decoded_content );
    unless ( defined $quote ) {
        $self->error('Quote not found');
        return;
    }

    return $self->quote( $quote );
}

sub _parse_quote {
    my ( $self, $content ) = @_;

    my $p = HTML::TokeParser::Simple->new( \$content );

    my $get_quote;
    my $quote;
    while ( my $t = $p->get_token ) {
        if ( $t->is_start_tag('p')
            and defined $t->get_attr('class')
            and $t->get_attr('class') eq 'qt'
        ) {
            $get_quote = 1;
        }

        if ( $get_quote and $t->is_text ) {
            $quote .= $t->as_is;
        }

        if ( $get_quote and $t->is_end_tag('p') ) {
            $quote =~ s/&nbsp;/ /g;
            return decode_entities $quote;
        }
    }

    return undef;
}

1;
__END__