Net::Google::Code::Role::Fetchable - Fetchable Role


Net-Google-Code documentation Contained in the Net-Google-Code distribution.

Index


Code Index:

NAME

Top

Net::Google::Code::Role::Fetchable - Fetchable Role

DESCRIPTION

Top

INTERFACE

Top

mech
fetch

AUTHOR

Top

sunnavy <sunnavy@bestpractical.com>

LICENCE AND COPYRIGHT

Top


Net-Google-Code documentation Contained in the Net-Google-Code distribution.

package Net::Google::Code::Role::Fetchable;
use Any::Moose 'Role';
use Params::Validate ':all';
use WWW::Mechanize;

our $MECH;

sub mech { 
    if (!$MECH) { 
        $MECH = WWW::Mechanize->new(
            agent       => 'Net-Google-Code',
            keep_alive  => 4,
            cookie_jar  => {},
            stack_depth => 1,
            timeout     => 60,
        );
    }
    return $MECH ;
}

sub fetch {
    my $self = shift;
    my ($url) = validate_pos( @_, { type => SCALAR } );
    $self->mech->get($url);
    if ( !$self->mech->response->is_success ) {
        die "Server threw an error "
          . $self->mech->response->status_line . " for "
          . $url;
    }
    else {
        my $content = $self->mech->content;
        # auto decode the content to erase HTML::Parser's utf8 warning like this:
        # Parsing of undecoded UTF-8 will give garbage when decoding entities
        utf8::downgrade( $content, 1 );
        return $content;
    }
}

no Any::Moose;

1;

__END__