File Coverage

blib/lib/WWW/Google/Contacts.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package WWW::Google::Contacts;
2             {
3             $WWW::Google::Contacts::VERSION = '0.38';
4             }
5              
6             # ABSTRACT: Google Contacts Data API
7              
8 17     17   223926 use Moose;
  0            
  0            
9              
10             use Carp qw/croak/;
11              
12             use WWW::Google::Contacts::Server;
13             use WWW::Google::Contacts::Contact;
14             use WWW::Google::Contacts::ContactList;
15             use WWW::Google::Contacts::Group;
16             use WWW::Google::Contacts::GroupList;
17              
18             has username => (
19             isa => 'Str',
20             is => 'rw',
21             default => sub { $ENV{GOOGLE_USERNAME} },
22             );
23              
24             has password => (
25             isa => 'Str',
26             is => 'rw',
27             default => sub { $ENV{GOOGLE_PASSWORD} },
28             );
29              
30             has protocol => (
31             isa => 'Str',
32             is => 'ro',
33             default => 'http',
34             );
35              
36             has server => (
37             isa => 'Object',
38             is => 'ro',
39             lazy_build => 1,
40             );
41              
42             # backward compability
43             has email =>
44             ( isa => 'Str', is => 'rw', trigger => sub { $_[0]->username( $_[1] ) } );
45             has pass =>
46             ( isa => 'Str', is => 'rw', trigger => sub { $_[0]->password( $_[1] ) } );
47              
48             sub _build_server {
49             my $self = shift;
50             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             my $self = shift;
61             my $args =
62             ( scalar(@_) == 1 and ref( $_[0] ) eq 'HASH' )
63             ? { %{ $_[0] }, server => $self->server }
64             : { @_, server => $self->server };
65             return WWW::Google::Contacts::Contact->new($args);
66             }
67              
68             sub contact {
69             my ( $self, $id ) = @_;
70             return WWW::Google::Contacts::Contact->new( id => $id,
71             server => $self->server )->retrieve;
72             }
73              
74             sub contacts {
75             my $self = shift;
76              
77             my $list =
78             WWW::Google::Contacts::ContactList->new( server => $self->server );
79             return $list;
80             }
81              
82             sub new_group {
83             my $self = shift;
84             my $args =
85             ( scalar(@_) == 1 and ref( $_[0] ) eq 'HASH' )
86             ? { %{ $_[0] }, server => $self->server }
87             : { @_, server => $self->server };
88             return WWW::Google::Contacts::Group->new($args);
89             }
90              
91             sub group {
92             my ( $self, $id ) = @_;
93             return WWW::Google::Contacts::Group->new( id => $id,
94             server => $self->server )->retrieve;
95             }
96              
97             sub groups {
98             my $self = shift;
99              
100             my $list = WWW::Google::Contacts::GroupList->new( server => $self->server );
101             return $list;
102             }
103              
104             # All code below is for backwards compability
105              
106             sub login {
107             my ( $self, $email, $pass ) = @_;
108             warn "This method is deprecated and will be removed shortly";
109             $self->email($email);
110             $self->pass($pass);
111             my $server = WWW::Google::Contacts::Server->new(
112             { username => $self->email, password => $self->password } );
113             $server->authenticate;
114             return 1;
115             }
116              
117             sub create_contact {
118             my $self = shift;
119             warn "This method is deprecated and will be removed shortly";
120             my $data = scalar @_ % 2 ? shift : {@_};
121              
122             my $contact = $self->new_contact;
123             return $self->_create_or_update_contact( $contact, $data );
124             }
125              
126             sub _create_or_update_contact {
127             my ( $self, $contact, $data ) = @_;
128              
129             $contact->given_name( $data->{givenName} );
130             $contact->family_name( $data->{familyName} );
131             $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             );
140             if ( $contact->{secondaryMail} ) {
141             $contact->add_email(
142             {
143             type => "home",
144             value => $data->{secondaryMail},
145             }
146             );
147             }
148              
149             # if ( $contact->{groupMembershipInfo} ) {
150             # $data->{'atom:entry'}->{'gContact:groupMembershipInfo'} = {
151             # deleted => 'false',
152             # href => $contact->{groupMembershipInfo}
153             # };
154             # }
155             if ( $contact->create_or_update ) {
156             return 1;
157             }
158             return 0;
159             }
160              
161             sub get_contacts {
162             my $self = shift;
163              
164             warn "This method is deprecated and will be removed shortly";
165             my $list = $self->contacts;
166             my @contacts;
167             foreach my $c ( @{ $list->elements } ) {
168             my $d = $c;
169             ( $d->{id} ) =
170             map { $_->{href} }
171             grep { $_->{rel} eq 'self' } @{ $d->{link} };
172             $d->{name} = $d->{'gd:name'};
173             $d->{email} = $d->{'gd:email'};
174             $d->{groupMembershipInfo} = $d->{'gContact:groupMembershipInfo'};
175             push @contacts, $d;
176             }
177             return @contacts;
178             }
179              
180             sub get_contact {
181             my ( $self, $id ) = @_;
182              
183             warn "This method is deprecated and will be removed shortly";
184             my $contact = $self->new_contact( id => $id )->retrieve;
185             my $data = $contact->raw_data_for_backwards_compability;
186             $data->{name} = $data->{'gd:name'};
187             $data->{email} = $data->{'gd:email'};
188             $data->{groupMembershipInfo} = $data->{'gContact:groupMembershipInfo'};
189             return $data;
190             }
191              
192             sub update_contact {
193             my ( $self, $id, $contact ) = @_;
194              
195             warn "This method is deprecated and will be removed shortly";
196             my $c = $self->new_contact( id => $id )->retrieve;
197             return $self->_create_or_update_contact( $c, $contact );
198             }
199              
200             sub delete_contact {
201             my ( $self, $id ) = @_;
202              
203             warn "This method is deprecated and will be removed shortly";
204             $self->new_contact( id => $id )->delete;
205             }
206              
207             sub get_groups {
208             my $self = shift;
209              
210             warn "This method is deprecated and will be removed shortly";
211             my $list = $self->groups;
212             my @groups;
213             foreach my $d ( @{ $list->elements } ) {
214             my $link = ref( $d->{link} ) eq 'ARRAY' ? $d->{link} : [ $d->{link} ];
215             ( $d->{id} ) =
216             map { $_->{href} }
217             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             ? ( 'gContact:systemGroup' => $d->{'gContact:systemGroup'}->{'id'} )
225             : (),
226             };
227             }
228             return @groups;
229             }
230              
231             sub get_group {
232             my ( $self, $id ) = @_;
233              
234             warn "This method is deprecated and will be removed shortly";
235             my $group = $self->new_group( id => $id )->retrieve;
236             my $data = $group->raw_data_for_backwards_compability;
237             return $data;
238             }
239              
240             sub _create_or_update_group {
241             my ( $self, $group, $data ) = @_;
242              
243             $group->title( $data->{title} );
244             if ( $group->create_or_update ) {
245             return 1;
246             }
247             return 0;
248             }
249              
250             sub create_group {
251             my $self = shift;
252             my $data = scalar @_ % 2 ? shift : {@_};
253              
254             warn "This method is deprecated and will be removed shortly";
255             my $group = $self->new_group;
256             return $self->_create_or_update_group( $group, $data );
257             }
258              
259             sub update_group {
260             my ( $self, $id, $args ) = @_;
261              
262             warn "This method is deprecated and will be removed shortly";
263             my $g = $self->new_group( id => $id )->retrieve;
264             return $self->_create_or_update_group( $g, $args );
265             }
266              
267             sub delete_group {
268             my ( $self, $id ) = @_;
269              
270             warn "This method is deprecated and will be removed shortly";
271             $self->new_group( id => $id )->delete;
272             }
273              
274             no Moose;
275             __PACKAGE__->meta->make_immutable;
276             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