File Coverage

blib/lib/Net/MailChimp.pm
Criterion Covered Total %
statement 26 61 42.6
branch 0 14 0.0
condition n/a
subroutine 9 15 60.0
pod 0 4 0.0
total 35 94 37.2


line stmt bran cond sub pod time code
1             package Net::MailChimp {
2 1     1   99710 use Moo;
  1         10197  
  1         6  
3 1     1   3189 use Mojo::UserAgent;
  1         649861  
  1         13  
4 1     1   88 use Carp qw/croak confess/;
  1         2  
  1         82  
5 1     1   7 use List::Util qw/first/;
  1         4  
  1         81  
6 1     1   22 use Mojo::Util qw/url_escape url_unescape/;
  1         2  
  1         95  
7 1     1   8 use Digest::MD5 qw/md5_hex/;
  1         2  
  1         73  
8 1     1   685 use namespace::clean;
  1         23323  
  1         9  
9 1     1   960 use version;
  1         2582  
  1         7  
10 1     1   118 use v5.36;
  1         8  
11              
12             our $VERSION = qv("v0.2.0");
13              
14             has endpoint_uri => ( is => 'ro', default => sub {
15             'https://' . $_[0]->dc . '.api.mailchimp.com/' . $_[0]->api_version . '/'
16             } );
17             has dc => ( is => 'ro' );
18             has api_key => ( is => 'ro' );
19             has api_version => ( is => 'ro', default => '3.0' );
20             has request_timeout => ( is => 'ro', default => sub { 20 } );
21             has connect_timeout => ( is => 'ro', default => sub { 15 } );
22             has ua => ( is => 'ro', lazy => 1, default => sub {
23             Mojo::UserAgent->new()->connect_timeout($_[0]->connect_timeout)->inactivity_timeout($_[0]->request_timeout)
24             } );
25              
26             sub BUILD {
27 0     0 0   my ($self, $args) = @_;
28              
29 0 0         croak 'Please provide dc' if !exists $args->{dc};
30 0 0         croak 'Please provide api_key' if !exists $args->{api_key};
31             }
32              
33 0     0 0   sub request($self, $path, $method, $args = {}) {
  0            
  0            
  0            
  0            
  0            
34 0 0         croak 'Please provide path' if !defined $path;
35 0 0         croak 'Invalid path' if $path !~ m/\w+/xs;
36 0           $method = $self->_validate_method($method);
37              
38 0           my $reqargs = {
39             %$args,
40             };
41              
42 0 0         my $datatransport = $method eq 'get' ? 'form' : 'json';
43              
44 0           my $res = $self->ua->$method( $self->endpoint_uri . "$path" =>
45             {
46             Authorization => 'Bearer ' . $self->api_key,
47             },
48             $datatransport => $reqargs
49             )->result;
50              
51             # We have the caller handle errors, as some like 400-not_found can happen
52 0 0         if ( !$res->is_success ) {
53 0           return { httpstatus => $res->code, message => $res->body };
54             }
55              
56 0           return $res->json;
57             }
58              
59 0     0 0   sub md5($self, $string) {
  0            
  0            
  0            
60 0           return md5_hex($string);
61             }
62              
63 0     0 0   sub md5address($self, $string) {
  0            
  0            
  0            
64 0           return $self->md5(lc $string);
65             }
66              
67 0     0     sub _validate_method($self, $method) {
  0            
  0            
  0            
68 0 0   0     confess 'Invalid-method' if !defined first { $_ eq uc($method) } qw/GET POST PUT DELETE/;
  0            
69 0           return lc $method;
70             }
71             }
72              
73             1;
74              
75             =head1 NAME
76              
77             Net::MailChimp - Perl library with MINIMAL interface to use MailChimp API.
78              
79             =head1 SYNOPSIS
80              
81             use Net::MailChimp;
82              
83             my $mc = Net::MailChimp->new(
84             api_key => 'xxxxxxxx'
85             dc => 'us21',
86             version => '3.0', # Optional, default is 3.0
87             );
88              
89             my $res;
90              
91             # md5address() turns lowerscase and returns MD5, as MailChimp wants
92             my $mailhash = $mc->md5address('test@test.com');
93              
94             my $res = $mch->request('lists/00000/members/'.$mailhash, 'GET');
95             # The module will never die, as most MailChimp errors require processing by the caller
96             say $res->{message} if $res->{httpstatus};
97             say $res->{status}; # subscribed, pending, unsubscribed, cleaned
98              
99             $mch->request('lists/00000/members/', 'POST', {
100             email_address => 'test2@test.com',
101             {
102             status => 'pending',
103             merge_fields => {
104             FNAME => 'Test1',
105             }
106             }
107             });
108              
109              
110             =head1 DESCRIPTION
111              
112             This is HIGHLY EXPERIMENTAL and in the works, do not use for now.
113              
114             =head1 AUTHOR
115              
116             Michele Beltrame, C
117              
118             =head1 LICENSE
119              
120             This library is free software under the Artistic License 2.0.
121              
122             =cut