Hubot/Robot.pm

filter: Statementstmt 72.3% 128 / 177 Branchbran 41.6% 20 / 48 Conditioncond 52.6% 10 / 19 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 63.6% 28 / 44 Podpod 0.0% 0 / 21 totaltotal 58.6% 187 / 319 CC 38 SCAR 15.8
CC
38
Cov
65%
respond
13.3
userForName
12.0
setupHerokuPing
7.1
CRAP 4.9
« Hubot/EventEmitter.pm ↑ Summary Hubot/Adapter/Campfire.pm »
1 package Hubot::Robot;
2 6 $Hubot::Robot::VERSION = '0.2.8';
3 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use AnyEvent::HTTPD;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use AnyEvent::HTTP::ScopedClient;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Hubot::User;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Hubot::Brain;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Hubot::Listener;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Hubot::TextListener;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 has 'name' => ( is => 'rw', isa => 'Str' );
18 6 has 'alias' => ( is => 'rw', isa => 'Str' );
19 6 has 'mode' => ( is => 'rw', isa => 'Str', default => '' );
20 6 has 'adapter' => ( is => 'rw' );
21 has 'brain' => (
22 is => 'ro',
23 isa => 'Hubot::Brain',
24 5 ▶ default => sub { Hubot::Brain->new }
Subroutine __ANON__: called CC=1 SCAR=0.0
25 6 );
26 has '_helps' => (
27 traits => ['Array'],
28 is => 'rw',
29 isa => 'ArrayRef[Str]',
30 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 6 handles => { helps => 'elements', addHelp => 'push', }
32 );
33 has '_commands' => (
34 traits => ['Array'],
35 is => 'rw',
36 isa => 'ArrayRef[Str]',
37 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
38 6 handles => { commands => 'elements', addCommand => 'push', }
39 );
40 has '_listeners' => (
41 traits => ['Array'],
42 is => 'rw',
43 isa => 'ArrayRef[Hubot::Listener]',
44 5 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
45 6 handles => { listeners => 'elements', addListener => 'push', }
46 );
47  
48 ## Ping Watcher
49 6 has 'pw' => ( is => 'rw' );
50 6 has 'httpd' => ( is => 'rw' );
51  
52 5 ▶sub BUILD {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
53 5 my $self = shift;
54  
55 5 $self->setupHerokuPing;
56 5 $self->loadAdapter( $self->adapter );
57 }
58  
59 5 ▶sub setupHerokuPing {
Subroutine setupHerokuPing: called CC=4 SCAR=19.6
Pod undocumented
60 5 my $self = shift;
61  
62 5 ▶ my $httpd = AnyEvent::HTTPD->new( port => $ENV{PORT} || 8080, host => $ENV{HUBOT_HTTPD_ADDRESS} || '0.0.0.0' );
Condition50%2/4
$ENV{'PORT'} || 8080
l!l
05
$ENV{'HUBOT_HTTPD_ADDRESS'} || '0.0.0.0'
l!l
05
MC/DC0%0/1
$ENV{'PORT'} || 8080
$ENV{'PORT'}
MC/DC0%0/1
$ENV{'HUBOT_HTTPD_ADDRESS'} || '0.0.0.0'
$ENV{'HUBOT_HTTPD_ADDRESS'}
Truth table50%1/2
A
A= $ENV{'PORT'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'HUBOT_HTTPD_ADDRESS'}
Aresult
11
00
63 $httpd->reg_cb(
64 0 ▶ '/hubot/ping' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 0 my ( $httpd, $req ) = @_;
66 0 $req->respond( { content => ['text/plain', "pong"] } );
67 }
68 5 );
69  
70 5 $self->httpd($httpd);
71  
72 5 ▶ my $herokuUrl = $ENV{HEROKU_URL} || return;
Condition50%1/2
$ENV{'HEROKU_URL'} || (return)
l!l
05
MC/DC0%0/1
$ENV{'HEROKU_URL'} || (return)
$ENV{'HEROKU_URL'}
Truth table50%1/2
A
A= $ENV{'HEROKU_URL'}
Aresult
11
00
73 0 $herokuUrl =~ s{/?$}{/hubot/ping};
74  
75 $self->pw(
76 AE::timer 0,
77 120,
78 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 AnyEvent::HTTP::ScopedClient->new($herokuUrl)
80 0 ▶ ->post( sub { print "Keep alive ping!\n" if $ENV{DEBUG} } );
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 0 0
81 }
82 0 );
83 }
84  
85 5 ▶sub loadAdapter {
Subroutine loadAdapter: called CC=2 SCAR=7.2
Pod undocumented
86 5 my ( $self, $adapter ) = @_;
87  
88 5 $adapter = "Hubot::Adapter::" . ucfirst($adapter);
89 5 eval "require $adapter; 1";
90 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 5 5
91 0 print STDERR "Cannot load adapter $adapter - $@\n";
92 }
93 else {
94 5 $self->adapter( $adapter->new( { robot => $self } ) );
95 }
96 }
97  
98 5 ▶sub run { shift->adapter->run }
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
99  
100 21 ▶sub userForId {
Subroutine userForId: called CC=6 SCAR=19.1
Pod undocumented
101 21 my ( $self, $id, $options ) = @_;
102 21 my $user = $self->brain->{data}{users}{$id};
103 21 ▶ unless ($user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 6 15 21
104 6 $user = Hubot::User->new( { id => $id, %$options } );
105 6 $self->brain->{data}{users}{$id} = $user;
106 }
107  
108 21 ▶ my $options_room = $options->{room} || '';
Condition100%2/2
$options->{'room'} || ''
l!l
201
MC/DC100%1/1
$options->{'room'} || ''
$options->{'room'}
Truth table100%2/2
A
A= $options->{'room'}
Aresult
11
00
109 21 ▶ if ( $options_room
Branch: 1 branch
BranchTrueFalseTotal
if ($options_room and !$user->{"room"} || $user->{"room"} ne $options_room) 0 21 21
Condition50%3/6
$options_room and !$user->{"room"} || $user->{"room"} ne $options_room
!ll&&!rl&&r
1200
not $user->{"room"} or $user->{"room"} ne $options_room
l!l&&r!l&&!r
0020
MC/DC0%0/3
$options_room and !$user->{"room"} || $user->{"room"} ne $options_room
$options_room not $user->{"room"} $user->{"room"} ne $options_room
Truth table50%2/4
A and not(B) or C
A= $options_roomB= not $user->{"room"}C= $user->{"room"} ne $options_room
ABCresult
0XX0
1000
11X1
1011
110 && ( !$user->{room} || $user->{room} ne $options_room ) )
111 {
112 0 $user = Hubot::User->new( { id => $id, %$options } );
113 0 $self->brain->{data}{users}{$id} = $user;
114 }
115  
116 21 return $user;
117 }
118  
119 0 ▶sub userForName {
Subroutine userForName: not called CC=3 SCAR=24.8
Pod undocumented
120 0 my ( $self, $name ) = @_;
121 0 my $result;
122 0 for my $k ( keys %{ $self->brain->{data}{users} } ) {
123 0 my $userName = $self->brain->{data}{users}{$k}{name};
124 0 ▶ if ( lc $userName eq lc $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $userName eq lc $name) 0 0 0
125 0 $result = $self->brain->{data}{users}{$k};
126 }
127 }
128  
129 0 return $result;
130 }
131  
132 7 ▶sub usersForFuzzyRawName {
Subroutine usersForFuzzyRawName: called CC=4 SCAR=13.9
Pod undocumented
133 7 my ( $self, $fuzzyName ) = @_;
134 7 my $lowerFuzzyName = lc $fuzzyName;
135 7 my @users;
136 7 ▶ while ( my ( $key, $user ) = each %{ $self->brain->{data}{users} || {} } )
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->brain->{"data"}{"users"} or {};}) 14 7 21
Condition50%1/2
$self->brain->{'data'}{'users'} || {}
l!l
210
MC/DC0%0/1
$self->brain->{'data'}{'users'} || {}
$self->brain->{'data'}{'users'}
Truth table50%1/2
A
A= $self->brain->{'data'}{'users'}
Aresult
11
00
137 {
138 14 ▶ if ( lc( $user->{name} ) =~ m/^$lowerFuzzyName/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc($user->{"name"}) =~ /^$lowerFuzzyName/) 5 9 14
139 5 push @users, $user;
140 }
141 }
142  
143 7 return @users;
144 }
145  
146 7 ▶sub usersForFuzzyName {
Subroutine usersForFuzzyName: called CC=3 SCAR=11.0
Pod undocumented
147 7 my ( $self, $fuzzyName ) = @_;
148 7 my @matchedUsers = $self->usersForFuzzyRawName($fuzzyName);
149 7 my $lowerFuzzyName = lc $fuzzyName;
150 7 for my $user (@matchedUsers) {
151 5 ▶ return $user if lc( $user->{name} ) eq $lowerFuzzyName;
Branch: 1 branch
BranchTrueFalseTotal
if lc $user->{"name"} eq $lowerFuzzyName 5 0 5
152 }
153  
154 2 return @matchedUsers;
155 }
156  
157 0 ▶sub shutdown {
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod undocumented
158 0 my $self = shift;
159 0 $self->brain->close;
160 0 $self->adapter->close;
161 }
162  
163 5 ▶sub loadHubotScripts {
Subroutine loadHubotScripts: called CC=2 SCAR=6.9
Pod undocumented
164 5 my ( $self, $scripts ) = @_;
165 ## TODO: Debug Message
166 # print "Loading hubot-scripts\n" if $ENV{DEBUG};
167 5 for my $script (@$scripts) {
168 10 $self->loadFile($script);
169 }
170 }
171  
172 10 ▶sub loadFile {
Subroutine loadFile: called CC=2 SCAR=7.1
Pod undocumented
173 10 my ( $self, $script ) = @_;
174 10 my $full = "Hubot::Scripts::$script";
175 10 eval "require $full; 1";
176 10 $full->load($self);
177 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 10 10
178 0 print STDERR "Unable to load $full: $@\n";
179 }
180 else {
181 10 $self->parseHelp($full);
182 }
183 }
184  
185 10 ▶sub parseHelp {
Subroutine parseHelp: called CC=3 SCAR=11.0
Pod undocumented
186 10 my ( $self, $module ) = @_;
187 10 $module =~ s{::}{/}g;
188 10 my $fullpath = $INC{ $module . '.pm' };
189  
190 10 ▶ open my $fh, '>', \my $usage or die "Couldn't open filehandle: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", \my $usage or die "Couldn't open filehandle: $!\n" 10 0 10
191 10 pod2usage(
192 { -input => $fullpath, -output => $fh, -exitval => 'noexit', } );
193  
194 10 $usage =~ s/^Usage://;
195 10 $usage =~ s/(^\s+|\s+$)//gm;
196 10 $self->addHelp($_) for split( /\n/, $usage );
197  
198 10 $module =~ s{Hubot/Scripts/}{};
199 10 $self->addCommand($module);
200 }
201  
202 3 ▶sub hear {
Subroutine hear: called CC=1 SCAR=0.0
Pod undocumented
203 3 my ( $self, $regex, $callback ) = @_;
204 3 $self->addListener(
205 new Hubot::TextListener(
206 robot => $self,
207 regex => $regex,
208 callback => $callback
209 )
210 );
211 }
212  
213 16 ▶sub respond {
Subroutine respond: called CC=8 SCAR=25.9
Pod undocumented
214 16 my ( $self, $regex, $callback ) = @_;
215  
216 16 my $index = index "$regex", ':';
217 16 my $stringRegex = substr "$regex", ( $index + 1 ), -1;
218 16 my $first = substr $stringRegex, 0, 1;
219  
220 ## TODO: $^ 에 따른 분기; perl version 에 따라서 Regex object 의 modifier 위치가 달라짐
221 16 my $modifiers = '';
222 16 my $modifiersLen = $index - 3;
223 16 ▶ if ( $modifiersLen > 0 && length $stringRegex > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiersLen > 0 and length $stringRegex > 3) 16 0 16
Condition33%1/3
$modifiersLen > 0 and length $stringRegex > 3
!ll&&!rl&&r
0016
MC/DC0%0/2
$modifiersLen > 0 and length $stringRegex > 3
$modifiersLen > 0 length $stringRegex > 3
Truth table33%1/3
A and B
A= $modifiersLen > 0B= length $stringRegex > 3
ABresult
0X0
100
111
224 16 $modifiers = substr $stringRegex, 3, $modifiersLen;
225 }
226  
227 16 ▶ if ( $first eq '^' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first eq "^") 0 16 16
228 0 print STDERR
229 "Anchors don't work well with respond, perhaps you want to use 'hear'\n";
230 0 print STDERR "The regex in question was $stringRegex\n";
231 }
232  
233 16 my $newRegex;
234 16 my $name = $self->name;
235 16 ▶ if ( $self->alias ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->alias) { } 0 16 16
236 0 my $alias = $self->alias;
237 0 $alias =~ s/[-[\]{}()\*+?.,\\^$|#\s]/\\$&/g; # escape alias for regexp
238  
239 ## TODO: fix to generate correct regex
240 ## qr/regex/$var 처럼 modifier 에 변수가 들어갈 수 없음
241 ## 일단 modifiers 가 있다면 `i` 라고 가정하고 들어감 WTH..
242 0 ▶ if ($modifiers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers) { } 0 0 0
243 0 $newRegex = qr/^(?:$alias[:,]?|$name[:,]?)\s*(?:$stringRegex)/i;
244 }
245 else {
246 0 $newRegex = qr/^(?:$alias[:,]?|$name[:,]?)\s*(?:$stringRegex)/;
247 }
248 }
249 else {
250 16 ▶ if ($modifiers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modifiers) { } 16 0 16
251 16 $newRegex = qr/^(?:$name[:,]?)\s*(?:$stringRegex)/i;
252 }
253 else {
254 0 $newRegex = qr/^(?:$name[:,]?)\s*(?:$stringRegex)/;
255 }
256 }
257  
258 16 ▶ print "$newRegex\n" if $ENV{DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} 0 16 16
259 16 $self->addListener(
260 new Hubot::TextListener(
261 robot => $self,
262 regex => $newRegex,
263 callback => $callback
264 )
265 );
266 }
267  
268 0 ▶sub enter {
Subroutine enter: not called CC=1 SCAR=6.9
Pod undocumented
269 0 my ( $self, $callback ) = @_;
270 $self->addListener(
271 Hubot::Listener->new(
272 robot => $self,
273 0 ▶ matcher => sub { ref(shift) eq 'Hubot::EnterMessage' ? 1 : () },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref shift() eq 'Hubot::EnterMessage' ? : 0 0 0
274 0 callback => $callback
275 )
276 );
277 }
278  
279 0 ▶sub leave {
Subroutine leave: not called CC=1 SCAR=6.9
Pod undocumented
280 0 my ( $self, $callback ) = @_;
281 $self->addListener(
282 Hubot::Listener->new(
283 robot => $self,
284 0 ▶ matcher => sub { ref(shift) eq 'Hubot::LeaveMessage' ? 1 : () },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref shift() eq 'Hubot::LeaveMessage' ? : 0 0 0
285 0 callback => $callback
286 )
287 );
288 }
289  
290 0 ▶sub whisper {
Subroutine whisper: not called CC=1 SCAR=6.9
Pod undocumented
291 0 my ( $self, $callback ) = @_;
292 $self->addListener(
293 Hubot::Listener->new(
294 robot => $self,
295 0 ▶ matcher => sub { ref(shift) eq 'Hubot::WhisperMessage' ? 1 : () },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref shift() eq 'Hubot::WhisperMessage' ? : 0 0 0
296 0 callback => $callback
297 )
298 );
299 }
300  
301 0 ▶sub notice {
Subroutine notice: not called CC=1 SCAR=6.9
Pod undocumented
302 0 my ( $self, $callback ) = @_;
303 $self->addListener(
304 Hubot::Listener->new(
305 robot => $self,
306 0 ▶ matcher => sub { ref(shift) eq 'Hubot::NoticeMessage' ? 1 : () },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref shift() eq 'Hubot::NoticeMessage' ? : 0 0 0
307 0 callback => $callback
308 )
309 );
310 }
311  
312 0 ▶sub catchAll {
Subroutine catchAll: not called CC=1 SCAR=6.9
Pod undocumented
313 0 my ( $self, $callback ) = @_;
314 $self->addListener(
315 Hubot::Listener->new(
316 robot => $self,
317 0 ▶ matcher => sub { ref(shift) eq 'Hubot::CatchAllMessage' ? 1 : () },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
ref shift() eq 'Hubot::CatchAllMessage' ? : 0 0 0
318 0 ▶ callback => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
319 0 my $msg = shift;
320 0 $msg->message( $msg->message->message );
321 0 $callback->($msg);
322 }
323 )
324 0 );
325 }
326  
327 40 ▶sub receive {
Subroutine receive: called CC=5 SCAR=17.0
Pod undocumented
328 40 my ( $self, $message ) = @_;
329 40 my $results = [];
330 40 for my $listener ( $self->listeners ) {
331 168 eval $listener->call($message);
332 168 ▶ last if $message->done;
Branch: 1 branch
BranchTrueFalseTotal
if $message->done 0 168 168
333 168 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 168 168
334 0 print STDERR "Unable to call the listener: $@\n";
335 0 return 0;
336 }
337 }
338  
339 40 ▶ $self->receive( new Hubot::CatchAllMessage( message => $message ) )
Branch: 1 branch
BranchTrueFalseTotal
if ref $message ne "Hubot::CatchAllMessage" 20 20 40
340 if ref($message) ne 'Hubot::CatchAllMessage';
341 }
342  
343 4 ▶sub http { AnyEvent::HTTP::ScopedClient->new( $_[1] ) }
Subroutine http: called CC=1 SCAR=0.0
Pod undocumented
344  
345 6 __PACKAGE__->meta->make_immutable;
346  
347 6 1;
348  
349 =pod
350  
351 =encoding utf-8
352  
353 =head1 NAME
354  
355 Hubot::Robot
356  
357 =head1 VERSION
358  
359 version 0.2.8
360  
361 =head1 SYNOPSIS
362  
363 # Hubot::Robot has a CLI. named `hubot`
364 $ perldoc hubot
365  
366 # make sure `hubot-scripts.json` is exist in current working directory
367 use JSON::XS;
368 use Cwd 'cwd';
369 use Hubot::Robot;
370 my $robot = Hubot::Robot->new({
371 adapter => 'shell',
372 name => 'hubot'
373 });
374  
375 $robot->adapter->on(
376 'connected',
377 sub {
378 my $cwd = cwd();
379 my $scriptsFile = "$cwd/hubot-scripts.json";
380 if (-f $scriptsFile) {
381 my $json = read_file($scriptsFile);
382 my $scripts = decode_json($json);
383 $robot->loadHubotScripts($scripts);
384 }
385 }
386 );
387  
388 $robot->run;
389  
390 =head1 DESCRIPTION
391  
392 A customizable, kegerator-powered life embetterment robot.
393  
394 The original hubot description is..
395  
396 "This is a version of GitHub's Campfire bot, hubot. He's pretty cool."
397  
398 this is hubot B<Perl> port.
399  
400 =head1 SEE ALSO
401  
402 =over
403  
404 =item L<http://hubot.github.com/>
405  
406 =item L<https://github.com/github/hubot>
407  
408 =item L<hubot>
409  
410 =back
411  
412 =head1 AUTHOR
413  
414 Hyungsuk Hong <hshong@perl.kr>
415  
416 =head1 COPYRIGHT AND LICENSE
417  
418 This software is copyright (c) 2012 by Hyungsuk Hong.
419  
420 This is free software; you can redistribute it and/or modify it under
421 the same terms as the Perl 5 programming language system itself.
422  
423 =cut
« Hubot/EventEmitter.pm ↑ Summary Hubot/Adapter/Campfire.pm »