File Coverage

blib/lib/App/Automaton.pm
Criterion Covered Total %
statement 39 88 44.3
branch 5 28 17.8
condition n/a
subroutine 10 13 76.9
pod 6 7 85.7
total 60 136 44.1


line stmt bran cond sub pod time code
1             package App::Automaton;
2              
3             # ABSTRACT: Execute various tasks based on input from various sources
4              
5 1     1   420 use strict;
  1         1  
  1         24  
6 1     1   3 use warnings;
  1         1  
  1         22  
7              
8 1     1   388 use Moo;
  1         8847  
  1         6  
9 1     1   1342 use YAML::Tiny;
  1         3736  
  1         56  
10 1     1   485 use Module::Load;
  1         652  
  1         5  
11              
12 1     1   482 use Data::Dumper;
  1         4160  
  1         522  
13              
14             has conf => ( is => 'rw' );
15             has yaml_conf => ( is => 'rw' );
16             has conf_file => ( is => 'rw' );
17             has found_bits => ( is => 'rw' );
18             has debug => ( is => 'rw');
19              
20             sub BUILD {
21 2     2 0 3548 my $self = shift;
22 2         4 $self->load_conf();
23 2         33 return 1;
24             }
25              
26             sub load_conf {
27 2     2 1 3 my $self = shift;
28            
29 2 100       10 if ( $self->{yaml_conf} ) {
    50          
30 1         4 $self->{conf} = YAML::Tiny::Load($self->{yaml_conf});
31             } elsif ( $self->{conf_file} ) {
32 1 50       3 $self->{conf} = YAML::Tiny::LoadFile($self->{conf_file}) or die;
33             }
34            
35 2         10143 $self->logger('Loaded config');
36 2         2 return $self->{conf};
37             }
38              
39             sub check_sources {
40 0     0 1 0 my $self = shift;
41            
42 0         0 my $sources = $self->{conf}->{sources};
43 0         0 foreach my $name (keys %$sources) {
44 0         0 my $source = $sources->{$name};
45 0 0       0 next if $source->{bypass};
46 0         0 $source->{debug} = $self->{debug};
47 0         0 $self->logger("checking source: $name");
48 0         0 my $mod = 'App::Automaton::Plugin::Source::' . $source->{type};
49 0         0 load $mod;
50 0         0 my $s = eval {$mod->new()};
  0         0  
51 0 0       0 die $! unless $s;
52 0 0       0 die $! unless $s->can('go');
53 0         0 push(@{$self->{found_bits}}, $s->go($source));
  0         0  
54             }
55              
56 0         0 return $1;
57             }
58              
59             sub apply_filters {
60 0     0 1 0 my $self = shift;
61            
62 0         0 my $filters = $self->{conf}{filters};
63 0         0 foreach my $name (keys %{$filters}) {
  0         0  
64 0         0 my $filter = $filters->{$name};
65 0 0       0 next if $filter->{bypass};
66 0         0 $filter->{debug} = $self->{debug};
67 0         0 $self->logger("Applying filter: $name");
68 0         0 my $mod = 'App::Automaton::Plugin::Filter::' . $filter->{type};
69 0         0 load $mod;
70 0         0 my $a = eval {$mod->new()};
  0         0  
71 0 0       0 die $! unless $a;
72 0 0       0 die $! unless $a->can('go');
73 0         0 $a->go($filter, $self->{found_bits});
74             }
75            
76 0         0 return 1;
77             }
78              
79             sub do_actions {
80 0     0 1 0 my $self = shift;
81             # process each action for each buffer
82              
83 0         0 my $actions = $self->{conf}{actions};
84 0         0 foreach my $name (keys %$actions) {
85 0         0 my $action = $actions->{$name};
86 0 0       0 next if $action->{bypass};
87 0         0 $action->{debug} = $self->{debug};
88 0         0 $self->logger("Executing action: $name");
89 0         0 my $mod = 'App::Automaton::Plugin::Action::' . $action->{type};
90 0         0 load $mod;
91 0         0 my $a = eval {$mod->new()};
  0         0  
92 0 0       0 die $! unless $a;
93 0 0       0 die $! unless $a->can('go');
94 0         0 my $r = $a->go($action, $self->found_bits());
95 0 0       0 $self->logger("Unsuccessful return from action: $name") unless $r;
96             }
97            
98 0         0 return 1;
99             }
100              
101             sub dedupe {
102 1     1 1 1066 my $self = shift;
103 1         1 my %hash;
104 1         3 $self->logger("Removing duplicates");
105 1         2 @hash{@{$self->{found_bits}}} = ();
  1         4  
106 1         3 @{$self->{found_bits}} = keys %hash;
  1         2  
107 1         3 return 1;
108             }
109              
110             sub logger {
111 3     3 1 4 my $self = shift;
112 3         6 my $message = shift;
113 3 50       9 if ($self->{debug}) {
114 0         0 print STDERR "$message\n";
115             }
116 3         2 return 1;
117             }
118              
119             1;
120              
121             __END__