File Coverage

blib/lib/WWW/Google/Contacts/Roles/CRUD.pm
Criterion Covered Total %
statement 9 40 22.5
branch 0 8 0.0
condition n/a
subroutine 3 9 33.3
pod 0 6 0.0
total 12 63 19.0


line stmt bran cond sub pod time code
1             package WWW::Google::Contacts::Roles::CRUD;
2             {
3             $WWW::Google::Contacts::Roles::CRUD::VERSION = '0.39';
4             }
5              
6 17     17   15559 use Moose::Role;
  17         45  
  17         171  
7 17     17   97893 use Carp qw( croak );
  17         44  
  17         1007  
8 17     17   11779 use WWW::Google::Contacts::Data;
  17         67  
  17         7280  
9              
10             requires 'create_url';
11              
12             has raw_data_for_backwards_compability => ( is => 'rw' );
13             has server => ( is => 'ro', required => 1 );
14              
15             sub as_xml {
16 0     0 0   my $self = shift;
17             my $entry = {
18             entry => {
19             'xmlns' => 'http://www.w3.org/2005/Atom',
20             'xmlns:gd' => 'http://schemas.google.com/g/2005',
21             'xmlns:gContact' => 'http://schemas.google.com/contact/2008',
22 0           %{ $self->to_xml_hashref },
  0            
23             },
24             };
25 0           my $xml = WWW::Google::Contacts::Data->encode_xml($entry);
26 0           return $xml;
27             }
28              
29             sub create_or_update {
30 0     0 0   my $self = shift;
31 0 0         if ( $self->has_id ) {
32 0           return $self->update;
33             }
34             else {
35 0           return $self->create;
36             }
37             }
38              
39             sub create {
40 0     0 0   my $self = shift;
41              
42 0           my $xml = $self->as_xml;
43 0           my $res =
44             $self->server->post( $self->create_url, undef, 'application/atom+xml',
45             $xml );
46 0           my $data = WWW::Google::Contacts::Data->decode_xml( $res->content );
47 0           $self->set_from_server($data);
48 0           1;
49             }
50              
51             sub retrieve {
52 0     0 0   my $self = shift;
53 0 0         croak "No id set" unless $self->id;
54              
55 0           my $res = $self->server->get( $self->id );
56 0           my $data = WWW::Google::Contacts::Data->decode_xml( $res->content );
57 0           $self->raw_data_for_backwards_compability($data);
58 0           $self->set_from_server($data);
59 0           $self;
60             }
61              
62             sub update {
63 0     0 0   my $self = shift;
64 0 0         croak "No id set" unless $self->id;
65              
66 0           my $xml = $self->as_xml;
67 0           $self->server->put( $self->id, $self->etag, 'application/atom+xml', $xml );
68 0           $self;
69             }
70              
71             sub delete {
72 0     0 0   my $self = shift;
73 0 0         croak "No id set" unless $self->id;
74              
75 0           $self->server->delete( $self->id, $self->etag );
76 0           1;
77             }
78              
79             1;