lib/Text/vCard/Addressbook.pm

filter: Statementstmt 99.0% 99 / 100 Branchbran 89.2% 25 / 28 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 100% 18 / 18 Podpod 100% 7 / 7 totaltotal 97.5% 160 / 164 CC 22 SCAR 12.7
CC
22
Cov
97%
new
7.0
_pre_process_text
6.0
load
4.0
CRAP 3.6
« blib/lib/vCard/AddressBook.pm ↑ Summary blib/lib/vCard/Role/FileIO.pm »
1 package Text::vCard::Addressbook;
2 15 $Text::vCard::Addressbook::VERSION = '3.09';
3 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Text::vFile::asData;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Text::vCard;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # See this module for your basic parser functions
11 15 ▶use base qw(Text::vFile::asData);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Text::vCard::Addressbook - Parse, edit, and create vCard address books (RFC 2426)
16  
17 =head1 WARNING
18  
19 L<vCard::AddressBook> is built on top of this module and provides a more
20 intuitive user interface. Please try that module first.
21  
22 =head1 SYNOPSIS
23  
24 use Text::vCard::Addressbook;
25  
26 # To read an existing address book file:
27  
28 my $address_book = Text::vCard::Addressbook->new({
29 'source_file' => '/path/to/address_book.vcf',
30 });
31  
32 foreach my $vcard ( $address_book->vcards() ) {
33 print "Got card for " . $vcard->fullname() . "\n";
34 }
35  
36 # To create a new address book file:
37  
38 my $address_book = Text::vCard::Addressbook->new();
39 my $vcard = $address_book->add_vcard;
40 $vcard->fullname('Foo Bar');
41 $vcard->EMAIL('foo@bar.com');
42  
43 open my $out, '>:encoding(UTF-8)', 'new_address_book.vcf' or die;
44 print $out $address_book->export;
45  
46  
47 =head1 DESCRIPTION
48  
49 This package provides an API to reading / editing and creating multiple vCards.
50 A vCard is an electronic business card. This package has been developed based
51 on rfc2426.
52  
53 You will find that many applications (Apple Address book, MS Outlook, Evolution
54 etc) can export and import vCards.
55  
56  
57 =head1 ENCODING AND UTF-8
58  
59 =head2 Constructor Arguments
60  
61 The 'encoding_in' and 'encoding_out' constructor arguments allow you to read
62 and write vCard files with any encoding. Examples of valid values are
63 'UTF-8', 'Latin1', and 'none'.
64  
65 Both values default to 'UTF-8' and this should just work for the vast majority
66 of people. The latest vCard RFC 6350 only allows UTF-8 as an encoding so most
67 people should not need to use either of these constructor arguments.
68  
69 =head2 MIME encodings
70  
71 vCard RFC 6350 only allows UTF-8 but it still permits 8bit MIME encoding
72 schemes such as Quoted-Printable and Base64 which are supported by this module.
73  
74 =head2 Manually setting values on a Text::vCard or Text::vCard::Node object
75  
76 If you manually set values on a Text::vCard or Text::vCard::Node object they
77 must be decoded values. The only exception to this rule is if you are messing
78 around with the 'encoding_out' constructor arg.
79  
80  
81 =head1 METHODS FOR LOADING VCARDS
82  
83 =head2 load()
84  
85 my $address_book = Text::vCard::Addressbook->load(
86 [ 'foo.vCard', 'Addresses.vcf' ], # list of files to load
87 );
88  
89 This method will croak if it is unable to read in any of the files.
90  
91 =cut
92  
93 8 ▶sub load {
Subroutine load: called CC=4 SCAR=13.9
Pod documented
94 8 my ( $proto, $filenames, $constructor_args ) = @_;
95  
96 8 my $self = __PACKAGE__->new($constructor_args);
97  
98 8 foreach my $filename ( @{$filenames} ) {
99  
100 8 ▶ croak "Unable to read file $filename" unless -r $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $filename 1 7 8
101  
102 7 my $file = $self->_path($filename);
103 7 my $string = $file->slurp( $self->_iomode_in );
104  
105 7 ▶ die <<EOS
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\r\n/m 0 7 7
106 ERROR: Either there is no END in this vCard or there is a problem with the line
107 endings. Note that the vCard RFC requires line endings delimited by \\r\\n
108 regardless of your operating system. Windows :crlf mode will strip out the \\r
109 so don't use that.
110 EOS
111 unless $string =~ m/\r\n/m;
112  
113 7 $self->import_data($string);
114 }
115  
116 7 return $self;
117  
118 }
119  
120 =head2 import_data()
121  
122 $address_book->import_data($string);
123  
124 This method imports data directly from a string. $string is assumed to be
125 decoded (but not MIME decoded).
126  
127 =cut
128  
129 7 ▶sub import_data {
Subroutine import_data: called CC=1 SCAR=0.0
Pod documented
130 7 my ( $self, $value ) = @_;
131  
132 7 $self->_process_text($value);
133 }
134  
135 =head2 new()
136  
137 # Create a new (empty) address book
138 my $address_book = Text::vCard::Addressbook->new();
139
140 # Load vcards from a single file
141 my $address_book = Text::vCard::Addressbook->new({
142 source_file => '/path/to/address_book.vcf'
143 });
144  
145 # Load vcards from a a string
146 my $address_book = Text::vCard::Addressbook->new({
147 source_text => $source_text
148 });
149  
150 This method will croak if it is unable to read the source_file.
151  
152 The constructor accepts 'encoding_in' and 'encoding_out' attributes. The
153 default values for both are 'UTF-8'. You can set them to 'none' if
154 you don't want your output encoded with Encode::encode(). But be aware the
155 latest vCard RFC 6350 mandates UTF-8.
156  
157 =cut
158  
159 33 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
160 33 my ( $proto, $conf ) = @_;
161 33 ▶ my $class = ref($proto) || $proto;
Condition100%3/3
ref $proto || $proto
l!l&&r!l&&!r
1311
MC/DC100%2/2
ref $proto || $proto
ref $proto $proto
Truth table100%3/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
162 33 my $self = {};
163  
164 33 bless( $self, $class );
165  
166 # create some where to store out individual vCard objects
167 32 $self->{'cards'} = [];
168 32 ▶ $self->{encoding_in} = $conf->{encoding_in} || 'UTF-8';
Condition100%2/2
$conf->{'encoding_in'} || 'UTF-8'
l!l
725
MC/DC100%1/1
$conf->{'encoding_in'} || 'UTF-8'
$conf->{'encoding_in'}
Truth table100%2/2
A
A= $conf->{'encoding_in'}
Aresult
11
00
169 32 ▶ $self->{encoding_out} = $conf->{encoding_out} || 'UTF-8';
Condition100%2/2
$conf->{'encoding_out'} || 'UTF-8'
l!l
725
MC/DC100%1/1
$conf->{'encoding_out'} || 'UTF-8'
$conf->{'encoding_out'}
Truth table100%2/2
A
A= $conf->{'encoding_out'}
Aresult
11
00
170  
171 # slurp in file contents
172 32 ▶ if ( defined $conf->{'source_file'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $conf->{"source_file"}) 13 19 32
173  
174 croak "Unable to read file $conf->{'source_file'}\n"
175 13 ▶ unless -r $conf->{'source_file'};
Branch: 1 branch
BranchTrueFalseTotal
unless -r $conf->{"source_file"} 1 12 13
176  
177 12 my $filename = $conf->{source_file};
178 12 my $file = $self->_path($filename);
179 12 $conf->{source_text} = $file->slurp( $self->_iomode_in );
180 }
181  
182 # Process the text if we have it.
183 $self->_process_text( $conf->{'source_text'} )
184 31 ▶ if defined $conf->{'source_text'};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conf->{"source_text"} 12 19 31
185  
186 30 return $self;
187 }
188  
189 =head1 OTHER METHODS
190  
191 =head2 add_vcard()
192  
193 my $vcard = $address_book->add_vcard();
194  
195 This method creates a new empty L<Text::vCard> object, stores it in the
196 address book and return it so you can add data to it.
197  
198 =cut
199  
200 3 ▶sub add_vcard {
Subroutine add_vcard: called CC=1 SCAR=0.0
Pod documented
201 3 my $self = shift;
202 3 my $vcard = Text::vCard->new( { encoding_out => $self->{encoding_out} } );
203 3 push( @{ $self->{cards} }, $vcard );
204 3 return $vcard;
205 }
206  
207 =head2 vcards()
208  
209 my $vcards = $address_book->vcards();
210 my @vcards = $address_book->vcards();
211  
212 This method returns a reference to an array or an array of
213 vcards in this address book. This could be an empty list
214 if there are no entries in the address book.
215  
216 =cut
217  
218 27 ▶sub vcards {
Subroutine vcards: called CC=2 SCAR=6.9
Pod documented
219 27 my $self = shift;
220 27 ▶ return wantarray ? @{ $self->{cards} } : $self->{cards};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 13 14 27
221 }
222  
223 =head2 set_encoding()
224  
225 DEPRECATED. Use the 'encoding_in' and 'encoding_out' constructor arguments.
226  
227 =cut
228  
229 1 ▶sub set_encoding {
Subroutine set_encoding: called CC=2 SCAR=7.4
Pod documented
230 1 my ( $self, $coding ) = @_;
231 1 $self->{'encoding'} |= '';
232 1 ▶ $self->{'encoding'} = ";charset=$coding" if ( defined $coding );
Branch: 1 branch
BranchTrueFalseTotal
if defined $coding 1 0 1
233 1 return $self->{'encoding'};
234 0 die "DEPRECATED. Use the 'encoding_in' and 'encoding_out'"
235 . " constructor arguments";
236 }
237  
238 =head2 export()
239  
240 my $string = $address_book->export()
241  
242 This method returns the vcard data as a string in the vcf file format.
243  
244 Please note there is no validation, you must ensure that the correct nodes
245 (FN,N,VERSION) are already added to each vcard if you want to comply with
246 RFC 2426.
247  
248 =cut
249  
250 9 ▶sub export {
Subroutine export: called CC=2 SCAR=6.9
Pod documented
251 9 my $self = shift;
252 9 my $string = '';
253 9 $string .= $_->as_string for $self->vcards;
254 9 return $string;
255 }
256  
257 # PRIVATE METHODS
258  
259 # PerlIO layers should look like ':encoding(UTF-8)'
260 # The ':encoding()' part does character set and encoding transformations.
261 # Without it you are just declaring the stream to be of a certain encoding.
262 # See PerlIO, PerlIO::encoding docs.
263 19 ▶sub _iomode_in {
Subroutine _iomode_in: called CC=2 SCAR=6.9
264 19 my ($self) = @_;
265 19 ▶ return { binmode => ':raw' } if $self->{encoding_in} eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"encoding_in"} eq "none" 1 18 19
266 18 return { binmode => ':raw:encoding(' . $self->{encoding_in} . ')' };
267 }
268  
269 # Filename can be a string, a Path::Tiny obj, or a Path::Class obj.
270 # Returns a Path::Tiny obj.
271 19 ▶sub _path {
Subroutine _path: called CC=2 SCAR=7.2
272 19 my ( $self, $filename ) = @_;
273 19 ▶ return ref $filename eq 'Path::Class::File' #
Branch: 1 branch
BranchTrueFalseTotal
ref $filename eq 'Path::Class::File' ? : 0 19 19
274 ? path("$filename")
275 : path($filename); # works for strings and Path::Tiny objects
276 }
277  
278 # Process a chunk of text, create Text::vCard objects and store in the address book
279 25 ▶sub _pre_process_text {
Subroutine _pre_process_text: called CC=6 SCAR=17.9
280 25 my ( $self, $text ) = @_;
281  
282 25 ▶ if ( $text =~ /quoted-printable/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /quoted-printable/i) 5 20 25
283  
284 # Edge case for 2.1 version
285 #
286 # http://tools.ietf.org/html/rfc2045#section-6.7 point (5),
287 # lines containing quoted-printable encoded data can contain soft line
288 # breaks. These are indicated as single '=' sign at the end of the
289 # line.
290 #
291 # No longer needed in version 3.0:
292 # http://tools.ietf.org/html/rfc2426 point (5)
293 #
294 # 'perldoc perlport' says using \r\n is wrong and confusing for a few
295 # reasons but mainly because the value of \n is different on different
296 # operating systems. It recommends \x0D\x0A instead.
297  
298 5 my $out;
299 5 my $inside = 0;
300 5 foreach my $line ( split( "\x0D\x0A", $text ) ) {
301  
302 61 ▶ if ($inside) {
Branch: 1 branch
BranchTrueFalseTotal
if ($inside) 10 51 61
303 10 ▶ if ( $line =~ /=$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /=$/) { } 1 9 10
304 1 $line =~ s/=$//;
305 } else {
306 9 $inside = 0;
307 }
308 }
309  
310 61 ▶ if ( $line =~ /ENCODING=QUOTED-PRINTABLE/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /ENCODING=QUOTED-PRINTABLE/i) 9 52 61
311 9 $inside = 1;
312 9 $line =~ s/=$//;
313 }
314 61 $out .= $line . "\x0D\x0A";
315 }
316 5 $text = $out;
317  
318 }
319  
320 # Add error checking here ?
321 25 my $asData = Text::vFile::asData->new;
322 25 $asData->preserve_params(1);
323  
324 25 my @lines = split "\x0D\x0A", $text;
325 25 my @lines_with_newlines = map { $_ . "\x0D\x0A" } @lines;
326 25 return $asData->parse_lines(@lines_with_newlines)->{objects};
327 }
328  
329 19 ▶sub _process_text {
Subroutine _process_text: called CC=3 SCAR=11.0
330 19 my ( $self, $text ) = @_;
331  
332 19 my $cards = $self->_pre_process_text($text);
333  
334 19 foreach my $card (@$cards) {
335  
336 # Run through each card in the data
337 21 ▶ if ( $card->{'type'} =~ /VCARD/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->{'type'} =~ /VCARD/i) { } 20 1 21
338 my $vcard = Text::vCard->new(
339 { 'asData_node' => $card->{'properties'},
340 encoding_in => $self->{encoding_in},
341 encoding_out => $self->{encoding_out}
342 }
343 20 );
344 20 push( @{ $self->{'cards'} }, $vcard );
345 } else {
346 1 carp
347 "This file contains $card->{'type'} data which was not parsed";
348 }
349 }
350  
351 18 return $self->{cards};
352 }
353  
354 =head1 AUTHOR
355  
356 Leo Lapworth, LLAP@cuckoo.org
357 Eric Johnson (kablamo), github ~!at!~ iijo dot org
358  
359 =head1 COPYRIGHT
360  
361 Copyright (c) 2003 Leo Lapworth. All rights reserved.
362 This program is free software; you can redistribute
363 it and/or modify it under the same terms as Perl itself.
364  
365 =head1 ACKNOWLEDGEMENTS
366  
367 The authors of L<Text::vFile::asData> for making my life so much easier.
368  
369 =head1 SEE ALSO
370  
371 L<Text::vCard>, L<Text::vCard::Node>
372  
373 =cut
374  
375 15 1;
« blib/lib/vCard/AddressBook.pm ↑ Summary blib/lib/vCard/Role/FileIO.pm »