OFun/CodeName.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 105 CC 4 SCAR 15.5
CC
4
Cov
0%
assign_codename
12.0
generate_code_name
6.0
load_schema
2.0
CRAP 4.7
1 package Bot::Backbone::Service::OFun::CodeName;
2 $Bot::Backbone::Service::OFun::CodeName::VERSION = '0.142230';
3 use Bot::Backbone::Service;
4  
5 with qw(
6 Bot::Backbone::Service::Role::Service
7 Bot::Backbone::Service::Role::Responder
8 Bot::Backbone::Service::Role::Storage
9 );
10  
11 use File::Slurp qw( read_file );
12 use MooseX::Types::Path::Class;
13 use Digest::SHA qw( sha1 );
14 use List::Util qw( reduce );
15  
16 # ABSTRACT: Assign code names to words and phrases for fun and profit
17  
18  
19 service_dispatcher as {
20 command '!codename' => given_parameters {
21 parameter 'phrase' => ( match_original => qr/.+/ );
22 } respond_by_method 'assign_codename';
23 };
24  
25  
26 for my $part (qw( adjective noun )) {
27 my $part_file = "${part}s_file";
28 has $part_file => (
29 is => 'ro',
30 isa => 'Path::Class::File',
31 required => 1,
32 coerce => 1,
33  
34 );
35  
36 __PACKAGE__->meta->add_method("_build_${part}s" => sub {
37 my $self = shift;
38 my @words = map { chomp; s/\s+$//; s/^\s+//; $_ } read_file($self->$part_file);
39 return \@words;
40 });
41  
42 has "${part}s" => (
43 is => 'ro',
44 isa => 'ArrayRef[Str]',
45 lazy_build => 1,
46 traits => [ 'Array' ],
47 handles => {
48 "get_${part}" => 'get',
49 "${part}s_count" => 'count',
50 },
51 );
52 }
53  
54  
55 sub load_schema {
56 my ($self, $conn) = @_;
57  
58 $conn->run(fixup => sub {
59 $_->do(q[
60 CREATE TABLE IF NOT EXISTS codenames(
61 name varchar(255),
62 alias varchar(255),
63 is_code_name integer,
64 PRIMARY KEY (name)
65 )
66 ]);
67 });
68 }
69  
70  
71 sub assign_codename {
72 my ($self, $message) = @_;
73  
74 my $phrase = lc $message->parameters->{phrase};
75 $phrase =~ s/^\s+//;
76 $phrase =~ s/\s+$//;
77 $phrase =~ s/\s+/ /g;
78  
79 my $alias = $self->find_key($phrase);
80 if (defined $alias) {
81 return $alias;
82 }
83  
84 my $code_name = $self->generate_code_name($phrase);
85 if ($code_name) {
86 $self->store_key($code_name => $phrase, 1);
87 $self->store_key($phrase => $code_name, 0);
88  
89 return $code_name;
90 }
91 else {
92 return "Too many duplicates. Can't come up with a code name for that. Maybe you need to expand your adjectives or nouns list.";
93 }
94 }
95  
96  
97 sub generate_code_name {
98 my ($self, $phrase) = @_;
99 my $try_phrase = $phrase;
100  
101 my $max_tries = 5;
102 TRY: while ($max_tries >= 0) {
103 my $inv_phrase = reverse $try_phrase;
104  
105 my $raw_adj_index = reduce { $a ^ $b } unpack "L*", sha1($try_phrase);
106 my $raw_noun_index = reduce { $a ^ $b } unpack "L*", sha1($inv_phrase);
107  
108 my $adj_index = $raw_adj_index % $self->adjectives_count;
109 my $noun_index = $raw_noun_index % $self->nouns_count;
110  
111 my $adjective = $self->get_adjective($adj_index);
112 my $noun = $self->get_noun($noun_index);
113 my $code_name = join ' ', $adjective, $noun;
114  
115 # Duplicate check
116 my $pair = $self->find_key($code_name);
117 if ($pair) {
118 $try_phrase = $try_phrase . '\0' . $phrase;
119 $max_tries--;
120 next TRY;
121 }
122  
123 return $code_name;
124 }
125  
126 return;
127 }
128  
129  
130 sub find_key {
131 my ($self, $key) = @_;
132  
133 my ($alias) = $self->db_conn->run(fixup => sub {
134 my $sth = $_->prepare(q[
135 SELECT alias
136 FROM codenames
137 WHERE name = ?
138 ]);
139 $sth->execute($key);
140 $sth->fetchrow_array;
141 });
142  
143 return $alias;
144 }
145  
146  
147 sub store_key {
148 my ($self, $key, $alias, $iscn) = @_;
149  
150 $self->db_conn->run(fixup => sub {
151 $_->do(q[
152 INSERT INTO codenames(name, alias, is_code_name)
153 VALUES (?, ?, ?)
154 ], undef, $key, $alias, $iscn);
155 });
156 }
157  
158  
159 sub initialize { }
160  
161 __PACKAGE__->meta->make_immutable;
162  
163 __END__