File Coverage

blib/lib/WebService/Google/Contact.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package WebService::Google::Contact;
2              
3 2     2   54908 use warnings;
  2         5  
  2         67  
4 2     2   11 use strict;
  2         4  
  2         68  
5 2     2   10 use Carp;
  2         8  
  2         232  
6              
7             our $VERSION = '0.1';
8              
9 2     2   10 use base qw/Class::Accessor::Fast/;
  2         4  
  2         2392  
10             __PACKAGE__->mk_accessors(qw(email contacts));
11              
12 2     2   11036 use URI;
  2         19471  
  2         76  
13 2     2   2916 use LWP::UserAgent;
  2         136733  
  2         85  
14 2     2   2546 use XML::Liberal;
  0            
  0            
15              
16             sub name_by_email {
17             my $self = shift;
18             my ($name) = split '@', $self->email;
19             return $name;
20             }
21              
22             sub uri_to_login {
23             my ($self, $next, $scope) = @_;
24             $scope ||= 'http://www.google.com/m8/feeds';
25             my $base = 'https://www.google.com/accounts/AuthSubRequest';
26             my $uri = URI->new( $base );
27             $uri->query_form(
28             scope => $scope,
29             session => 1,
30             next => $next,
31             secure => 0,
32             );
33             $uri->as_string;
34             }
35              
36             sub verify {
37             my ($self, $token) = @_;
38             if ( $self->upgrade_to_session_token( $token ) ) {
39             return $self->get_email;
40             }
41             return;
42             }
43              
44             sub upgrade_to_session_token {
45             my ($self, $token) = @_;
46             my $ua = LWP::UserAgent->new();
47             my $res = $ua->get(
48             'https://www.google.com/accounts/AuthSubSessionToken',
49             'Content-Type' => 'application/x-www-form-urlencoded',
50             'Authorization' => 'AuthSub token="' . $token . '"',
51             );
52             if ( $res->is_success and $res->content =~ /^Token=(.+)$/ ) {
53             $self->{session_token} = $1;
54             return 1;
55             }
56             Carp::cluck "error upgrade_to_session_token";
57             return;
58             }
59              
60             sub get_email {
61             my $self = shift;
62             my $token = $self->{session_token};
63             my $ua = LWP::UserAgent->new();
64             my $res = $ua->get(
65             'http://www.google.com/m8/feeds/contacts/default/full',
66             'Content-Type' => 'application/x-www-form-urlencoded',
67             'Authorization' => 'AuthSub token="' . $token . '"',
68             );
69             if ( $res->is_success ) {
70             $self->{xml_content} = $res->content;
71             return $self->{email} = $1 if $self->{xml_content} =~ /(.+?)<\/id>/;
72             }
73             Carp::cluck 'error get_contact';
74             }
75              
76             sub get_contact {
77             my $self = shift;
78             my $xml = $self->{xml_content};
79             my $parser = XML::Liberal->new;
80             my $doc = eval { $parser->parse_string( $xml ) };
81              
82             Carp::cluck $@ if $@;
83              
84             my $root = $doc->documentElement;
85              
86             my @entries = $root->findnodes("//*[local-name()='entry']");
87             my @contacts;
88             for my $entry ( @entries ) {
89             my %elems;
90             $elems{ $_ } = $entry->findvalue("./*[local-name()='" . $_ . "']") for qw(id updated title);
91             $elems{"name"} = $elems{"title"};
92             $elems{"email"} = $entry->getElementsByTagName('gd:email')->[0]->getAttribute("address");
93             push @contacts, \%elems;
94             }
95             return $self->{contacts} = \@contacts;
96             }
97              
98             1;
99              
100             __END__