POE::Component::WWW::Alexa::TrafficRank - non-blocking wrapper around WWW::Alexa::TrafficRank
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.
The module is a non-blocking wrapper around WWW::Alexa::TrafficRank which provides interface to traffic rank on http://alexa.com/
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'
'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.
30
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.
0
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.
$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:
uri
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.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 => 'random', _another => 'more', }
Optional. Any keys starting with _ (underscore) will not affect the component and will be passed back in the result intact.
_
$poe_kernel->post( alexa_rank => 'shutdown' );
Takes no arguments. Tells the component to shut itself down.
$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()
$_[ARG0]
{ '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' => 'zoffix.com' }
The uri key's value will be whatever you've specified in the uri argument to rank() event/method.
{ '_blah' => 'foos' }
Any arguments beginning with _ (underscore) passed into the rank() event/method will be present intact in the result.
The examples/ directory of this distribution contains a workable script which fetched ranks for URI passed on the command line:
examples/
perl examples/rank.pl zoffix.com
POE, WWW::Alexa::TrafficRank
Fork this module on GitHub: https://github.com/zoffixznet/POE-Component-Bundle-WebDevelopment
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
bug-POE-Component-Bundle-WebDevelopment at rt.cpan.org
Zoffix Znet <zoffix at cpan.org> (http://zoffix.com/, http://haslayout.net/)
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.
LICENSE
To install POE::Component::Bundle::WebDevelopment, copy and paste the appropriate command in to your terminal.
cpanm
cpanm POE::Component::Bundle::WebDevelopment
CPAN shell
perl -MCPAN -e shell install POE::Component::Bundle::WebDevelopment
For more information on module installation, please visit the detailed CPAN module installation guide.