Bot-ChatBots

 view release on metacpan or  search on metacpan

lib/Bot/ChatBots/Role/WebHook.pm  view on Meta::CPAN

package Bot::ChatBots::Role::WebHook;
use strict;
use warnings;
{ our $VERSION = '0.014'; }

use Ouch;
use Mojo::URL;
use Log::Any qw< $log >;
use Scalar::Util qw< blessed weaken refaddr >;
use Bot::ChatBots::Weak;
use Try::Tiny;

use Moo::Role;

with 'Bot::ChatBots::Role::Source';
requires 'process_updates';

has app => (
   is       => 'ro',

lib/Bot/ChatBots/Weak.pm  view on Meta::CPAN

package Bot::ChatBots::Weak;
use strict;
use warnings;
{ our $VERSION = '0.014'; }

use Scalar::Util qw< weaken >;

sub clone {
   my $self = shift;
   return ref($self)->new($self);
}

sub get {
   my ($self, $key) = @_;
   return $self->{$key};
}

lib/Bot/ChatBots/Weak.pm  view on Meta::CPAN

   my $self = bless {}, $package;
   return $self->set(@_);
}

sub set {
   my $self = shift;
   my @args = (@_ && ref($_[0])) ? %{$_[0]} : @_;
   while (@args) {
      my ($key, $value) = splice @args, 0, 2;
      $self->{$key} = $value;
      weaken($self->{$key}) if ref $value;
   }
   return $self;
} ## end sub set

sub TO_JSON { return undef }

1;

lib/Bot/ChatBots/Weak.pod  view on Meta::CPAN

   my $will_be_undef = $object->TO_JSON;


=head1 DESCRIPTION

This module provides a little wrapper class to keep things you want to be
as weak as possible.

The constructor takes either a hash reference or key/value pairs, and will
save it internally (the object is a hash reference). All references in
values will be weakened via L<Scalar::Util/weaken>, so that you don't have
to worry about circular references.

Additionally, when some JSON encoder tries to encode this object calling
L</TO_JSON>, it will get C<undef> back. This comes handy if you want to
e.g. defer some processing and put the whole thing in a queue, while still
getting rid of things that will not make sense any more while "on the
other side of the queue".

For example, while defining a hook for a L<Mojolicious> application, you
might want to save a reference to the C<$app> or to the controller C<$c>

lib/Bot/ChatBots/Weak.pod  view on Meta::CPAN

Method equivalent to C<< @{$object}{@keys} >>. Remember that the object is
a hash reference, so you can do that directly!

=head2 B<< new >>

   my $object = Bot::ChatBots::Weak->new(%kvpairs);
   my $object = Bot::ChatBots::Weak->new(\%kvpairs);

Returns a I<Weak> object based on the provided key/value pairs in
C<%kvpairs>. Every reference in values that is stored in C<$object> is
weakened via C<Scalar::Util/weaken>.

Returns an object that can be used as a hash reference. If you want to
add/change a key's value, it's strongly suggested to use L</set> below or
you might lose the weakening.

=head2 B<< set >>

   $object->set(%kvpairs); # OR
   $object->set(\%kvpairs);

Set key/value pairs according to what's in C<%kvpairs>. Every value that
is a reference is stored in a weakened way (i.e. via
L<Scalar::Util/weaken>).

You MUST use this method for setting values, otherwise the I<weak>
behaviour is not guaranteed (unless you do it by yourself, which is kind
of weird).

Returns C<$object> for possible chaining.

=head2 B<< TO_JSON >>

   my $this_is_undef = $object->TO_JSON;



( run in 0.294 second using v1.01-cache-2.11-cpan-65fba6d93b7 )