Bot::ChatBots::Role::UserAgent - Bot::ChatBots Role for having a user agent
package Bot::ChatBots::Whatever::Sender; use Moo; with 'Bot::ChatBots::Role::Sender'; with 'Bot::ChatBots::Role::UserAgent'; sub send_message { my ($self, $message) = @_; $self->ua_request( GET => 'http://frobozz.example.com/v1/whatever', {Accept => 'application/json'}, json => $message ); } 1;
This role adds some user-agent capabilities using Mojo::UserAgent.
The following methods have a same-named option that can be passed to the constructor.
my $cb = $obj->callback; $obj->callback(sub {...});
Accessor for a possible callback sub reference. This is/can be used when callling Mojo::UserAgent in non-blocking mode.
say 'loop starts automatically' if $obj->start_loop; $obj->start_loop(0); # don't start automatically $obj->start_loop(1); # start loop automatically
Accessor for boolean attribute that instructs "ua_request" to start the Mojo::IOLoop automatically. This can still be overridden by anything passed with key start_loop in "ua_request".
start_loop
Defaults to 0 (false).
0
my $ua = $obj->ua; $obj->ua(Mojo::UserAgent->new);
Accessor for a Mojo::UserAgent compatible object.
It should be safe to override the following methods in your classes composing this role.
Builder for "ua". Defaults to a new fresh instance of Mojo::UserAgent.
$obj->clear_callback;
Remove any "callback" currently set.
say 'yes' if $obj->has_callback;
Predicate function to assess whethere a "callback" is set or not.
$self->may_start_loop(%args); $self->may_start_loop(\%args);
Evaluates conditions for starting Mojo::IOLoop. These conditions are:
if the provided %args contains a key start_loop, it is used to establish whether to start the loop or not, OTHERWISE
%args
if "start_loop" is false, the loop is not started, OTHERWISE
the loop is started if it's not already running.
This method is used by "ua_request".
It is suggested to use this method only if you are also using a callback in your call. Assuming that @callback contains a callback or not, you might want to call this method like this:
@callback
$obj->may_start_loop(%args) if @callback;
$obj->ua_request($method, %args);
This is a wrapper around different $method methods available in Mojo::UserAgent. For example, this call:
$method
$obj->ua_request(get => ua_args => \@parameters);
translates into this:
$obj->ua->get(@parameters);
If a "callback" is available (see "has_callback"), it is appended to the @parameters unless the last item is a sub reference itself. In either cases, Mojo::IOLoop is started unless it's already running.
@parameters
Mojo::IOLoop
Returns whatever the call to Mojo::UserAgent returns.
The recognised keys in %args are the following:
in case there is a callback, this flag tells ua_request to start the Mojo::IOLoop if it's not already running. Defaults to what set in accessor "start_loop".
ua_request
ua_args
the argument list for Mojo::UserAgent. This might be extended if it does not contain a callback but the object "has_callback".
Bot::ChatBots, Bot::ChatBots::Role::Sender.
Flavio Poletti <polettix@cpan.org>
Copyright (C) 2016 by Flavio Poletti <polettix@cpan.org>
This module is free software. You can redistribute it and/or modify it under the terms of the Artistic License 2.0.
This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose.
To install Bot::ChatBots, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Bot::ChatBots
CPAN shell
perl -MCPAN -e shell install Bot::ChatBots
For more information on module installation, please visit the detailed CPAN module installation guide.