package II0e2073608046581d7be737ae6b5074c0::mod_mailchimp; use strict; use Data::Dumper; use LWP::UserAgent; sub checkSettings { my $class = shift; my $this = shift; my $settings = $this->unpackSettings; return ['error.missing_config'] if grep { ! $settings->{$_} } qw/apikey list_id/; my $content = $class->_call_api($this, 'ping'); return ['error.connect', $content] unless $content =~ /Chimpy\!/; return; } sub subscribe { my $class = shift; my ($this, $user) = @_; $class->_call_api($this, "lists/" . $this->{list_id} . "/members", { email_address => $user->{email}, }); return; } sub _call_api { my $class = shift; my ($this, $method, $params) = @_; my $settings = $this->unpackSettings; unless ($settings->{apikey} && $settings->{list_id}) { warn "Autoresponder Mailchimp #$this->{id}: apikey or list id is missing"; return; } my ($dc) = ($settings->{apikey} =~ /\-(\w+)$/); my $url = "https://" . ($dc || 'us1') . ".api.mailchimp.com/3.0/" . $method; my $ua = LWP::UserAgent->new; $ua->timeout(20); $ua->env_proxy; my $req = HTTP::Request->new($method == 'ping' ? 'GET' : 'POST', $url); $req->content_type('application/json'); $req->header( Authorization => "apikey " . $settings->{apikey}); unless ($method == 'ping') { $req->content('{"email_address":"' . $params->{email_address}  . '", "status":"subscribed"}'); } my $response = $ua->request($req); warn "MAILCHIMP API CALL: $this->{url}\n";
use Data::Dumper;
warn "RESPONSE status: " . $response->status_line;
warn "RESPONSE content: " . $response->content;
$response->content; } 1; 