The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

POE::Component::WWW::Alexa::TrafficRank - non-blocking wrapper around WWW::Alexa::TrafficRank

SYNOPSIS

use strict;
use warnings;

use POE qw(Component::WWW::Alexa::TrafficRank);

my $poco = POE::Component::WWW::Alexa::TrafficRank->spawn;

POE::Session->create(
    package_states => [ main => [qw(_start rank )] ],
);

$poe_kernel->run;

sub _start {
    $poco->rank( {
            uri   => 'zoffix.com',
            event => 'rank',
        }
    );
}

sub rank {
    my $in_ref = $_[ARG0];

    if ( $in_ref->{error} ) {
        print "Error: $in_ref->{error}\n";
    }
    else {
        print "$in_ref->{uri}\'s rank is $in_ref->{rank}\n";
    }
    $poco->shutdown;
}

Using event based interface is also possible of course.

DESCRIPTION

The module is a non-blocking wrapper around WWW::Alexa::TrafficRank which provides interface to traffic rank on http://alexa.com/

CONSTRUCTOR

spawn

my $poco = POE::Component::WWW::Alexa::TrafficRank->spawn;

POE::Component::WWW::Alexa::TrafficRank->spawn(
    alias   => 'alexa_rank',
    agent   => 'Your User-Agent HTTP header',
    proxy   => 'http://someproxy.com',
    timeout => 30,
    options => {
        debug => 1,
        trace => 1,
        # POE::Session arguments for the component
    },
    debug => 1, # output some debug info
);

The spawn method returns a POE::Component::WWW::Alexa::TrafficRank object. It takes a few arguments, all of which are optional. The possible arguments are as follows:

alias

->spawn( alias => 'alexa_rank' );

Optional. Specifies a POE Kernel alias for the component.

agent

->new( agent => 'Your User-Agent HTTP header', );

Optional. Specifies which User-Agent string to use when accessing http://alexa.com/. Defaults to: 'Opera 9.5'

proxy

->new( proxy => 'http://someproxy.com' );

Optional. Specifies which HTTP proxy to use when accessing http://alexa.com/. By default no proxies are used.

timeout

->new( timeout => 30 );

Optional. Specifies the request timeout in seconds.. this will be passed to LWP::UserAgent object used by WWW::Alexa::TrafficRank. Defaults to: 30 seconds.

options

->spawn(
    options => {
        trace => 1,
        default => 1,
    },
);

Optional. A hashref of POE Session options to pass to the component's session.

debug

->spawn(
    debug => 1
);

When set to a true value turns on output of debug messages. Defaults to: 0.

METHODS

rank

$poco->rank( {
        event       => 'event_for_output',
        uri         => 'uri.for.which.to.get.rank.com',
        _blah       => 'pooh!',
        session     => 'other',
    }
);

Takes a hashref as an argument, does not return a sensible return value. See rank event's description for more information.

session_id

my $poco_id = $poco->session_id;

Takes no arguments. Returns component's session ID.

shutdown

$poco->shutdown;

Takes no arguments. Shuts down the component.

ACCEPTED EVENTS

rank

$poe_kernel->post( alexa_rank => rank => {
        event       => 'event_for_output',
        uri         => 'uri.for.which.to.get.rank.com',
        _blah       => 'pooh!',
        session     => 'other',
    }
);

Instructs the component to get the rank for the URI specified by the uri argument. Takes a hashref as an argument, the possible keys/value of that hashref are as follows:

event

{ event => 'results_event', }

Mandatory. Specifies the name of the event to emit when results are ready. See OUTPUT section for more information.

uri

{ uri => 'uri.for.which.to.get.rank.com', }

Mandatory. Specifies the URI for which to get the traffic rank.

session

{ session => 'other' }

{ session => $other_session_reference }

{ session => $other_session_ID }

Optional. Takes either an alias, reference or an ID of an alternative session to send output to.

user defined

{
    _user    => 'random',
    _another => 'more',
}

Optional. Any keys starting with _ (underscore) will not affect the component and will be passed back in the result intact.

shutdown

$poe_kernel->post( alexa_rank => 'shutdown' );

Takes no arguments. Tells the component to shut itself down.

OUTPUT

$VAR1 = {
    '_user' => 'defined argument',
    'rank' => '903,220',
    'uri' => 'zoffix.com'
};

$VAR1 = {
    'error' => 'No Data',
    '_user' => 'defined argument',
    'uri' => 'fsdofsdofsofsdfsdf.com'
};

The event handler set up to handle the event which you've specified in the event argument to rank() method/event will receive input in the $_[ARG0] in a form of a hashref. The possible keys/value of that hashref are as follows:

rank

{ 'rank' => '903,220', }

The rank for the URI which you passed in uri argument to rank() event/method. Will not be present if an error occurred (see below).

error

{ 'error' => 'No Data', }

If an error occurred during the request, it will be present in the error key. The rank will be missing in this case.

uri

{ 'uri' => 'zoffix.com' }

The uri key's value will be whatever you've specified in the uri argument to rank() event/method.

user defined

{ '_blah' => 'foos' }

Any arguments beginning with _ (underscore) passed into the rank() event/method will be present intact in the result.

EXAMPLES

The examples/ directory of this distribution contains a workable script which fetched ranks for URI passed on the command line:

perl examples/rank.pl zoffix.com

SEE ALSO

POE, WWW::Alexa::TrafficRank

REPOSITORY

Fork this module on GitHub: https://github.com/zoffixznet/POE-Component-Bundle-WebDevelopment

BUGS

To report bugs or request features, please use https://github.com/zoffixznet/POE-Component-Bundle-WebDevelopment/issues

If you can't access GitHub, you can email your request to bug-POE-Component-Bundle-WebDevelopment at rt.cpan.org

AUTHOR

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

LICENSE

You can use and distribute this module under the same terms as Perl itself. See the LICENSE file included in this distribution for complete details.