| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Email::ConstantContact::Contact; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 3071 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 39 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 163 |  | 
| 5 | 1 |  |  | 1 |  | 7 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 73 |  | 
| 6 | 1 |  |  | 1 |  | 7 | use LWP::UserAgent; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 7 | 1 |  |  | 1 |  | 6 | use HTTP::Request::Common qw(POST GET); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 47 |  | 
| 8 | 1 |  |  | 1 |  | 450 | use XML::Simple; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | use XML::Writer; | 
| 10 |  |  |  |  |  |  | use POSIX qw( strftime ); | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | =head1 NAME | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | Email::ConstantContact::Contact - Internal class to interact with ConstantContact Contact 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 Status EmailAddress EmailType Name FirstName MiddleName LastName JobTitle | 
| 41 |  |  |  |  |  |  | CompanyName HomePhone WorkPhone Addr1 Addr2 Addr3 City StateCode StateName | 
| 42 |  |  |  |  |  |  | CountryCode CountryName PostalCode SubPostalCode Note Confirmed InsertTime | 
| 43 |  |  |  |  |  |  | LastUpdateTime CustomField1 CustomField2 CustomField3 CustomField4 CustomField5 | 
| 44 |  |  |  |  |  |  | CustomField6 CustomField7 CustomField8 CustomField9 CustomField10 CustomField11 | 
| 45 |  |  |  |  |  |  | CustomField12 CustomField13 CustomField14 CustomField15 OptInTime OptInSource | 
| 46 |  |  |  |  |  |  | OptOutTime OptOutSource OptOutReason InsertTime LastUpdateTime | 
| 47 |  |  |  |  |  |  | ); | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub new { | 
| 50 |  |  |  |  |  |  | my $class	= shift; | 
| 51 |  |  |  |  |  |  | my $ccobj	= shift; | 
| 52 |  |  |  |  |  |  | my $data	= shift; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | my $self  = { | 
| 55 |  |  |  |  |  |  | '_cc'			=> $ccobj, | 
| 56 |  |  |  |  |  |  | 'OptInSource'	=> 'ACTION_BY_CUSTOMER', | 
| 57 |  |  |  |  |  |  | 'ContactLists'	=> [] | 
| 58 |  |  |  |  |  |  | }; | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | foreach my $field (@fields) { | 
| 61 |  |  |  |  |  |  | $self->{$field} = $data->{'content'}->{'Contact'}->{$field}; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | if (exists($data->{'link'}) && ref($data->{'link'})) { | 
| 65 |  |  |  |  |  |  | foreach my $link (@{$data->{'link'}}) { | 
| 66 |  |  |  |  |  |  | if ($link->{'rel'} eq 'edit') { | 
| 67 |  |  |  |  |  |  | $self->{'link'} = $link->{'href'}; | 
| 68 |  |  |  |  |  |  | } | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  | if (exists($data->{'content'}->{'Contact'}->{'ContactLists'}) | 
| 72 |  |  |  |  |  |  | && ref($data->{'content'}->{'Contact'}->{'ContactLists'})) { | 
| 73 |  |  |  |  |  |  | foreach my $cl (@{$data->{'content'}->{'Contact'}->{'ContactLists'}}) { | 
| 74 |  |  |  |  |  |  | push (@{$self->{ContactLists}}, $cl->{'id'}); | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | bless ($self, $class); | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | return $self; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub addToList { | 
| 84 |  |  |  |  |  |  | my $self = shift; | 
| 85 |  |  |  |  |  |  | my $list = shift; | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | if (ref($list) eq 'Email::ConstantContact::List') { | 
| 88 |  |  |  |  |  |  | push (@{$self->{ContactLists}}, $list->{id}); | 
| 89 |  |  |  |  |  |  | } | 
| 90 |  |  |  |  |  |  | else { | 
| 91 |  |  |  |  |  |  | push (@{$self->{ContactLists}}, $list); | 
| 92 |  |  |  |  |  |  | } | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | sub clearAllLists { | 
| 96 |  |  |  |  |  |  | my $self = shift; | 
| 97 |  |  |  |  |  |  | $self->{ContactLists} = []; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | sub removeFromList { | 
| 101 |  |  |  |  |  |  | my $self = shift; | 
| 102 |  |  |  |  |  |  | my $list = shift; | 
| 103 |  |  |  |  |  |  | my @newcls; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | foreach my $cl (@{$self->{ContactLists}}) { | 
| 106 |  |  |  |  |  |  | if (ref($list) eq 'Email::ConstantContact::List') { | 
| 107 |  |  |  |  |  |  | push (@newcls, $list->{id}) if ($cl ne $list->{id}); | 
| 108 |  |  |  |  |  |  | } | 
| 109 |  |  |  |  |  |  | else { | 
| 110 |  |  |  |  |  |  | push (@newcls, $list) if ($cl ne $list); | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  | $self->{ContactLists} = \@newcls; | 
| 114 |  |  |  |  |  |  | } | 
| 115 |  |  |  |  |  |  |  | 
| 116 |  |  |  |  |  |  | sub optOut { | 
| 117 |  |  |  |  |  |  | my $self = shift; | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | my $ua = new LWP::UserAgent; | 
| 120 |  |  |  |  |  |  | my $url = lc($self->{id}); | 
| 121 |  |  |  |  |  |  | $url =~ s/^http:/https:/; | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | my $req = new HTTP::Request('DELETE', $url); | 
| 124 |  |  |  |  |  |  | $req->authorization_basic($self->{'_cc'}->{apikey} . '%' . $self->{'_cc'}->{username}, $self->{'_cc'}->{password}); | 
| 125 |  |  |  |  |  |  | $req->content_type('application/atom+xml'); | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | my $res = $ua->request($req); | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | if ($res->code == 204) { | 
| 130 |  |  |  |  |  |  | # Delete is successful | 
| 131 |  |  |  |  |  |  | return 1; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  | else { | 
| 134 |  |  |  |  |  |  | carp "Contact optout request returned code " . $res->status_line; | 
| 135 |  |  |  |  |  |  | } | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | sub save { | 
| 139 |  |  |  |  |  |  | my $self = shift; | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | my $xmlcontent; | 
| 142 |  |  |  |  |  |  | my $writer = new XML::Writer(OUTPUT => \$xmlcontent, DATA_MODE => 1, DATA_INDENT => 1); | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | $writer->startTag('entry', 'xmlns' => 'http://www.w3.org/2005/Atom'); | 
| 145 |  |  |  |  |  |  | $writer->dataElement('id', $self->{'id'}); | 
| 146 |  |  |  |  |  |  | $writer->dataElement('title', $self->{'title'}, type => 'text'); | 
| 147 |  |  |  |  |  |  | $writer->dataElement('author', ''); | 
| 148 |  |  |  |  |  |  | $writer->dataElement('updated', strftime('%Y-%m-%dT%H:%M:%SZ', gmtime())); | 
| 149 |  |  |  |  |  |  | $writer->dataElement('summary', 'ContactList', type => 'text'); | 
| 150 |  |  |  |  |  |  | $writer->startTag('content', 'type' => 'application/vnd.ctct+xml'); | 
| 151 |  |  |  |  |  |  | $writer->startTag('Contact', 'xmlns' => 'http://ws.constantcontact.com/ns/1.0/', | 
| 152 |  |  |  |  |  |  | 'id' => $self->{'id'}); | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | $writer->dataElement('OptInSource', 'ACTION_BY_CUSTOMER'); | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | foreach my $field (@fields) { | 
| 157 |  |  |  |  |  |  | $writer->dataElement($field, $self->{$field}) | 
| 158 |  |  |  |  |  |  | if ($self->{$field}); | 
| 159 |  |  |  |  |  |  | } | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | $writer->startTag('ContactLists'); | 
| 162 |  |  |  |  |  |  | foreach my $cl (@{$self->{ContactLists}}) { | 
| 163 |  |  |  |  |  |  | $writer->dataElement('ContactList', '', id => $cl); | 
| 164 |  |  |  |  |  |  | } | 
| 165 |  |  |  |  |  |  | $writer->endTag('ContactLists'); | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | $writer->endTag('Contact'); | 
| 168 |  |  |  |  |  |  | $writer->endTag('content'); | 
| 169 |  |  |  |  |  |  | $writer->endTag('entry'); | 
| 170 |  |  |  |  |  |  | $writer->end(); | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | my $ua = new LWP::UserAgent; | 
| 173 |  |  |  |  |  |  | my $url = lc($self->{'id'}); | 
| 174 |  |  |  |  |  |  | $url =~ s/^http:/https:/; | 
| 175 |  |  |  |  |  |  |  | 
| 176 |  |  |  |  |  |  | my $req = new HTTP::Request('PUT', $url); | 
| 177 |  |  |  |  |  |  | $req->authorization_basic($self->{'_cc'}->{apikey} . '%' . $self->{'_cc'}->{username}, $self->{'_cc'}->{password}); | 
| 178 |  |  |  |  |  |  | $req->content_type('application/atom+xml'); | 
| 179 |  |  |  |  |  |  | $req->content($xmlcontent); | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | my $res = $ua->request($req); | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | if ($res->is_success) { | 
| 184 |  |  |  |  |  |  | return 1; | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  | else { | 
| 187 |  |  |  |  |  |  | carp "Contact update request returned code " . $res->status_line; | 
| 188 |  |  |  |  |  |  | } | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | } | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | sub create { | 
| 193 |  |  |  |  |  |  | my $self = shift; | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | my $xmlcontent; | 
| 196 |  |  |  |  |  |  | my $writer = new XML::Writer(OUTPUT => \$xmlcontent, DATA_MODE => 1, DATA_INDENT => 1); | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | $writer->startTag('entry', 'xmlns' => 'http://www.w3.org/2005/Atom'); | 
| 199 |  |  |  |  |  |  | $writer->dataElement('id', 'data:,none'); | 
| 200 |  |  |  |  |  |  | $writer->dataElement('title', '', type => 'text'); | 
| 201 |  |  |  |  |  |  | $writer->dataElement('author', ''); | 
| 202 |  |  |  |  |  |  | $writer->dataElement('updated', strftime('%Y-%m-%dT%H:%M:%SZ', gmtime())); | 
| 203 |  |  |  |  |  |  | $writer->dataElement('summary', 'Contact', type => 'text'); | 
| 204 |  |  |  |  |  |  | $writer->startTag('content', 'type' => 'application/vnd.ctct+xml'); | 
| 205 |  |  |  |  |  |  | $writer->startTag('Contact', 'xmlns' => 'http://ws.constantcontact.com/ns/1.0/'); | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | foreach my $field (@fields) { | 
| 208 |  |  |  |  |  |  | $writer->dataElement($field, $self->{$field}) | 
| 209 |  |  |  |  |  |  | if ($self->{$field}); | 
| 210 |  |  |  |  |  |  | } | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | $writer->startTag('ContactLists'); | 
| 213 |  |  |  |  |  |  | foreach my $cl (@{$self->{ContactLists}}) { | 
| 214 |  |  |  |  |  |  | $writer->dataElement('ContactList', '', id => $cl); | 
| 215 |  |  |  |  |  |  | } | 
| 216 |  |  |  |  |  |  | $writer->endTag('ContactLists'); | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | $writer->endTag('Contact'); | 
| 219 |  |  |  |  |  |  | $writer->endTag('content'); | 
| 220 |  |  |  |  |  |  | $writer->endTag('entry'); | 
| 221 |  |  |  |  |  |  | $writer->end(); | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | my $ua = new LWP::UserAgent; | 
| 224 |  |  |  |  |  |  | my $url = lc($self->{'_cc'}->{rooturl} . '/contacts'); | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | my $req = new HTTP::Request('POST', $url); | 
| 227 |  |  |  |  |  |  | $req->authorization_basic($self->{'_cc'}->{apikey} . '%' . $self->{'_cc'}->{username}, $self->{'_cc'}->{password}); | 
| 228 |  |  |  |  |  |  | $req->content_type('application/atom+xml'); | 
| 229 |  |  |  |  |  |  | $req->content($xmlcontent); | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | my $res = $ua->request($req); | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | if ($res->code == 201) { | 
| 234 |  |  |  |  |  |  | # Create is successful | 
| 235 |  |  |  |  |  |  | my $xs = XML::Simple->new(KeyAttr => [], SuppressEmpty => 'undef', | 
| 236 |  |  |  |  |  |  | GroupTags => { ContactLists => 'ContactList' }, ForceArray => ['link','entry','ContactList']); | 
| 237 |  |  |  |  |  |  | my $xmlobj = $xs->XMLin($res->content); | 
| 238 |  |  |  |  |  |  | my $newcontact = new Email::ConstantContact::Contact($self->{'_cc'}, $xmlobj); | 
| 239 |  |  |  |  |  |  | $self = $newcontact; | 
| 240 |  |  |  |  |  |  | return $newcontact; | 
| 241 |  |  |  |  |  |  | } | 
| 242 |  |  |  |  |  |  | else { | 
| 243 |  |  |  |  |  |  | carp "Contact creation request returned code " . $res->status_line; | 
| 244 |  |  |  |  |  |  | return wantarray? (): undef; | 
| 245 |  |  |  |  |  |  | } | 
| 246 |  |  |  |  |  |  | } | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | =head1 AUTHOR | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | Adam Rich, C<<  >> | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | =head1 BUGS | 
| 254 |  |  |  |  |  |  |  | 
| 255 |  |  |  |  |  |  | Please report any bugs or feature requests to C, or through | 
| 256 |  |  |  |  |  |  | the web interface at L.  I will be notified, and then you'll | 
| 257 |  |  |  |  |  |  | automatically be notified of progress on your bug as I make changes. | 
| 258 |  |  |  |  |  |  |  | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | =head1 SUPPORT | 
| 263 |  |  |  |  |  |  |  | 
| 264 |  |  |  |  |  |  | You can find documentation for this module with the perldoc command. | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | perldoc Email::ConstantContact::Contact | 
| 267 |  |  |  |  |  |  |  | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | You can also look for information at: | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | =over 4 | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | =item * RT: CPAN's request tracker | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  | L | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 278 |  |  |  |  |  |  |  | 
| 279 |  |  |  |  |  |  | L | 
| 280 |  |  |  |  |  |  |  | 
| 281 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 282 |  |  |  |  |  |  |  | 
| 283 |  |  |  |  |  |  | L | 
| 284 |  |  |  |  |  |  |  | 
| 285 |  |  |  |  |  |  | =item * Search CPAN | 
| 286 |  |  |  |  |  |  |  | 
| 287 |  |  |  |  |  |  | L | 
| 288 |  |  |  |  |  |  |  | 
| 289 |  |  |  |  |  |  | =back | 
| 290 |  |  |  |  |  |  |  | 
| 291 |  |  |  |  |  |  |  | 
| 292 |  |  |  |  |  |  | =head1 ACKNOWLEDGEMENTS | 
| 293 |  |  |  |  |  |  |  | 
| 294 |  |  |  |  |  |  |  | 
| 295 |  |  |  |  |  |  | =head1 COPYRIGHT & LICENSE | 
| 296 |  |  |  |  |  |  |  | 
| 297 |  |  |  |  |  |  | Copyright 2009-2011 Adam Rich, all rights reserved. | 
| 298 |  |  |  |  |  |  |  | 
| 299 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it | 
| 300 |  |  |  |  |  |  | under the same terms as Perl itself. | 
| 301 |  |  |  |  |  |  |  | 
| 302 |  |  |  |  |  |  |  | 
| 303 |  |  |  |  |  |  | =cut | 
| 304 |  |  |  |  |  |  |  | 
| 305 |  |  |  |  |  |  | 1; # End of Email::ConstantContact::Contact |