blib/lib/vCard/AddressBook.pm

filter: Statementstmt 100% 113 / 113 Branchbran 75.0% 12 / 16 Conditioncond 42.8% 15 / 35 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 100% 20 / 20 Podpod 100% 5 / 5 totaltotal 78.9% 165 / 209 CC 30 SCAR 17.7
CC
30
Cov
85%
_copy_addresses
13.2
_copy_name
9.4
_copy_simple_nodes
5.1
CRAP 5.9
« lib/Text/vCard.pm ↑ Summary lib/Text/vCard/Addressbook.pm »
1 package vCard::AddressBook;
2 3 $vCard::AddressBook::VERSION = '3.09';
3 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use vCard;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Text::vCard;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Text::vCard::Addressbook;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 vCard::AddressBook - Read, write, and edit vCard address books
13  
14 =head1 SYNOPSIS
15  
16 use vCard::AddressBook;
17  
18 # create the object
19 my $address_book = vCard::AddressBook->new();
20  
21 # these methods load vCard formatted data
22 $address_book->load_file('/path/file.vcf');
23 $address_book->load_string($string);
24  
25 my $vcard = $address_book->add_vcard; # returns a vCard object
26 $vcard->full_name('Bruce Banner, PhD');
27 $vcard->family_names(['Banner']);
28 $vcard->given_names(['Bruce']);
29 $vcard->email_addresses([
30 { type => ['work'], address => 'bbanner@ssh.secret.army.mil' },
31 { type => ['home'], address => 'bbanner@timewarner.com' },
32 ]);
33  
34 # $address_book->vcards() returns an arrayref of vCard objects
35 foreach my $vcard (@{ $address_book->vcards() }) {
36 print $vcard->full_name() . "\n";
37 print $vcard->email_addresses->[0]->{address} . "\n";
38 }
39  
40 # these methods output data in vCard format
41 my $file = $address_book->as_file('/path/file.vcf'); # write to a file
42 my $string = $address_book->as_string();
43  
44  
45 =head1 DESCRIPTION
46  
47 A vCard is a digital business card. L<vCard> and vCard::AddressBook provide an
48 API for parsing, editing, and creating vCards.
49  
50 This module is built on top of L<Text::vCard> and L<Text::vCard::AddressBook>
51 and provides a more intuitive user interface.
52  
53  
54 =head1 ENCODING AND UTF-8
55  
56 =head2 Constructor Arguments
57  
58 The 'encoding_in' and 'encoding_out' constructor parameters allow you to read
59 and write vCard files with any encoding. Examples of valid values are
60 'UTF-8', 'Latin1', and 'none'.
61  
62 Both parameters default to 'UTF-8' and this should just work for the vast
63 majority of people. The latest vCard RFC 6350 only allows UTF-8 as an encoding
64 so most people should not need to use either parameter.
65  
66 =head2 MIME encodings
67  
68 vCard RFC 6350 only allows UTF-8 but it still permits 8bit MIME encoding
69 schemes such as Quoted-Printable and Base64 which are supported by this module.
70  
71 =head2 Getting and setting values on a vCard object
72  
73 If you set values on a vCard object they must be decoded values. The
74 only exception to this rule is if you are messing around with the
75 'encoding_out' constructor arg.
76  
77 When you get values from a vCard object they will be decoded values.
78  
79  
80 =head1 METHODS
81  
82 =cut
83  
84 1 ▶has encoding_in => ( is => 'rw', default => sub {'UTF-8'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
85 1 ▶has encoding_out => ( is => 'rw', default => sub {'UTF-8'} );
Subroutine __ANON__: called CC=1 SCAR=0.0
86 6 ▶has vcards => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
87  
88 3 with 'vCard::Role::FileIO';
89  
90 =head2 add_vcard()
91  
92 Creates a new vCard object and adds it to the address book. Returns a L<vCard>
93 object.
94  
95 =cut
96  
97 3 ▶sub add_vcard {
Subroutine add_vcard: called CC=1 SCAR=0.0
Pod documented
98 3 my ($self) = @_;
99 3 my $vcard = vCard->new(
100 { encoding_in => $self->encoding_in,
101 encoding_out => $self->encoding_out,
102 }
103 );
104 3 push @{ $self->vcards }, $vcard;
105 3 return $vcard;
106 }
107  
108 =head2 load_file($filename)
109  
110 Load and parse the contents of $filename. Returns $self so the method can be
111 chained.
112  
113 =cut
114  
115 3 ▶sub load_file {
Subroutine load_file: called CC=1 SCAR=0.0
Pod documented
116 3 my ( $self, $filename ) = @_;
117  
118 3 my $file = $self->_path($filename);
119 3 my $string = $file->slurp( $self->_iomode_in );
120  
121 3 $self->load_string($string);
122  
123 3 return $self;
124 }
125  
126 =head2 load_string($string)
127  
128 Load and parse the contents of $string. This method assumes that $string is
129 decoded (but not MIME decoded). Returns $self so the method can be chained.
130  
131 =cut
132  
133 7 ▶sub load_string {
Subroutine load_string: called CC=2 SCAR=6.9
Pod documented
134 7 my ( $self, $string ) = @_;
135  
136 7 ▶ die <<EOS
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\r\n/m 1 6 7
137 ERROR: Either there is no END in this vCard or there is a problem with the line
138 endings. Note that the vCard RFC requires line endings delimited by \\r\\n
139 regardless of your operating system. Windows :crlf mode will strip out the \\r
140 so don't use that.
141 EOS
142 unless $string =~ m/\r\n/m;
143  
144 6 $self->_create_vcards($string);
145  
146 6 return $self;
147 }
148  
149 6 ▶sub _create_vcards {
Subroutine _create_vcards: called CC=3 SCAR=11.0
150 6 my ( $self, $string ) = @_;
151  
152 6 my $vcards_data = Text::vCard::Addressbook->new(
153 { encoding_in => $self->encoding_in,
154 encoding_out => $self->encoding_out,
155 }
156 )->_pre_process_text($string);
157  
158 6 foreach my $vcard_data (@$vcards_data) {
159 carp "This file has $vcard_data->{type} data that was not parsed"
160 6 ▶ unless $vcard_data->{type} =~ /VCARD/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $vcard_data->{"type"} =~ /VCARD/i 0 6 6
161  
162 6 my $vcard = vCard->new(
163 { encoding_in => $self->encoding_in,
164 encoding_out => $self->encoding_out,
165 }
166 );
167 my $text_vcard = Text::vCard->new(
168 { asData_node => $vcard_data->{properties},
169 6 encoding_out => $self->encoding_out,
170 }
171 );
172  
173 6 $self->_copy_simple_nodes( $text_vcard => $vcard );
174 6 $self->_copy_name( $text_vcard => $vcard );
175 6 $self->_copy_photo( $text_vcard => $vcard );
176 6 $self->_copy_phones( $text_vcard => $vcard );
177 6 $self->_copy_addresses( $text_vcard => $vcard );
178 6 $self->_copy_email_addresses( $text_vcard => $vcard );
179  
180 6 push @{ $self->vcards }, $vcard;
181 }
182 }
183  
184 6 ▶sub _copy_simple_nodes {
Subroutine _copy_simple_nodes: called CC=5 SCAR=16.3
185 6 my ( $self, $text_vcard, $vcard ) = @_;
186  
187 6 foreach my $node_type ( vCard->_simple_node_types ) {
188 36 ▶ if ( $node_type eq 'full_name' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node_type eq 'full_name') { } 6 30 36
189 6 ▶ next unless $text_vcard->fullname;
Branch: 1 branch
BranchTrueFalseTotal
unless $text_vcard->fullname 0 6 6
190 6 $vcard->full_name( $text_vcard->fullname );
191 } else {
192 30 ▶ next unless $text_vcard->$node_type;
Branch: 1 branch
BranchTrueFalseTotal
unless $text_vcard->$node_type 0 30 30
193 30 $vcard->$node_type( $text_vcard->$node_type );
194 }
195 }
196 }
197  
198 6 ▶sub _copy_photo {
Subroutine _copy_photo: called CC=1 SCAR=0.0
199 6 my ( $self, $text_vcard, $vcard ) = @_;
200 6 $vcard->photo( URI->new( $text_vcard->photo ) );
201 }
202  
203 6 ▶sub _copy_name {
Subroutine _copy_name: called CC=6 SCAR=22.4
204 6 my ( $self, $text_vcard, $vcard ) = @_;
205  
206 6 my ($node) = $text_vcard->get('n');
207  
208 6 ▶ $vcard->family_names( [ $node->family || () ] );
Condition33%1/3
$node->family || ()
l!l&&r!l&&!r
600
MC/DC0%0/2
$node->family || ()
$node->family ()
Truth table33%1/3
A || B
A= $node->familyB= ()
ABresult
1X1
011
000
209 6 ▶ $vcard->given_names( [ $node->given || () ] );
Condition33%1/3
$node->given || ()
l!l&&r!l&&!r
600
MC/DC0%0/2
$node->given || ()
$node->given ()
Truth table33%1/3
A || B
A= $node->givenB= ()
ABresult
1X1
011
000
210 6 ▶ $vcard->other_names( [ $node->middle || () ] );
Condition33%1/3
$node->middle || ()
l!l&&r!l&&!r
060
MC/DC0%0/2
$node->middle || ()
$node->middle ()
Truth table33%1/3
A || B
A= $node->middleB= ()
ABresult
1X1
011
000
211 6 ▶ $vcard->honorific_prefixes( [ $node->prefixes || () ] );
Condition33%1/3
$node->prefixes || ()
l!l&&r!l&&!r
600
MC/DC0%0/2
$node->prefixes || ()
$node->prefixes ()
Truth table33%1/3
A || B
A= $node->prefixesB= ()
ABresult
1X1
011
000
212 6 ▶ $vcard->honorific_suffixes( [ $node->suffixes || () ] );
Condition33%1/3
$node->suffixes || ()
l!l&&r!l&&!r
600
MC/DC0%0/2
$node->suffixes || ()
$node->suffixes ()
Truth table33%1/3
A || B
A= $node->suffixesB= ()
ABresult
1X1
011
000
213 }
214  
215 6 ▶sub _copy_phones {
Subroutine _copy_phones: called CC=4 SCAR=13.9
216 6 my ( $self, $text_vcard, $vcard ) = @_;
217  
218 6 my @phones;
219 6 ▶ my $nodes = $text_vcard->get('tel') || [];
Condition50%1/2
$text_vcard->get('tel') || []
l!l
60
MC/DC0%0/1
$text_vcard->get('tel') || []
$text_vcard->get('tel')
Truth table50%1/2
A
A= $text_vcard->get('tel')
Aresult
11
00
220  
221 6 foreach my $node (@$nodes) {
222 12 my $phone;
223 12 $phone->{type} = scalar $node->types;
224 12 ▶ $phone->{preferred} = $node->is_pref ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$node->is_pref ? : 6 6 12
225 12 $phone->{number} = $node->value;
226 12 push @phones, $phone;
227 }
228  
229 6 $vcard->phones( \@phones );
230 }
231  
232 6 ▶sub _copy_addresses {
Subroutine _copy_addresses: called CC=11 SCAR=25.8
233 6 my ( $self, $text_vcard, $vcard ) = @_;
234  
235 6 my @addresses;
236 6 ▶ my $nodes = $text_vcard->get('adr') || [];
Condition50%1/2
$text_vcard->get('adr') || []
l!l
60
MC/DC0%0/1
$text_vcard->get('adr') || []
$text_vcard->get('adr')
Truth table50%1/2
A
A= $text_vcard->get('adr')
Aresult
11
00
237  
238 6 foreach my $node (@$nodes) {
239 12 my $address;
240 12 $address->{type} = scalar $node->types;
241 12 ▶ $address->{preferred} = $node->is_pref ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$node->is_pref ? : 0 12 12
242 12 ▶ $address->{po_box} = $node->po_box || undef;
Condition50%1/2
$node->po_box || undef
l!l
012
MC/DC0%0/1
$node->po_box || undef
$node->po_box
Truth table50%1/2
A
A= $node->po_box
Aresult
11
00
243 12 ▶ $address->{street} = $node->street || undef;
Condition50%1/2
$node->street || undef
l!l
120
MC/DC0%0/1
$node->street || undef
$node->street
Truth table50%1/2
A
A= $node->street
Aresult
11
00
244 12 ▶ $address->{city} = $node->city || undef;
Condition50%1/2
$node->city || undef
l!l
120
MC/DC0%0/1
$node->city || undef
$node->city
Truth table50%1/2
A
A= $node->city
Aresult
11
00
245 12 ▶ $address->{post_code} = $node->post_code || undef;
Condition50%1/2
$node->post_code || undef
l!l
120
MC/DC0%0/1
$node->post_code || undef
$node->post_code
Truth table50%1/2
A
A= $node->post_code
Aresult
11
00
246 12 ▶ $address->{region} = $node->region || undef;
Condition50%1/2
$node->region || undef
l!l
120
MC/DC0%0/1
$node->region || undef
$node->region
Truth table50%1/2
A
A= $node->region
Aresult
11
00
247 12 ▶ $address->{country} = $node->country || undef;
Condition50%1/2
$node->country || undef
l!l
120
MC/DC0%0/1
$node->country || undef
$node->country
Truth table50%1/2
A
A= $node->country
Aresult
11
00
248 12 ▶ $address->{extended} = $node->extended || undef;
Condition50%1/2
$node->extended || undef
l!l
012
MC/DC0%0/1
$node->extended || undef
$node->extended
Truth table50%1/2
A
A= $node->extended
Aresult
11
00
249 12 push @addresses, $address;
250 }
251  
252 6 $vcard->addresses( \@addresses );
253 }
254  
255 6 ▶sub _copy_email_addresses {
Subroutine _copy_email_addresses: called CC=4 SCAR=13.9
256 6 my ( $self, $text_vcard, $vcard ) = @_;
257  
258 6 my @email_addresses;
259 6 ▶ my $nodes = $text_vcard->get('email') || [];
Condition50%1/2
$text_vcard->get('email') || []
l!l
60
MC/DC0%0/1
$text_vcard->get('email') || []
$text_vcard->get('email')
Truth table50%1/2
A
A= $text_vcard->get('email')
Aresult
11
00
260  
261 6 foreach my $node (@$nodes) {
262 12 my $email_address;
263 12 $email_address->{type} = scalar $node->types;
264 12 ▶ $email_address->{preferred} = $node->is_pref ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$node->is_pref ? : 6 6 12
265 12 $email_address->{address} = $node->value;
266 12 push @email_addresses, $email_address;
267 }
268  
269 6 $vcard->email_addresses( \@email_addresses );
270 }
271  
272 =head2 as_file($filename)
273  
274 Write all the vCards to $filename. Files are written as UTF-8 by default.
275 Dies if not successful.
276  
277 =cut
278  
279 1 ▶sub as_file {
Subroutine as_file: called CC=1 SCAR=0.0
Pod documented
280 1 my ( $self, $filename ) = @_;
281 1 my $file = $self->_path($filename);
282 1 $file->spew( $self->_iomode_out, $self->as_string );
283 1 return $file;
284 }
285  
286 =head2 as_string()
287  
288 Returns all the vCards as a single string.
289  
290 =cut
291  
292 1 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=6.9
Pod documented
293 1 my ($self) = @_;
294 1 my $string = '';
295 1 $string .= $_->as_string for @{ $self->vcards };
296 1 return $string;
297 }
298  
299 =head1 AUTHOR
300  
301 Eric Johnson (kablamo), github ~!at!~ iijo dot org
302  
303 =head1 ACKNOWLEDGEMENTS
304  
305 Thanks to L<Foxtons|http://foxtons.co.uk> for making this module possible by
306 donating a significant amount of developer time.
307  
308 =cut
309  
310 3 1;
« lib/Text/vCard.pm ↑ Summary lib/Text/vCard/Addressbook.pm »