package POE::Component::WWW::OhNoRobotCom::Search; use warnings; use strict; our $VERSION = '0.001'; use Carp; use WWW::OhNoRobotCom::Search; use POE; use base 'POE::Component::NonBlockingWrapper::Base'; sub _methods_define { return ( search => '_wheel_entry' ); } sub search { $poe_kernel->post( shift->{session_id} => search => @_ ); } sub _prepare_wheel { my ( $self, $args ) = @_; $self->{_robo} = WWW::OhNoRobotCom::Search->new( %{ $self->{obj_args} || {} } ); } sub _check_args { my ( $self, $args_ref ) = @_; defined $args_ref->{term} or carp 'Missing `term` argument' and return; } sub _process_request { my ( $self, $in_ref ) = @_; my @search_args; for ( qw(comic_id include max_results lucky) ) { exists $in_ref->{$_} and push @search_args, $_, $in_ref->{$_}; } my $out_ref = $self->{_robo}->search( $in_ref->{term}, @search_args ); if ( defined $out_ref ) { $in_ref->{results} = $out_ref; } else { $in_ref->{error} = $self->{_robo}->error; } } 1; __END__ =head1 NAME POE::Component::WWW::OhNoRobotCom::Search - non-blocking POE based wrapper around WWW::OhNoRobotCom::Search module =head1 SYNOPSIS use strict; use warnings; use POE qw(Component::WWW::OhNoRobotCom::Search); my $poco = POE::Component::WWW::OhNoRobotCom::Search->spawn; POE::Session->create( package_states => [ main => [qw(_start results )] ], ); $poe_kernel->run; sub _start { $poco->search( { term => 'foo', comic_id => 56, # XKCD comics event => 'results', } ); } sub results { my $in_ref = $_[ARG0]; exists $in_ref->{error} and die "ZOMG! ERROR!: $in_ref->{error}"; print "Results for XKCD comic search are as follows:\n"; keys %{ $in_ref->{results} }; while ( my ( $uri, $title ) = each %{ $in_ref->{results} } ) { print "$title [ $uri ]\n"; } $poco->shutdown; } Using event based interface is also possible of course. =head1 DESCRIPTION The module is a non-blocking wrapper around L which provides interface to L search =head1 CONSTRUCTOR =head2 spawn my $poco = POE::Component::WWW::OhNoRobotCom::Search->spawn; POE::Component::WWW::OhNoRobotCom::Search->spawn( alias => 'robo', obj_args => { timeout => 10, }, options => { debug => 1, trace => 1, # POE::Session arguments for the component }, debug => 1, # output some debug info ); The C method returns a POE::Component::WWW::OhNoRobotCom::Search object. It takes a few arguments, I. The possible arguments are as follows: =head3 alias POE::Component::WWW::OhNoRobotCom::Search->spawn( alias => 'robo' ); B. Specifies a POE Kernel alias for the component. =head3 obj_args POE::Component::WWW::OhNoRobotCom::Search->spawn( obj_args => { timeout => 10, } ); B. The C argument takes a hashref as a value which will be dereferenced directly into L constructor. See documentation for L for more details. B empty (default L constructor) =head3 options my $poco = POE::Component::WWW::OhNoRobotCom::Search->spawn( options => { trace => 1, default => 1, }, ); B. A hashref of POE Session options to pass to the component's session. =head3 debug my $poco = POE::Component::WWW::OhNoRobotCom::Search->spawn( debug => 1 ); When set to a true value turns on output of debug messages. B C<0>. =head1 METHODS =head2 search $poco->search( { event => 'event_for_output', term => 'foo', comic_id => 56, include => [ qw(all_text meta) ], max_results => 20, lucky => 1, _blah => 'pooh!', session => 'other', } ); Takes a hashref as an argument, does not return a sensible return value. See C event's description for more information. =head2 session_id my $poco_id = $poco->session_id; Takes no arguments. Returns component's session ID. =head2 shutdown $poco->shutdown; Takes no arguments. Shuts down the component. =head1 ACCEPTED EVENTS =head2 search $poe_kernel->post( robo => search => { event => 'event_for_output', term => 'foo', comic_id => 56, include => [ qw(all_text meta) ], max_results => 20, lucky => 1, _blah => 'pooh!', session => 'other', } ); Instructs the component to perform the search. Takes a hashref as an argument, the possible keys/value of that hashref are as follows: =head3 event { event => 'results_event', } B. Specifies the name of the event to emit when results are ready. See OUTPUT section for more information. =head3 term { term => 'foo' } B. Specifies the term to search for. The first argument (mandatory) is the term you want to search. The other, optional, arguments are given in a key/value fashion and are as follows: =head3 comic_id { comic_id => 56 } B. The C argument takes a scalar as a value which should be a comic ID number or an empty string which indicates that search should be done on all comics. To obtain the comic ID number go to L, "View Source" and search for the name of the comic, when you'll find an