Bot-ChatBots
view release on metacpan or search on metacpan
lib/Bot/ChatBots/Role/Poller.pod view on Meta::CPAN
=pod
=encoding utf8
=head1 NAME
Bot::ChatBots::Role::Poller - Bot::ChatBots Role for Pollers
=head1 SYNOPSIS
package Something;
use Moo;
with 'Bot::ChatBots::Role::Poller';
sub poll {
my ($self, $callback, $args) = @_;
...
}
sub parse_response {
my ($self, $data) = @_;
...
}
sub normalize_record {
my ($self, $record) = @_;
...
}
1;
# later that night...
my $something = Something->new(
interval => 30, # polling interval
processor => \&process_record,
);
Mojo::IOLoop->start;
=head1 DESCRIPTION
This is a poller role for bots. This role is actually a derivation of
L<Bot::ChatBots::Role::Source>.
A poller works like this:
=over
=item *
L</schedule> installs a recurring call to whatever L</poller> returns.
Nothing is executed until L<Mojo::IOLoop/start> is run.
=item *
L</poller> wraps a call to L</poll>, which is the real workhorse for
fetching data. The wrapping is such that L</poll> will not be called if
another instance of it is already running.
=item *
L</poll> - provided by your class - fetches new data and pushes this new
data to a callback. This allows asynchronous implementations, e.g. based
on L<Mojo::UserAgent> (which accepts a callback).
=item *
L</parse_response>, L</process_updates> and L</normalize_record> (required
by the base L<Bot::ChatBots::Role::Source>) are then called for
implementing the usual pipeline (parsing data into updates, process them
and normalize the resulting records).
=back
=head2 What Should You Provide/Override
This is what you should provide and probably override in the general case:
=over
=item *
L</normalize_record> as per L<Bot::ChatBots::Role::Source>.
=item *
L</parse_response> is where you dissect received data into updates.
=item *
L</poll> is called regularly to fetch some updates.
=back
=head1 METHODS
It should be safe to override the following methods in your classes
composing this role.
=begin COMMENT
=head2 B<< BUILD >>
calls L</schedule> upon construction.
=end COMMENT
=head2 B<< args >>
my $args_aref = $obj->args;
Set a list of arguments eventually consumed by L</poll>, inside an array
reference.
=head2 B<< interval >>
my $interval = $obj->interval;
Interval for scheduling calls to L</poll>.
=head2 B<< poller >>
my $sub_ref = $obj->poller;
( run in 0.498 second using v1.01-cache-2.11-cpan-39bf76dae61 )