| 1 | package Jaipo::Config; | ||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Hash::Merge; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use YAML::Syck; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | Hash::Merge::set_behavior ('RIGHT_PRECEDENT'); | |||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Number::RecordLocator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use base qw/Class::Accessor::Fast/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | __PACKAGE__->mk_accessors (qw/stash last_sp_cnt/); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use vars qw/$CONFIG/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | sub new { | ||||||||||||||||||||||||||||
| 15 | 1 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 16 | 1 | my %args = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 19 | 1 | bless $self , $class; | |||||||||||||||||||||||||||
| 20 | 1 | $self->stash( {} ); | |||||||||||||||||||||||||||
| 21 | 1 | $self->last_sp_cnt ( 500000 + int( rand 100 ) ); | |||||||||||||||||||||||||||
| 22 | 1 | $self->load; | |||||||||||||||||||||||||||
| 23 | 1 | return $self; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub app_config_path { | ||||||||||||||||||||||||||||
| 27 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
app_config_path: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # XXX: get application config path for different platform | ||||||||||||||||||||||||||||
| 30 | # windows , unix or ... | ||||||||||||||||||||||||||||
| 31 | 1 | return $ENV{HOME} . '/.jaipo.yml'; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 4 | ▶ | sub app { return shift->_get( application => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
app: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | sub user { return shift->_get( user => @_ ) } | ||||||||||||||||||||||||||
|
Subroutine
user: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | # A teeny helper | ||||||||||||||||||||||||||||
| 39 | 4 | ▶ | sub _get { return $_[0]->stash->{ $_[1] }{ $_[2] } } | ||||||||||||||||||||||||||
|
Subroutine
_get: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub save { | ||||||||||||||||||||||||||||
| 43 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
save: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 44 | 1 | ▶ | my $config_filepath = shift || $self->app_config_path; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->app_config_path
MC/DC0%0/2
shift() || $self->app_config_path
shift()
$self->app_config_path
Truth table33%1/3
A || B
A= shift()B= $self->app_config_path
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 1 | open CONFIG_FH , ">" , $config_filepath; | |||||||||||||||||||||||||||
| 47 | 1 | print CONFIG_FH Dump( $self->stash ); | |||||||||||||||||||||||||||
| 48 | 1 | close CONFIG_FH; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | sub set_service_option { | ||||||||||||||||||||||||||||
| 53 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_service_option: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 1 | my $sp_name = shift; | |||||||||||||||||||||||||||
| 55 | 1 | my $opt = shift; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1 | my $new_config = $self->stash; | |||||||||||||||||||||||||||
| 58 | 1 | my @sps = @{ $self->app('Services') }; | |||||||||||||||||||||||||||
| 59 | 1 | ▶ | for( my $i=0; my $sp = $sps[ $i ] ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 1 | my $c_spname = join q{},keys %{ $sps[$i] } ; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | if( $c_spname eq $sp_name | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$c_spname eq $sp_name and $sp->{$c_spname}{"sp_id"} eq $opt->{"sp_id"}
MC/DC0%0/2
$c_spname eq $sp_name and $sp->{$c_spname}{"sp_id"} eq $opt->{"sp_id"}
$c_spname eq $sp_name
$sp->{$c_spname}{"sp_id"} eq $opt->{"sp_id"}
Truth table33%1/3
A and B
A= $c_spname eq $sp_nameB= $sp->{$c_spname}{"sp_id"} eq $opt->{"sp_id"}
| |||||||||||||||||||||||||||||
| 63 | and $sp->{ $c_spname }->{sp_id} eq $opt->{sp_id} ) | ||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||
| 65 | 1 | $new_config->{application}{Services}->[$i]->{ $c_spname } = $opt | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 1 | $self->stash( $new_config ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =head2 find_service_option_by_name | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Returns a config hash | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =cut | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | sub find_service_option_by_name { | ||||||||||||||||||||||||||||
| 78 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
find_service_option_by_name: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 79 | 1 | my $name = shift; | |||||||||||||||||||||||||||
| 80 | 1 | my @services = @{ $self->app ('Services') }; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # @services = grep { $name eq shift keys $_ }, @services; | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 1 | my @configs = (); | |||||||||||||||||||||||||||
| 85 | 1 | map { my ($p)=keys %$_; | |||||||||||||||||||||||||||
| 86 | 1 | ▶ | push @configs,values %$_ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | if $p =~ m/\Q$name/ } @services; | ||||||||||||||||||||||||||||
| 88 | 1 | ▶ | return wantarray ? @configs : $configs[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 find_service_option_by_trigger | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | in list context , returns an array of one or more service plugin | ||||||||||||||||||||||||||||
| 95 | config hash. | ||||||||||||||||||||||||||||
| 96 | in scalar contxt , returnes a config hash | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =cut | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub find_service_option_by_trigger { | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
find_service_option_by_trigger: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 0 | my $trigger = shift; | |||||||||||||||||||||||||||
| 103 | 0 | my @services = @{ $self->app ('Services') }; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # @services = grep { $name eq shift keys $_ }, @services; | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 0 | my @configs = (); | |||||||||||||||||||||||||||
| 108 | map { | ||||||||||||||||||||||||||||
| 109 | 0 | my ($v)=values %$_; | |||||||||||||||||||||||||||
| 110 | push @configs,$v | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if $v->{trigger_name} eq $trigger; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } @services; | ||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return wantarray ? @configs : $configs[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 load | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub load { | ||||||||||||||||||||||||||||
| 123 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
load: called CC=3 SCAR=11.7
Pod
documented
| |||||||||||||||||||||||||||||
| 124 | 1 | ▶ | my $config_filepath = shift || $self->app_config_path; | ||||||||||||||||||||||||||
|
Condition33%1/3
shift() || $self->app_config_path
MC/DC0%0/2
shift() || $self->app_config_path
shift()
$self->app_config_path
Truth table33%1/3
A || B
A= shift()B= $self->app_config_path
| |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | # if we can not find yml config file | ||||||||||||||||||||||||||||
| 127 | # load from default function , then write back to config file | ||||||||||||||||||||||||||||
| 128 | 1 | my $config; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 1 | ▶ | if ( -e $config_filepath ) { # XXX: check config version | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | $config = LoadFile( $config_filepath ); | |||||||||||||||||||||||||||
| 132 | 0 | $self->stash ($config); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | else { | ||||||||||||||||||||||||||||
| 135 | 1 | $config = $self->load_default_config; | |||||||||||||||||||||||||||
| 136 | 1 | $self->stash ($config); | |||||||||||||||||||||||||||
| 137 | # write back | ||||||||||||||||||||||||||||
| 138 | 1 | $self->save( $config_filepath ); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Canicalization | ||||||||||||||||||||||||||||
| 142 | 1 | $self->set_sp_id; | |||||||||||||||||||||||||||
| 143 | 1 | $self->set_default_trigger_name; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | # load user jaipo yaml config from file here | ||||||||||||||||||||||||||||
| 146 | 1 | return $config; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | sub set_default_trigger_name { | ||||||||||||||||||||||||||||
| 150 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_default_trigger_name: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 151 | 1 | my $new_config = $self->stash; | |||||||||||||||||||||||||||
| 152 | 1 | my @sps = @{ $self->app('Services') }; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | # set default trigger name for each service plugin that has no trigger name | ||||||||||||||||||||||||||||
| 155 | 1 | my %triggers; | |||||||||||||||||||||||||||
| 156 | 1 | ▶ | for( my $i=0; my $sp = $sps[$i] ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | 1 | my $c_spname = join q{},keys %{ $sp } ; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | my $tn = lc $c_spname; | |||||||||||||||||||||||||||
| 160 | 1 | ▶ | $tn .= '_' while( exists $triggers{ $tn } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | ▶ | $new_config->{application}{Services}->[$i]->{ $c_spname }->{trigger_name} ||= $tn; | ||||||||||||||||||||||||||
|
Condition33%1/3
$new_config->{"application"}{"Services"}[$i]{$c_spname}{"trigger_name"} ||= $tn
MC/DC0%0/2
$new_config->{"application"}{"Services"}[$i]{$c_spname}{"trigger_name"} ||= $tn
$new_config->{"application"}{"Services"}[$i]{$c_spname}{"trigger_name"}
$tn
Truth table33%1/3
A ||= B
A= $new_config->{"application"}{"Services"}[$i]{$c_spname}{"trigger_name"}B= $tn
| |||||||||||||||||||||||||||||
| 163 | 1 | $triggers{ $new_config->{application}{Services}->[$i]->{ $c_spname }->{trigger_name} } = 1; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1 | $self->stash( $new_config ); | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub set_sp_id { | ||||||||||||||||||||||||||||
| 170 | 1 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_sp_id: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 1 | my $sp_cnt = $self->last_sp_cnt; | |||||||||||||||||||||||||||
| 172 | 1 | my $new_config = $self->stash; | |||||||||||||||||||||||||||
| 173 | 1 | my @sps = @{ $self->app('Services') }; | |||||||||||||||||||||||||||
| 174 | 1 | my $num_rec = Number::RecordLocator->new; | |||||||||||||||||||||||||||
| 175 | 1 | ▶ | for( my $i=0; $i < scalar @sps ; $i++ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 1 | my $c_spname = join q{},keys %{ $sps[$i] } ; | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 1 | ▶ | if( ! defined $new_config->{application}{Services}->[$i]->{ $c_spname }->{sp_id} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | 1 | my $sp_id = $num_rec->encode( $sp_cnt ++ ); | |||||||||||||||||||||||||||
| 180 | 1 | $new_config->{application}{Services}->[$i]->{ $c_spname }->{sp_id} = $sp_id; | |||||||||||||||||||||||||||
| 181 | 1 | $self->last_sp_cnt( $sp_cnt ); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | 1 | $self->stash( $new_config ); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | sub load_default_config { | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # move this to a file later | ||||||||||||||||||||||||||||
| 190 | 2 | ▶ | my $config = <<YAML; | ||||||||||||||||||||||||||
|
Subroutine
load_default_config: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 191 | --- | ||||||||||||||||||||||||||||
| 192 | application: | ||||||||||||||||||||||||||||
| 193 | Verbose: 1 | ||||||||||||||||||||||||||||
| 194 | SavePassword: 0 | ||||||||||||||||||||||||||||
| 195 | Services: | ||||||||||||||||||||||||||||
| 196 | - Twitter: | ||||||||||||||||||||||||||||
| 197 | enable: 1 | ||||||||||||||||||||||||||||
| 198 | Plugins: {} | ||||||||||||||||||||||||||||
| 199 | user: {} | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | YAML | ||||||||||||||||||||||||||||
| 202 | 2 | return Load( $config ); | |||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 3 | 1; | |||||||||||||||||||||||||||