blib/lib/Bot/Backbone/Service/Interactive.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 57 CC 3 SCAR 12.8
CC
3
Cov
0%
interactive_command
6.0
got_input
6.0
_build_handle
2.0
CRAP 3.6
1 package Bot::Backbone::Service::Interactive;
2 {
3 $Bot::Backbone::Service::Interactive::VERSION = '0.142250';
4 }
5 use Bot::Backbone::Service;
6  
7 # ABSTRACT: Access an external command through a bot
8  
9 with qw(
10 Bot::Backbone::Service::Role::Service
11 Bot::Backbone::Service::Role::ChatConsumer
12 );
13  
14 use AnyEvent::Run;
15 use Scalar::Util qw( reftype );
16  
17  
18 service_dispatcher as {
19 not_command run_this_method 'interactive_command';
20 };
21  
22  
23 has handle => (
24 is => 'rw',
25 isa => 'AnyEvent::Run',
26 lazy_build => 1,
27 clearer => 'clear_handle',
28 );
29  
30 sub _build_handle {
31 my $self = shift;
32  
33 my $handle = AnyEvent::Run->new(
34 cmd => $self->run_command,
35 on_read => sub {
36 my ($handle) = @_;
37  
38 my $input = $handle->{rbuf};
39 $handle->{rbuf} = '';
40  
41 $self->got_input($input);
42 },
43 on_error => sub {
44 $self->clear_handle;
45 },
46 );
47  
48 return $handle;
49 }
50  
51  
52 has interactive_prefix => (
53 is => 'ro',
54 isa => 'Str',
55 required => 1,
56 default => '#',
57 );
58  
59  
60 has run_command => (
61 is => 'ro',
62 isa => 'ArrayRef[Str]',
63 required => 1,
64 );
65  
66  
67 has input_cleaner => (
68 is => 'ro',
69 isa => 'CodeRef',
70 predicate => 'has_input_cleaner',
71 );
72  
73  
74 sub interactive_command {
75 my ($self, $message) = @_;
76 my $text = $message->text;
77
78 my $p = $self->interactive_prefix;
79  
80 if ($text =~ /^$p(.*)/) {
81 my $send_text = $1;
82 $message->add_flag('command');
83 $self->handle->push_write($send_text."\n");
84 }
85 }
86  
87  
88 sub got_input {
89 my ($self, $input) = @_;
90  
91 if ($self->has_input_cleaner) {
92 local $_ = $input;
93 $input = $self->input_cleaner->($input);
94 }
95  
96 $self->send_message({ text => $input });
97 }
98  
99  
100 sub initialize { }
101  
102  
103 sub receive_message { }
104  
105 1;
106  
107 __END__