Contacts.pm

filter: Statementstmt 27.4% 40 / 146 Branchbran 11.1% 2 / 18 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 31.2% 10 / 32 Podpod 100% 17 / 17 totaltotal 32.2% 72 / 223 CC 14 SCAR 17.0
CC
14
Cov
26%
get_groups
20.0
new_group
12.0
_create_or_update_contact
12.0
CRAP 5.5
« Contacts/Server.pm ↑ Summary Contacts/Type/Rel.pm »
1 package WWW::Google::Contacts;
2 {
3 17 $WWW::Google::Contacts::VERSION = '0.38';
4 }
5  
6 # ABSTRACT: Google Contacts Data API
7  
8 17 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 17 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 17 ▶use WWW::Google::Contacts::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 17 ▶use WWW::Google::Contacts::Contact;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 17 ▶use WWW::Google::Contacts::ContactList;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 17 ▶use WWW::Google::Contacts::Group;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 17 ▶use WWW::Google::Contacts::GroupList;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 has username => (
19 isa => 'Str',
20 is => 'rw',
21 0 ▶ default => sub { $ENV{GOOGLE_USERNAME} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
22 17 );
23  
24 has password => (
25 isa => 'Str',
26 is => 'rw',
27 0 ▶ default => sub { $ENV{GOOGLE_PASSWORD} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 17 );
29  
30 17 has protocol => (
31 isa => 'Str',
32 is => 'ro',
33 default => 'http',
34 );
35  
36 17 has server => (
37 isa => 'Object',
38 is => 'ro',
39 lazy_build => 1,
40 );
41  
42 # backward compability
43 has email =>
44 0 ▶ ( isa => 'Str', is => 'rw', trigger => sub { $_[0]->username( $_[1] ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 has pass =>
46 0 ▶ ( isa => 'Str', is => 'rw', trigger => sub { $_[0]->password( $_[1] ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
47  
48 sub _build_server {
49 2 ▶ my $self = shift;
Subroutine _build_server: called CC=1 SCAR=0.0
50 2 return WWW::Google::Contacts::Server->new(
51 {
52 username => $self->username,
53 password => $self->password,
54 protocol => $self->protocol
55 }
56 );
57 }
58  
59 sub new_contact {
60 3 ▶ my $self = shift;
Subroutine new_contact: called CC=3 SCAR=11.0
Pod documented
61 my $args =
62 ( scalar(@_) == 1 and ref( $_[0] ) eq 'HASH' )
63 3 ▶ ? { %{ $_[0] }, server => $self->server }
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 && ref $_[0] eq 'HASH' ? : 1 2 3
Condition66%2/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
201
MC/DC50%1/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
64 : { @_, server => $self->server };
65 3 return WWW::Google::Contacts::Contact->new($args);
66 }
67  
68 sub contact {
69 0 ▶ my ( $self, $id ) = @_;
Subroutine contact: not called CC=1 SCAR=6.9
Pod documented
70 0 return WWW::Google::Contacts::Contact->new( id => $id,
71 server => $self->server )->retrieve;
72 }
73  
74 sub contacts {
75 0 ▶ my $self = shift;
Subroutine contacts: not called CC=1 SCAR=6.9
Pod documented
76  
77 0 my $list =
78 WWW::Google::Contacts::ContactList->new( server => $self->server );
79 0 return $list;
80 }
81  
82 sub new_group {
83 0 ▶ my $self = shift;
Subroutine new_group: not called CC=3 SCAR=24.8
Pod documented
84 my $args =
85 ( scalar(@_) == 1 and ref( $_[0] ) eq 'HASH' )
86 0 ▶ ? { %{ $_[0] }, server => $self->server }
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 && ref $_[0] eq 'HASH' ? : 0 0 0
Condition0%0/3
scalar @_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 1 and ref $_[0] eq "HASH"
scalar @_ == 1 ref $_[0] eq "HASH"
Truth table0%0/3
A and B
A= scalar @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
87 : { @_, server => $self->server };
88 0 return WWW::Google::Contacts::Group->new($args);
89 }
90  
91 sub group {
92 0 ▶ my ( $self, $id ) = @_;
Subroutine group: not called CC=1 SCAR=6.9
Pod documented
93 0 return WWW::Google::Contacts::Group->new( id => $id,
94 server => $self->server )->retrieve;
95 }
96  
97 sub groups {
98 0 ▶ my $self = shift;
Subroutine groups: not called CC=1 SCAR=6.9
Pod documented
99  
100 0 my $list = WWW::Google::Contacts::GroupList->new( server => $self->server );
101 0 return $list;
102 }
103  
104 # All code below is for backwards compability
105  
106 sub login {
107 0 ▶ my ( $self, $email, $pass ) = @_;
Subroutine login: not called CC=1 SCAR=6.9
Pod documented
108 0 warn "This method is deprecated and will be removed shortly";
109 0 $self->email($email);
110 0 $self->pass($pass);
111 0 my $server = WWW::Google::Contacts::Server->new(
112 { username => $self->email, password => $self->password } );
113 0 $server->authenticate;
114 0 return 1;
115 }
116  
117 sub create_contact {
118 0 ▶ my $self = shift;
Subroutine create_contact: not called CC=2 SCAR=17.9
Pod documented
119 0 warn "This method is deprecated and will be removed shortly";
120 0 ▶ my $data = scalar @_ % 2 ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ % 2 ? : 0 0 0
121  
122 0 my $contact = $self->new_contact;
123 0 return $self->_create_or_update_contact( $contact, $data );
124 }
125  
126 sub _create_or_update_contact {
127 0 ▶ my ( $self, $contact, $data ) = @_;
Subroutine _create_or_update_contact: not called CC=3 SCAR=24.8
128  
129 0 $contact->given_name( $data->{givenName} );
130 0 $contact->family_name( $data->{familyName} );
131 0 $contact->notes( $data->{Notes} );
132 $contact->email(
133 {
134 type => "work",
135 primary => 1,
136 value => $data->{primaryMail},
137 display_name => $data->{displayName},
138 }
139 0 );
140 0 ▶ if ( $contact->{secondaryMail} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contact->{"secondaryMail"}) 0 0 0
141 $contact->add_email(
142 {
143 type => "home",
144 value => $data->{secondaryMail},
145 }
146 0 );
147 }
148  
149 # if ( $contact->{groupMembershipInfo} ) {
150 # $data->{'atom:entry'}->{'gContact:groupMembershipInfo'} = {
151 # deleted => 'false',
152 # href => $contact->{groupMembershipInfo}
153 # };
154 # }
155 0 ▶ if ( $contact->create_or_update ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($contact->create_or_update) 0 0 0
156 0 return 1;
157 }
158 0 return 0;
159 }
160  
161 sub get_contacts {
162 0 ▶ my $self = shift;
Subroutine get_contacts: not called CC=2 SCAR=17.9
Pod documented
163  
164 0 warn "This method is deprecated and will be removed shortly";
165 0 my $list = $self->contacts;
166 0 my @contacts;
167 0 foreach my $c ( @{ $list->elements } ) {
168 0 my $d = $c;
169 ( $d->{id} ) =
170 0 map { $_->{href} }
171 0 grep { $_->{rel} eq 'self' } @{ $d->{link} };
172 0 $d->{name} = $d->{'gd:name'};
173 0 $d->{email} = $d->{'gd:email'};
174 0 $d->{groupMembershipInfo} = $d->{'gContact:groupMembershipInfo'};
175 0 push @contacts, $d;
176 }
177 0 return @contacts;
178 }
179  
180 sub get_contact {
181 0 ▶ my ( $self, $id ) = @_;
Subroutine get_contact: not called CC=1 SCAR=6.9
Pod documented
182  
183 0 warn "This method is deprecated and will be removed shortly";
184 0 my $contact = $self->new_contact( id => $id )->retrieve;
185 0 my $data = $contact->raw_data_for_backwards_compability;
186 0 $data->{name} = $data->{'gd:name'};
187 0 $data->{email} = $data->{'gd:email'};
188 0 $data->{groupMembershipInfo} = $data->{'gContact:groupMembershipInfo'};
189 0 return $data;
190 }
191  
192 sub update_contact {
193 0 ▶ my ( $self, $id, $contact ) = @_;
Subroutine update_contact: not called CC=1 SCAR=6.9
Pod documented
194  
195 0 warn "This method is deprecated and will be removed shortly";
196 0 my $c = $self->new_contact( id => $id )->retrieve;
197 0 return $self->_create_or_update_contact( $c, $contact );
198 }
199  
200 sub delete_contact {
201 0 ▶ my ( $self, $id ) = @_;
Subroutine delete_contact: not called CC=1 SCAR=6.9
Pod documented
202  
203 0 warn "This method is deprecated and will be removed shortly";
204 0 $self->new_contact( id => $id )->delete;
205 }
206  
207 sub get_groups {
208 0 ▶ my $self = shift;
Subroutine get_groups: not called CC=4 SCAR=30.0
Pod documented
209  
210 0 warn "This method is deprecated and will be removed shortly";
211 0 my $list = $self->groups;
212 0 my @groups;
213 0 foreach my $d ( @{ $list->elements } ) {
214 0 ▶ my $link = ref( $d->{link} ) eq 'ARRAY' ? $d->{link} : [ $d->{link} ];
Branch: 1 branch
BranchTrueFalseTotal
ref $d->{'link'} eq 'ARRAY' ? : 0 0 0
215 ( $d->{id} ) =
216 0 map { $_->{href} }
217 0 grep { $_->{rel} eq 'self' } @{$link};
218 push @groups,
219 {
220 id => $d->{id},
221 title => $d->{title},
222 updated => $d->{updated},
223 exists $d->{'gContact:systemGroup'}
224 0 ▶ ? ( 'gContact:systemGroup' => $d->{'gContact:systemGroup'}->{'id'} )
Branch: 1 branch
BranchTrueFalseTotal
exists $d->{'gContact:systemGroup'} ? : 0 0 0
225 : (),
226 };
227 }
228 0 return @groups;
229 }
230  
231 sub get_group {
232 0 ▶ my ( $self, $id ) = @_;
Subroutine get_group: not called CC=1 SCAR=6.9
Pod documented
233  
234 0 warn "This method is deprecated and will be removed shortly";
235 0 my $group = $self->new_group( id => $id )->retrieve;
236 0 my $data = $group->raw_data_for_backwards_compability;
237 0 return $data;
238 }
239  
240 sub _create_or_update_group {
241 0 ▶ my ( $self, $group, $data ) = @_;
Subroutine _create_or_update_group: not called CC=2 SCAR=17.9
242  
243 0 $group->title( $data->{title} );
244 0 ▶ if ( $group->create_or_update ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group->create_or_update) 0 0 0
245 0 return 1;
246 }
247 0 return 0;
248 }
249  
250 sub create_group {
251 0 ▶ my $self = shift;
Subroutine create_group: not called CC=2 SCAR=17.9
Pod documented
252 0 ▶ my $data = scalar @_ % 2 ? shift : {@_};
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ % 2 ? : 0 0 0
253  
254 0 warn "This method is deprecated and will be removed shortly";
255 0 my $group = $self->new_group;
256 0 return $self->_create_or_update_group( $group, $data );
257 }
258  
259 sub update_group {
260 0 ▶ my ( $self, $id, $args ) = @_;
Subroutine update_group: not called CC=1 SCAR=6.9
Pod documented
261  
262 0 warn "This method is deprecated and will be removed shortly";
263 0 my $g = $self->new_group( id => $id )->retrieve;
264 0 return $self->_create_or_update_group( $g, $args );
265 }
266  
267 sub delete_group {
268 0 ▶ my ( $self, $id ) = @_;
Subroutine delete_group: not called CC=1 SCAR=6.9
Pod documented
269  
270 0 warn "This method is deprecated and will be removed shortly";
271 0 $self->new_group( id => $id )->delete;
272 }
273  
274 17 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
275 17 __PACKAGE__->meta->make_immutable;
276 17 1;
277 __END__
278  
279 =head1 SYNOPSIS
280  
281 use WWW::Google::Contacts;
282  
283 my $google = WWW::Google::Contacts->new(
284 username => "your.username",
285 password => "your.password",
286 protocol => "https",
287 );
288  
289 # Create a new contact
290 my $contact = $google->new_contact;
291 $contact->full_name("Emmett Brown");
292 $contact->name_prefix("Dr");
293 $contact->email('doctor@timetravel.org');
294 $contact->hobby("Time travel");
295 $contact->jot([ "Went back in time", "Went forward in time", "Became blacksmith" ]),
296 $contact->create; # save it to the server
297  
298 # Now search for the given name, and read the jots
299 my @contacts = $google->contacts->search({ given_name => "Emmett" });
300 foreach my $c ( @contacts ) {
301 print "Got the following jots about the good doctor\n";
302 foreach my $jot ( @{ $c->jot } ) {
303 print "Jot: " . $jot->value . "\n";
304 }
305 print "And now he goes back to the future\n";
306 $c->delete;
307 }
308  
309 # Print the names of all groups
310 my $groups = $google->groups;
311 while ( my $group = $groups->next ) {
312 print "Title = " . $group->title . "\n";
313 }
314  
315 # Add the contact to existing group 'Movie stars' and to a new group 'Back to the future'
316 my $new_group = $google->new_group({ title => "Back to the future" });
317 $new_group->create; # create on server
318  
319 my @groups = $google->groups->search({ title => "Movie stars" });
320 my $movie_stars_group = shift @groups;
321  
322 $contact->add_group_membership( $new_group );
323 $contact->add_group_membership( $movie_stars_group );
324 $contact->update;
325  
326  
327 =head1 DESCRIPTION
328  
329 This module implements 'Google Contacts Data API' according L<http://code.google.com/apis/contacts/docs/3.0/developers_guide_protocol.html>
330  
331 B<NOTE> This new interface is still quite untested. Please report any bugs.
332  
333 =head1 CONSTRUCTOR
334  
335 =head2 new( username => .., password => .. , protocol => ..)
336  
337 I<username> and I<password> are required arguments and must be valid Google credentials. If you do not have a Google account
338 you can create one at L<https://www.google.com/accounts/NewAccount>.
339  
340 I<protocol> defaults to B<http>, but can optionally be set to B<https>.
341  
342 =head1 METHODS
343  
344 =head2 $google->new_contact
345  
346 Returns a new empty L<WWW::Google::Contacts::Contact> object.
347  
348 =head2 $google->contact( $id )
349  
350 Given a valid contact ID, returns a L<WWW::Google::Contacts::Contact> object populated with contact data from Google.
351  
352 =head2 $google->contacts
353  
354 Returns a L<WWW::Google::Contacts::ContactList> object which can be used to iterate over all your contacts.
355  
356 =head2 $google->new_group
357  
358 Returns a new L<WWW::Google::Contacts::Group> object.
359  
360 =head2 $google->group( $id )
361  
362 Given a valid group ID, returns a L<WWW::Google::Contacts::Group> object populated with group data from Google.
363  
364 =head2 $google->groups
365  
366 Returns a L<WWW::Google::Contacts::GroupList> object which can be used to iterate over all your groups.
367  
368 =head1 DEPRECATED METHODS
369  
370 The old module interface is still available, but its use is discouraged. It will eventually be removed from the module.
371  
372 =over 4
373  
374 =item * new/login
375  
376 my $gcontacts = WWW::Google::Contacts->new();
377 $gcontacts->login('fayland@gmail.com', 'pass') or die 'login failed';
378  
379 =item * create_contact
380  
381 $gcontacts->create_contact( {
382 givenName => 'FayTestG',
383 familyName => 'FayTestF',
384 fullName => 'Fayland Lam',
385 Notes => 'just a note',
386 primaryMail => 'primary@example.com',
387 displayName => 'FayTest Dis',
388 secondaryMail => 'secndary@test.com', # optional
389 } );
390  
391 return 1 if created
392  
393 =item * get_contacts
394  
395 my @contacts = $gcontacts->get_contacts;
396 my @contacts = $gcontacts->get_contacts( {
397 group => 'thin', # default to 'full'
398 } )
399 my @contacts = $gcontacts->get_contacts( {
400 updated-min => '2007-03-16T00:00:00',
401 start-index => 10,
402 max-results => 99, # default as 9999
403 } );
404  
405 get contacts from this account.
406  
407 C<group> refers L<http://code.google.com/apis/contacts/docs/2.0/reference.html#Projections>
408  
409 C<start-index>, C<max_results> etc refer L<http://code.google.com/apis/contacts/docs/2.0/reference.html#Parameters>
410  
411 =item * get_contact($id)
412  
413 my $contact = $gcontacts->get_contact('http://www.google.com/m8/feeds/contacts/account%40gmail.com/base/1');
414  
415 get a contact by B<id>
416  
417 =item * update_contact
418  
419 my $status = $gcontacts->update_contact('http://www.google.com/m8/feeds/contacts/account%40gmail.com/base/123623e48cb4e70a', {
420 givenName => 'FayTestG2',
421 familyName => 'FayTestF2',
422 fullName => 'Fayland Lam2',
423 Notes => 'just a note2',
424 primaryMail => 'primary@example2.com',
425 displayName => 'FayTest2 Dis',
426 secondaryMail => 'secndary@test62.com', # optional
427 } );
428  
429 update a contact
430  
431 =item * delete_contact($id)
432  
433 my $status = $gcontacts->delete_contact('http://www.google.com/m8/feeds/contacts/account%40gmail.com/base/1');
434  
435 The B<id> is from C<get_contacts>.
436  
437 =item * create_group
438  
439 my $status = $gcontacts->create_group( { title => 'Test Group' } );
440  
441 Create a new group
442  
443 =item * get_groups
444  
445 my @groups = $gcontacts->get_groups;
446 my @groups = $gcontacts->get_groups( {
447 updated-min => '2007-03-16T00:00:00',
448 start-index => 10,
449 max-results => 99, # default as 9999
450 } );
451  
452 Get all groups.
453  
454 =item * get_group($id)
455  
456 my $group = $gcontacts->get_group('http://www.google.com/m8/feeds/groups/account%40gmail.com/base/6e744e7d0a4b398c');
457  
458 get a group by B<id>
459  
460 =item * update_group($id, { title => $title })
461  
462 my $status = $gcontacts->update_group( 'http://www.google.com/m8/feeds/groups/account%40gmail.com/base/6e744e7d0a4b398c', { title => 'New Test Group 66' } );
463  
464 Update a group
465  
466 =item * delete_group
467  
468 my $status = $gcontacts->delete_contact('http://www.google.com/m8/feeds/groups/account%40gmail.com/base/6e744e7d0a4b398c');
469  
470 =back
471  
472 =head1 SEE ALSO
473  
474 L<WWW::Google::Contacts::Contact>
475  
476 L<WWW::Google::Contacts::ContactList>
477  
478 L<WWW::Google::Contacts::Group>
479  
480 L<WWW::Google::Contacts::GroupList>
481  
482 L<http://code.google.com/apis/contacts/docs/3.0/developers_guide_protocol.html>
483  
484 =head1 ACKNOWLEDGEMENTS
485  
486 Fayland Lam - who wrote the first version of this module
487  
488 John Clyde - who shared his code about Contacts API with Fayland
489  
490 =head1 TODO
491  
492 =over 4
493  
494 =item More POD
495  
496 =item Unit tests. Very lame right now
497  
498 =item Images
499  
500 =item Fix bugs :)
501  
502 =back
503  
504 =head1 AUTHOR
505  
506 Magnus Erixzon <magnus@erixzon.com>
507  
508 =head1 COPYRIGHT AND LICENSE
509  
510 This software is copyright (c) 2010 by Magnus Erixzon.
511  
512 This is free software; you can redistribute it and/or modify it under
513 the same terms as perl itself.
514  
515 =cut
« Contacts/Server.pm ↑ Summary Contacts/Type/Rel.pm »