File Coverage

blib/lib/Bot/Backbone/Service/Role/Chat.pm
Criterion Covered Total %
statement 12 13 92.3
branch n/a
condition n/a
subroutine 5 5 100.0
pod 2 2 100.0
total 19 20 95.0


line stmt bran cond sub pod time code
1             package Bot::Backbone::Service::Role::Chat;
2             $Bot::Backbone::Service::Role::Chat::VERSION = '0.160630';
3 4     4   1762 use v5.10;
  4         10  
4 4     4   15 use Moose::Role;
  4         5  
  4         19  
5              
6             with 'Bot::Backbone::Service::Role::SendPolicy';
7              
8 4     4   8968 use Bot::Backbone::SendPolicy::Aggregate;
  4         957  
  4         653  
9              
10             # ABSTRACT: Chat services must implement this role
11              
12              
13             has chat_consumers => (
14                 is => 'ro',
15                 isa => 'ArrayRef', # 'ArrayRef[DOES Bot::Backbone::Service::Role::ChatConsumer]',
16                 required => 1,
17                 default => sub { [] },
18                 traits => [ 'Array' ],
19                 handles => {
20                     register_chat_consumer => 'push',
21                     list_chat_consumers => 'elements',
22                 },
23             );
24              
25              
26             sub send_reply {
27 3     3 1 5     my ($self, $message, $options) = @_;
28              
29 3         69     $self->send_message({
30                     group => $message->group,
31                     to => $message->from->username,
32                     %$options,
33                 });
34             }
35              
36              
37             sub resend_message {
38 21     21 1 89     my ($self, $message) = @_;
39              
40 21         735     for my $consumer ($self->list_chat_consumers) {
41 0                   $consumer->receive_message($message);
42                 }
43             }
44              
45             1;
46              
47             __END__
48            
49             =pod
50            
51             =encoding UTF-8
52            
53             =head1 NAME
54            
55             Bot::Backbone::Service::Role::Chat - Chat services must implement this role
56            
57             =head1 VERSION
58            
59             version 0.160630
60            
61             =head1 DESCRIPTION
62            
63             A chat service is one that sends and receives messages to other entities.
64            
65             See the following implementations:
66            
67             =over
68            
69             =item *
70            
71             L<Bot::Backbone::Service::ConsoleChat>
72            
73             =item *
74            
75             L<Bot::Backbone::Service::JabberChat>
76            
77             =item *
78            
79             L<Bot::Backbone::Service::IRChat>
80            
81             =item *
82            
83             L<Bot::Backbone::Service::SlackChat>
84            
85             =back
86            
87             =head1 ATTRIBUTES
88            
89             =head2 chat_consumers
90            
91             This is a list of L<Bot::Backbone::Service::Role::ChatConsumer>s that have registered to
92             receive messages from this chat service. A chat consumer is registered using the
93             C<register_chat_cnosumer> method. A C<list_chat_consumers> method is provided to
94             list the registered consumers.
95            
96             =head1 REQUIRED METHODS
97            
98             =head2 send_message
99            
100             # Send a direct message
101             $chat->send_message({
102             to => $to_username,
103             text => 'blah blah blah',
104             });
105            
106             # Send a group message
107             $chat->send_message({
108             group => $to_group,
109             text => 'blah blah blah',
110             });
111            
112             Sends a message to a group or individual using this chat service. This is used
113             when the message is not being made as a direct reply to a message received from
114             the chat service.
115            
116             If both C<group> and C<to> are passed, the preference should be to send to the C<group>.
117            
118             This role also provides a wrapper around your chat's implementation of
119             C<send_message>, which will apply the current service's send policy to the
120             message. The most restrictive send policy encountered at any point will win.
121            
122             A send policy may be explicitly provided by setting the C<send_policy> setting
123             in the parameters to a L<Bot::Backbone::SendPolicy> object. Missing this
124             parameter will result in an error. The C<send_policy_result> may also be set to
125             reflect the most restrictive send policy result encountered so far.
126            
127             =head1 METHODS
128            
129             =head2 send_reply
130            
131             $chat->send_reply($message, \%options);
132            
133             Given a message generated by this chat service, this should send a reply to the
134             origin of the message, whether that be a group or individual or other entity.
135            
136             The second argument is a hash reference of options, which is used to modify the
137             reply further. See L</send_message> for additional options and be aware that
138             individual chat implementations may provide more options in addition to those
139             shown here.
140            
141             =head2 resend_message
142            
143             $chat->resend_message($message);
144            
145             This should be called whenever a message is received from the chat service. This
146             message willb e forwarded to all of the registered L</chat_consumers>.
147            
148             =head1 AUTHOR
149            
150             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
151            
152             =head1 COPYRIGHT AND LICENSE
153            
154             This software is copyright (c) 2016 by Qubling Software LLC.
155            
156             This is free software; you can redistribute it and/or modify it under
157             the same terms as the Perl 5 programming language system itself.
158            
159             =cut
160