File Coverage

blib/lib/BBDB/Export.pm
Criterion Covered Total %
statement 67 102 65.6
branch 16 40 40.0
condition 7 12 58.3
subroutine 10 12 83.3
pod 7 7 100.0
total 107 173 61.8


line stmt bran cond sub pod time code
1             package BBDB::Export;
2 3     3   104378 use strict;
  3         8  
  3         126  
3 3     3   17 use warnings;
  3         7  
  3         207  
4              
5             our $VERSION = '0.015';
6              
7              
8             #
9             #_* Config
10             #
11              
12             # TODO: make this configurable
13             my %colors = (
14             info => 'green',
15             command => 'bold yellow',
16             error => 'red',
17             verbose => 'blue',
18             );
19              
20             #
21             #_* Libraries
22             #
23 3     3   1949 use BBDB;
  3         15378  
  3         176  
24 3     3   4726 use Term::ANSIColor;
  3         35431  
  3         352  
25 3     3   3706 use Data::Dumper;
  3         31892  
  3         3654  
26              
27             #
28             #_* new
29             #
30              
31             sub new
32             {
33 28     28 1 12284 my ( $class, $data ) = @_;
34              
35 28         70 my $objref = ( { data => $data } );
36              
37 28         69 bless $objref, $class;
38              
39 28         65 return $objref;
40             }
41              
42             #
43             #_* get_record_hash
44             #
45             sub get_record_hash
46             {
47 28     28 1 6417 my ( $self, $record ) = @_;
48              
49 28 50       59 return unless ( $record );
50              
51             # store record data
52 28         31 my %record;
53              
54             # get entire data structure
55 28         77 my $data = $record->part('all');
56              
57             # first/last names
58 28         248 $record{'first'} = $data->[0];
59 28         54 $record{'last'} = $data->[1];
60 28         76 $record{'full' } = join( " ", ( $record{'first'}, $record{'last'} ) );
61 28         109 $record{'full' } =~ s|^\s+||;
62 28         81 $record{'full' } =~ s|\s+$||;
63              
64             # nicks
65 28 100 66     142 @{$record{'aka'}} = @{ $data->[2] } if $data->[2] && $data->[2]->[0];
  4         10  
  4         8  
66              
67             # company
68 28         58 $record{'company'} = $data->[3];
69              
70             # phone numbers
71 28 100 66     120 if ( $data->[4] && $data->[4]->[0] )
72             {
73 8         7 for my $phone ( @{ $data->[4] } )
  8         18  
74             {
75 20         25 my $loc = $phone->[0];
76              
77 20         20 my @numbers = @{ $phone->[1] };
  20         46  
78 20         20 pop @numbers;
79              
80 20         20 my $number;
81              
82 20 50       35 if ( $#numbers == 2 )
83             {
84 20         41 $number = "(" . $numbers[0] . ") " . $numbers[1] . "-" . $numbers[2];
85             }
86             else
87             {
88 0         0 $number = join( "-", @numbers );
89             }
90              
91 20         62 $record{'phone'}->{ $loc } = $number;
92             }
93             }
94              
95             # TODO: addresses
96              
97             # nicks
98 28 100 66     115 @{$record{'net'}} = @{ $data->[6] } if $data->[6] && $data->[6]->[0];
  8         23  
  8         16  
99              
100              
101             # notes
102 28 50 33     107 if ( $data->[7] && $data->[7]->[0] )
103             {
104 28         28 for my $note ( @{ $data->[7] } )
  28         49  
105             {
106 72         174 $record{ $note->[0] } = $note->[1];
107             }
108             }
109              
110 28         96 return \%record;
111             }
112              
113             #
114             #_* process_record
115             #
116              
117              
118             #
119             #_* export
120             #
121             sub export
122             {
123 21     21 1 87 my ( $self ) = @_;
124              
125 21 50       70 unless ( $self->{'data'}->{'bbdb_file'} )
126             {
127 0         0 $self->error( "export called but bbdb_file not specified" );
128 0         0 return;
129             }
130              
131 21 50       380 unless ( -r $self->{'data'}->{'bbdb_file'} )
132             {
133 0         0 $self->error( "export called but bbdb_file not readable" );
134 0         0 return;
135             }
136              
137 21         31 my $return = "";
138              
139 21         25 for my $record ( @{BBDB::simple( $self->{'data'}->{'bbdb_file'} )} )
  21         70  
140             {
141 21         4143 my $record_hash = $self->get_record_hash( $record );
142 21 50       48 unless ( $record_hash )
143             {
144 0         0 $self->error( "No data returned from record" );
145 0         0 next;
146             }
147              
148 21         79 my ( $output ) = $self->process_record( $record_hash );
149 21 100       125 $return .= $output if $output;
150             }
151              
152 21         138 $self->post_processing( $return );
153              
154 21         49 return $return;
155             }
156              
157              
158             #
159             #_* output
160             #
161              
162             sub info
163             {
164 7     7 1 15 my ( $self, @info ) = @_;
165              
166 7 50       30 return if $self->{'data'}->{'quiet'};
167              
168 0         0 print color $colors{'info'};
169 0 0       0 print join ( "\n", @info ) if $info[0];
170 0         0 print color 'reset';
171 0         0 print "\n";
172             }
173              
174             sub error
175             {
176 7     7 1 12 my ( $self, @error ) = @_;
177              
178 7 50       27 return if $self->{'data'}->{'quiet'};
179              
180 0           print color $colors{'error'};
181 0 0         print join ( "\n", @error ) if $error[0];
182 0           print color 'reset';
183 0           print "\n";
184             }
185              
186             sub verbose
187             {
188 0     0 1   my ( $self, @verbose ) = @_;
189              
190 0 0         return if $self->{'data'}->{'quiet'};
191 0 0         return unless $self->{'verbose'};
192              
193 0           print color $colors{'verbose'};
194 0 0         print join ( "\n", @verbose ) if $verbose[0];
195 0           print color 'reset';
196 0           print "\n";
197             }
198              
199             #
200             #_* spawn external command, e.g. ldapadd
201             #
202             sub run_command
203             {
204 0     0 1   my ( $self, $command, $supress_error ) = @_;
205              
206 0           $self->verbose("COMMAND: $command");
207              
208 0           open( my $run_fh, "-|", "$command 2>&1" );
209 0           my $out = join( "\n", <$run_fh> );
210              
211 0 0         if ( close $run_fh )
212             {
213 0 0         if ( $self->{'verbose'} )
214             {
215 0           print color $colors{'command'};
216 0           print $out;
217 0           print color 'reset';
218             }
219              
220 0           return $out;
221             }
222             else
223             {
224 0 0         unless ( $self->{'data'}->{'supress_error'} )
225             {
226 0           $self->error( $out );
227             }
228              
229 0           return;
230             }
231             }
232              
233              
234             1;
235             __END__