blib/lib/Net/MailChimp.pm

filter: Statementstmt 62.0% 36 / 58 Branchbran 0.0% 0 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 47.3% 9 / 19 Podpod 0.0% 0 / 4 totaltotal 47.3% 45 / 95 CC 9 SCAR 24.8
CC
9
Cov
50%
request
42.0
BUILD
12.0
_validate_method
6.0
CRAP 11.9
↑ Summary
1 1 package Net::MailChimp {
2 1 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶ use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶ use Carp qw/croak confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶ use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶ use Mojo::Util qw/url_escape url_unescape/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶ use Digest::MD5 qw/md5_hex/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶ use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our $VERSION = qv("v0.2.0");
13  
14 0 ▶ has endpoint_uri => ( is => 'ro', default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
15 0 'https://' . $_[0]->dc . '.api.mailchimp.com/' . $_[0]->api_version . '/'
16 1 } );
17 1 has dc => ( is => 'ro' );
18 1 has api_key => ( is => 'ro' );
19 1 has api_version => ( is => 'ro', default => '3.0' );
20 0 ▶ has request_timeout => ( is => 'ro', default => sub { 20 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
21 0 ▶ has connect_timeout => ( is => 'ro', default => sub { 15 } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
22 0 ▶ has ua => ( is => 'ro', lazy => 1, default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 Mojo::UserAgent->new()->connect_timeout($_[0]->connect_timeout)->inactivity_timeout($_[0]->request_timeout)
24 1 } );
25  
26 0 ▶ sub BUILD {
Subroutine BUILD: not called CC=3 SCAR=24.8
Pod undocumented
27 0 my ($self, $args) = @_;
28  
29 0 ▶ croak 'Please provide dc' if !exists $args->{dc};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args->{"dc"} 0 0 0
30 0 ▶ croak 'Please provide api_key' if !exists $args->{api_key};
Branch: 1 branch
BranchTrueFalseTotal
if not exists $args->{"api_key"} 0 0 0
31 }
32  
33 0 ▶ sub request($self, $path, $method, $args = {}) {
Subroutine request: not called CC=6 SCAR=37.4
Pod undocumented
34 0 ▶ croak 'Please provide path' if !defined $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 0 0
35 0 ▶ croak 'Invalid path' if $path !~ m/\w+/xs;
Branch: 1 branch
BranchTrueFalseTotal
unless $path =~ /\w+/sux 0 0 0
36 0 $method = $self->_validate_method($method);
37  
38 0 my $reqargs = {
39 %$args,
40 };
41  
42 0 ▶ my $datatransport = $method eq 'get' ? 'form' : 'json';
Branch: 1 branch
BranchTrueFalseTotal
$method eq 'get' ? : 0 0 0
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 ▶ if ( !$res->is_success ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->is_success) 0 0 0
53 0 return { httpstatus => $res->code, message => $res->body };
54 }
55  
56 0 return $res->json;
57 }
58  
59 0 ▶ sub md5($self, $string) {
Subroutine md5: not called CC=1 SCAR=6.9
Pod undocumented
60 0 return md5_hex($string);
61 }
62  
63 0 ▶ sub md5address($self, $string) {
Subroutine md5address: not called CC=1 SCAR=6.9
Pod undocumented
64 0 return $self->md5(lc $string);
65 }
66  
67 0 ▶ sub _validate_method($self, $method) {
Subroutine _validate_method: not called CC=2 SCAR=17.9
68 0 ▶ confess 'Invalid-method' if !defined first { $_ eq uc($method) } qw/GET POST PUT DELETE/;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined &first(sub { $_ eq uc $method; } , "GET", "POST", "PUT", "DELETE") 0 0 0
69 0 return lc $method;
70 }
71 }
72  
73 1 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<mb@blendgroup.it>
117  
118 =head1 LICENSE
119  
120 This library is free software under the Artistic License 2.0.
121  
122 =cut
↑ Summary