| 1 | package WWW::Google::Contacts::Photo; | ||
| 2 | { | ||
| 3 | $WWW::Google::Contacts::Photo::VERSION = '0.39'; | ||
| 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__ |