File Coverage

blib/lib/Email/ConstantContact/List.pm
Criterion Covered Total %
statement 16 18 88.8
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 22 24 91.6


line stmt bran cond sub pod time code
1             package Email::ConstantContact::List;
2              
3 1     1   6 use warnings;
  1         2  
  1         32  
4 1     1   5 use strict;
  1         3  
  1         29  
5 1     1   6 use Carp;
  1         2  
  1         69  
6 1     1   13 use LWP::UserAgent;
  1         2  
  1         30  
7 1     1   990 use HTTP::Request::Common qw(POST GET);
  1         2420  
  1         84  
8 1     1   510 use XML::Simple;
  0            
  0            
9             use XML::Writer;
10             use POSIX qw( strftime );
11              
12             =head1 NAME
13              
14             Email::ConstantContact::List - Internal class to interact with ConstantContact ContactList Objects.
15              
16             =head1 VERSION
17              
18             Version 0.05
19              
20             =cut
21              
22             use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
23              
24             require Exporter;
25              
26             @ISA = qw(Exporter);
27             @EXPORT = qw( );
28              
29             $VERSION = '0.05';
30              
31             =head1 SYNOPSIS
32              
33             This module is not typically used directly, but internally by the main
34             Email::ConstantContact object for processing requests.
35              
36             =cut
37              
38              
39             my @fields = qw (
40             id OptInDefault DisplayOnSignup Name ShortName SortOrder
41             );
42              
43             sub new {
44             my $class = shift;
45             my $ccobj = shift;
46             my $data = shift;
47             my $self = {'_cc' => $ccobj};
48              
49             foreach my $field (@fields) {
50             $self->{$field} = $data->{'content'}->{'ContactList'}->{$field};
51             }
52              
53             if (exists($data->{'link'}) && ref($data->{'link'})) {
54             foreach my $link (@{$data->{'link'}}) {
55             if ($link->{'rel'} eq 'edit') {
56             $self->{'link'} = $link->{'href'};
57             }
58             }
59             }
60              
61             bless ($self, $class);
62             return $self;
63             }
64              
65             sub remove {
66             my $self = shift;
67              
68             my $ua = new LWP::UserAgent;
69             my $url = lc($self->id);
70             $url =~ s/^http:/https:/;
71              
72             my $req = new HTTP::Request('DELETE', $url);
73             $req->authorization_basic($self->{'_cc'}->{apikey} . '%' . $self->{'_cc'}->{username}, $self->{'_cc'}->{password});
74             $req->content_type('application/atom+xml');
75              
76             my $res = $ua->request($req);
77              
78             if ($res->code == 204) {
79             # Delete is successful
80             return 1;
81             }
82             else {
83             carp "Contact List removal request returned code " . $res->status_line;
84             }
85             }
86              
87             sub save {
88             my $self = shift;
89              
90             my $xmlcontent;
91             my $writer = new XML::Writer(OUTPUT => \$xmlcontent, DATA_MODE => 1, DATA_INDENT => 1);
92              
93             $writer->startTag('entry', 'xmlns' => 'http://www.w3.org/2005/Atom');
94             $writer->dataElement('id', $self->{'id'});
95             $writer->dataElement('title', $self->{'title'}, type => 'text');
96             $writer->dataElement('author', '');
97             $writer->dataElement('updated', strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()));
98             $writer->dataElement('summary', 'ContactList', type => 'text');
99             $writer->startTag('content', 'type' => 'application/vnd.ctct+xml');
100             $writer->startTag('ContactList', 'xmlns' => 'http://ws.constantcontact.com/ns/1.0/',
101             'id' => $self->{'id'});
102              
103             foreach my $field (@fields) {
104             $writer->dataElement($field, $self->{$field})
105             if ($self->{$field});
106             }
107              
108             $writer->endTag('ContactList');
109             $writer->endTag('content');
110             $writer->endTag('entry');
111             $writer->end();
112              
113             my $ua = new LWP::UserAgent;
114             my $url = lc($self->{'id'});
115             $url =~ s/^http:/https:/;
116              
117             my $req = new HTTP::Request('PUT', $url);
118             $req->authorization_basic($self->{'_cc'}->{apikey} . '%' . $self->{'_cc'}->{username}, $self->{'_cc'}->{password});
119             $req->content_type('application/atom+xml');
120             $req->content($xmlcontent);
121              
122             my $res = $ua->request($req);
123              
124             if ($res->is_success) {
125             return 1;
126             }
127             else {
128             carp "ContactList update request returned code " . $res->status_line;
129             }
130              
131             }
132              
133             sub create {
134             my $self = shift;
135              
136             my $xmlcontent;
137             my $writer = new XML::Writer(OUTPUT => \$xmlcontent, DATA_MODE => 1, DATA_INDENT => 1);
138              
139             $writer->startTag('entry', 'xmlns' => 'http://www.w3.org/2005/Atom');
140             $writer->dataElement('id', 'data:,none');
141             $writer->dataElement('title', '', type => 'text');
142             $writer->dataElement('author', '');
143             $writer->dataElement('updated', strftime('%Y-%m-%dT%H:%M:%SZ', gmtime()));
144             $writer->dataElement('summary', 'ContactList', type => 'text');
145             $writer->startTag('content', 'type' => 'application/vnd.ctct+xml');
146             $writer->startTag('ContactList', 'xmlns' => 'http://ws.constantcontact.com/ns/1.0/');
147              
148             foreach my $field (@fields) {
149             $writer->dataElement($field, $self->{$field})
150             if ($self->{$field});
151             }
152              
153             $writer->endTag('ContactList');
154             $writer->endTag('content');
155             $writer->endTag('entry');
156             $writer->end();
157              
158             my $ua = new LWP::UserAgent;
159             my $url = lc($self->{'_cc'}->{rooturl} . '/lists');
160             my $req = new HTTP::Request('POST', $url);
161             $req->authorization_basic($self->{'_cc'}->{apikey} . '%' . $self->{'_cc'}->{username}, $self->{'_cc'}->{password});
162             $req->content_type('application/atom+xml');
163             $req->content($xmlcontent);
164              
165             my $res = $ua->request($req);
166              
167             if ($res->code == 201) {
168             # Create is successful
169             my $xs = XML::Simple->new(KeyAttr => [], ForceArray => ['link','entry']);
170             my $xmlobj = $xs->XMLin($res->content);
171             my $newlist = new Email::ConstantContact::List($self->{'_cc'}, $xmlobj);
172            
173             $self = $newlist;
174             return $newlist;
175             }
176             else {
177             carp "Contact List creation request returned code " . $res->status_line;
178             return wantarray? (): undef;
179             }
180             }
181              
182              
183             =head1 AUTHOR
184              
185             Adam Rich, C<< >>
186              
187             =head1 BUGS
188              
189             Please report any bugs or feature requests to C, or through
190             the web interface at L. I will be notified, and then you'll
191             automatically be notified of progress on your bug as I make changes.
192              
193              
194              
195              
196             =head1 SUPPORT
197              
198             You can find documentation for this module with the perldoc command.
199              
200             perldoc Email::ConstantContact::List
201              
202              
203             You can also look for information at:
204              
205             =over 4
206              
207             =item * RT: CPAN's request tracker
208              
209             L
210              
211             =item * AnnoCPAN: Annotated CPAN documentation
212              
213             L
214              
215             =item * CPAN Ratings
216              
217             L
218              
219             =item * Search CPAN
220              
221             L
222              
223             =back
224              
225              
226             =head1 ACKNOWLEDGEMENTS
227              
228              
229             =head1 COPYRIGHT & LICENSE
230              
231             Copyright 2009-2011 Adam Rich, all rights reserved.
232              
233             This program is free software; you can redistribute it and/or modify it
234             under the same terms as Perl itself.
235              
236              
237             =cut
238              
239             1; # End of Email::ConstantContact::List