File Coverage

blib/lib/App/TeleGramma/Plugin/Base.pm
Criterion Covered Total %
statement 36 55 65.4
branch 2 8 25.0
condition 2 2 100.0
subroutine 10 15 66.6
pod 9 11 81.8
total 59 91 64.8


line stmt bran cond sub pod time code
1             package App::TeleGramma::Plugin::Base;
2             $App::TeleGramma::Plugin::Base::VERSION = '0.14';
3             # ABSTRACT: Base class for TeleGramma plugins
4              
5 4     4   884 use Mojo::Base -base;
  4         8  
  4         28  
6              
7 4     4   1992 use App::TeleGramma::Constants qw/:const/;
  4         11  
  4         519  
8 4     4   1351 use App::TeleGramma::Store;
  4         9  
  4         30  
9 4     4   126 use File::Spec::Functions qw/catdir/;
  4         8  
  4         2090  
10              
11             has 'app_config';
12             has 'app';
13             has '_store';
14              
15              
16             sub truncated_package_name {
17 0     0 1 0 my $self = shift;
18 0         0 my $package = ref($self);
19 0         0 $package =~ s/^App::TeleGramma::Plugin:://;
20 0         0 return $package;
21             }
22              
23              
24             sub short_name {
25 36     36 1 44 my $self = shift;
26 36         63 my $package = ref($self);
27 36         142 $package =~ s/^App::TeleGramma::Plugin:://;
28 36         97 $package =~ s/::/-/g;
29              
30 36         79 return "plugin-" . $package;
31             }
32              
33              
34             # override: optional
35 1     1 1 3 sub default_config { {} }
36              
37             # override: optional
38             sub check_prereqs {
39 12     12 0 40 1;
40             }
41              
42              
43             sub synopsis {
44 0     0 1 0 "My author did not provide a synopsis!";
45             }
46              
47              
48             sub register {
49 0     0 1 0 my $self = shift;
50 0         0 die ref($self) . " did not supply a register method\n";
51             }
52              
53             sub create_default_config_if_necessary {
54 12     12 0 18 my $self = shift;
55 12         31 my $section = $self->short_name();
56              
57 12         34 $self->app_config->read();
58              
59 12 100       13370 if (! %{ $self->read_config }) {
  12         52  
60 6         79 $self->app_config->config->{$section} = $self->default_config;
61 6         38 $self->app_config->config->{$section}->{enable} = 'no';
62 6         35 $self->app_config->write();
63             }
64             }
65              
66              
67             sub read_config {
68 24     24 1 43 my $self = shift;
69 24         39 my $section = $self->short_name();
70 24         52 $self->app_config->read();
71 24   100     27964 return $self->app_config->config->{$section} || {};
72             }
73              
74              
75             sub reply_to {
76 7     7 1 13 my $self = shift;
77 7         12 my $msg = shift;
78 7         10 my $reply = shift;
79              
80 7         21 my $app = $self->app;
81              
82 7         40 $app->send_message_to_chat_id($msg->chat->id, $reply);
83             }
84              
85              
86             sub data_dir {
87 0     0 1   my $self = shift;
88 0           my $data_dir = catdir($self->app_config->path_plugin_data, $self->short_name);
89 0 0         mkdir $data_dir unless -d $data_dir;
90 0           return $data_dir;
91             }
92              
93              
94             sub store {
95 0     0 1   my $self = shift;
96 0 0         return $self->_store if ($self->_store);
97 0           my $data_dir = $self->data_dir;
98              
99 0           my $store_dir = catdir($data_dir, 'store');
100 0 0         mkdir $store_dir unless -d $store_dir;
101              
102 0           my $store = App::TeleGramma::Store->new(path => $store_dir);
103 0           $self->_store($store);
104 0           return $store;
105             }
106              
107             1;
108              
109             __END__
110              
111             =pod
112              
113             =encoding UTF-8
114              
115             =head1 NAME
116              
117             App::TeleGramma::Plugin::Base - Base class for TeleGramma plugins
118              
119             =head1 VERSION
120              
121             version 0.14
122              
123             =head1 METHODS
124              
125             =head2 truncated_package_name
126              
127             Provide the name of the plugin, in perl form (hierarchy delimited with '::')
128             but without the leading C<App::TeleGramma::Plugin>.
129              
130             =head2 short_name
131              
132             Provide the name of the plugin, with the '::' separators changed to '-', and
133             the leading 'App::TeleGramma::Plugin::' removed.
134              
135             =head2 default_config
136              
137             Override this method in your subclass if you want to provide a default
138             configuration for your plugin (apart from the "enabled" flag).
139              
140             =head2 synopsis
141              
142             Override this method to provide a one-line synopsis of your plugin.
143              
144             =head2 register
145              
146             Override this method to register your plugin. It must setup any required
147             listeners. See L<App::TeleGramma::Plugin::Core::Fortune> for an example.
148              
149             =head2 read_config
150              
151             Read the configuration specific to this plugin.
152              
153             Returns a hashref
154              
155             =head2 reply_to
156              
157             Reply to a message, with text.
158              
159             Should be provided the Telegram::Bot::Message object, and the text string
160             to respond with.
161              
162             =head2 data_dir
163              
164             Returns the path on disk that your plugin should store any data.
165              
166             =head2 store
167              
168             Returns an L<App::TeleGramma::Store> object for you to persist your plugin data.
169              
170             =head1 AUTHOR
171              
172             Justin Hawkins <justin@hawkins.id.au>
173              
174             =head1 COPYRIGHT AND LICENSE
175              
176             This software is copyright (c) 2019 by Justin Hawkins <justin@eatmorecode.com>.
177              
178             This is free software; you can redistribute it and/or modify it under
179             the same terms as the Perl 5 programming language system itself.
180              
181             =cut