Contacts/Photo.pm untested

filter: Statementstmt 0.0% 0 / 53 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 84 CC 4 SCAR 12.5
CC
4
Cov
0%
exists
6.0
_build_content
6.0
to_file
6.0
CRAP 3.5
1 package WWW::Google::Contacts::Photo;
2 {
3 $WWW::Google::Contacts::Photo::VERSION = '0.38';
4 }
5  
6 use Moose;
7 use MooseX::Types::Moose qw( Str );
8 use WWW::Google::Contacts::Server;
9  
10 use File::Slurp qw( read_file );
11  
12 extends 'WWW::Google::Contacts::Base';
13  
14 has server => ( is => 'ro', required => 1 );
15  
16 has rel => (
17 isa => Str,
18 is => 'rw',
19 traits => ['XmlField'],
20 xml_key => 'rel',
21 required => 1,
22 );
23  
24 has type => (
25 isa => Str,
26 is => 'rw',
27 traits => ['XmlField'],
28 xml_key => 'type',
29 required => 1,
30 );
31  
32 has href => (
33 isa => Str,
34 is => 'rw',
35 traits => ['XmlField'],
36 xml_key => 'href',
37 required => 1,
38 );
39  
40 has etag => (
41 isa => Str,
42 is => 'rw',
43 traits => ['XmlField'],
44 xml_key => 'gd:etag',
45 predicate => 'has_etag',
46 );
47  
48 has content => (
49 isa => Str,
50 is => 'rw',
51 lazy_build => 1,
52 );
53  
54 sub exists {
55 my $self = shift;
56 return undef unless $self->etag;
57 1;
58 }
59  
60 sub _build_content {
61 my ($self) = shift;
62 return undef unless $self->etag;
63 my $res = $self->server->get( $self->href );
64 return $res->content;
65 }
66  
67 sub to_file {
68 my ( $self, $filename ) = @_;
69  
70 my $content = $self->content;
71 die "There is no photo" unless $content;
72 open( my $fh, ">", $filename ) or die "Can't write to file [$filename]: $!";
73 print {$fh} $content;
74 close($fh) or die "Can't close file [$filename]: $!";
75 }
76  
77 sub from_file {
78 my ( $self, $filename ) = @_;
79  
80 my $content = read_file( $filename, binmode => ':raw' );
81 $self->content($content);
82 }
83  
84 sub create_or_update {
85 my $self = shift;
86 $self->server->put( $self->href, $self->etag, "image/*", $self->content );
87 }
88  
89 sub create { $_[0]->create_or_update }
90 sub update { $_[0]->create_or_update }
91  
92 sub delete {
93 my $self = shift;
94 $self->server->delete( $self->href, $self->etag );
95 }
96  
97 no Moose;
98 __PACKAGE__->meta->make_immutable;
99 1;
100 __END__
101  
102 =head1 SYNOPSIS
103  
104 use WWW::Google::Contacts;
105  
106 my $google = WWW::Google::Contacts->new( username => "your.username", password => "your.password" );
107  
108 my $contacts = $google->contacts;
109  
110 while ( my $contact = $contacts->next ) {
111 print "You got a friend called " . $contact->full_name . "\n";
112  
113 if ( $contact->photo->exists ) {
114 print "And you got a photo of this friend\n";
115 # Save the picture to disk!
116 $contact->photo->to_file( "/opt/images/" . $contact->full_name . ".jpg" );
117 } else {
118 # Upload a picture for this contact
119 $contact->photo->from_file( "/opt/images/" . $contact->full_name . ".jpg" );
120 # Remember to update Google
121 $contact->photo->update();
122 }
123 }
124  
125  
126 =head1 AUTHOR
127  
128 Magnus Erixzon <magnus@erixzon.com>
129  
130 =head1 COPYRIGHT AND LICENSE
131  
132 This software is copyright (c) 2010-2013 by Magnus Erixzon.
133  
134 This is free software; you can redistribute it and/or modify it under
135 the same terms as perl itself.
136  
137 =cut