blib/lib/WWW/Mailgun.pm

filter: Statementstmt 69.3% 77 / 111 Branchbran 47.5% 19 / 40 Conditioncond 60.0% 18 / 30 MC/DCmcdc 29.4% 5 / 17 Subroutinesub 72.2% 13 / 18 Podpod 66.6% 4 / 6 totaltotal 61.2% 136 / 222 SCAR 45.8
CC
35
Cov
63%
_handle_response
19.1
AUTOLOAD
8.7
send
8.1
CRAP 97.1
↑ Summary
1 package WWW::Mailgun;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 require LWP::UserAgent;
10  
11 BEGIN {
12 2 ▶ our $VERSION = 0.53;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 }
14  
15 2 my @IGNORE_DOMAIN = qw/domains/;
16 2 my @GET_METHODS = qw/stats domains log mailboxes/;
17 2 my @POST_METHODS = qw//;
18 2 my @ALL_METHODS = (@GET_METHODS, @POST_METHODS);
19  
20 2 my $ALIAS__OPTION = {
21 attachments => 'attachment',
22 tags => 'o:tag',
23 };
24  
25 2 my $OPTION__MAXIMUM = {
26 "o:tag" => 3,
27 };
28  
29 sub new {
30 4 ▶ my ($class, $param) = @_;
Subroutine new: called CC=5 SCAR=17.1
Pod documented
31  
32 4 ▶ my $Key = $param->{key} // die "You must specify an API Key";
Condition50%1/2
$param->{"key"} // die("You must specify an API Key")
l!l
40
MC/DC0%0/1
$param->{"key"} // die("You must specify an API Key")
$param->{"key"}
Truth table50%1/2
A
A= $param->{"key"}
Aresult
11
00
33 4 ▶ my $Domain = $param->{domain} // die "You need to specify a domain (IE: samples.mailgun.org)";
Condition50%1/2
$param->{"domain"} // die("You need to specify a domain (IE: samples.mailgun.org)")
l!l
40
MC/DC0%0/1
$param->{"domain"} // die("You need to specify a domain (IE: samples.mailgun.org)")
$param->{"domain"}
Truth table50%1/2
A
A= $param->{"domain"}
Aresult
11
00
34 4 ▶ my $Url = $param->{url} // "https://api.mailgun.net/v2";
Condition50%1/2
$param->{"url"} // "https://api.mailgun.net/v2"
l!l
04
MC/DC0%0/1
$param->{"url"} // "https://api.mailgun.net/v2"
$param->{"url"}
Truth table50%1/2
A
A= $param->{"url"}
Aresult
11
00
35 4 ▶ my $From = $param->{from} // "";
Condition100%2/2
$param->{"from"} // ""
l!l
22
MC/DC100%1/1
$param->{"from"} // ""
$param->{"from"}
Truth table100%2/2
A
A= $param->{"from"}
Aresult
11
00
36  
37 4 my $self = {
38 ua => LWP::UserAgent->new,
39 url => $Url . '/',
40 domain => $Domain,
41 from => $From,
42 };
43  
44 $self->{get} = sub {
45 0 ▶ my ($self, $type, $data) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 return my $r = $self->{ua}->get(_get_route($self,[$type, $data]));
47 4 };
48  
49 $self->{del} = sub {
50 0 ▶ my ($self, $type, $data) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
51 return my $r = $self->{ua}->request(
52 0 HTTP::Request->new( 'DELETE', _get_route( $self, [$type, $data] ) )
53 );
54 4 };
55  
56 $self->{post} = sub {
57 4 ▶ my ($self, $type, $data) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
58 4 return my $r = $self->{ua}->post(_get_route($self,$type), Content_Type => 'multipart/form-data', Content => $data);
59 4 };
60  
61 4 $self->{ua}->default_header('Authorization' => 'Basic ' . encode_base64('api:' . $Key));
62  
63 4 return bless $self, $class;
64 }
65  
66 sub _handle_response {
67 4 ▶ my ($response) = shift;
Subroutine _handle_response: called CC=9 SCAR=29.5
68  
69 4 my $rc = $response->code;
70  
71 4 ▶ return 1 if 200 <= $rc && $rc <= 299; # success
Branch: 1 branch
BranchTrueFalseTotal
if 200 <= $rc and $rc <= 299 3 1 4
Condition66%2/3
200 <= $rc and $rc <= 299
!ll&&!rl&&r
013
MC/DC50%1/2
200 <= $rc and $rc <= 299
200 <= $rc $rc <= 299
Truth table66%2/3
A and B
A= 200 <= $rcB= $rc <= 299
ABresult
0X0
100
111
72  
73 1 my $json = from_json($response->decoded_content);
74 1 ▶ if ($json->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"message"}) 0 1 1
75 0 die $response->status_line." ".$json->{message};
76 }
77  
78 1 ▶ die "Bad Request - Often missing a required parameter" if $rc == 400;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 400 0 1 1
79 1 ▶ die "Unauthorized - No valid API key provided" if $rc == 401;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 401 1 0 1
80 0 ▶ die "Request Failed - Parameters were valid but request failed" if $rc == 402;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 402 0 0 0
81 0 ▶ die "Not Found - The requested item doesn’t exist" if $rc == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 404 0 0 0
82 0 ▶ die "Server Errors - something is wrong on Mailgun’s end" if $rc >= 500;
Branch: 1 branch
BranchTrueFalseTotal
if $rc >= 500 0 0 0
83 }
84  
85 sub send {
86 3 ▶ my ($self, $msg) = @_;
Subroutine send: called CC=7 SCAR=20.9
Pod documented
87  
88 $msg->{from} = $msg->{from} || $self->{from}
89 3 ▶ or die "You must specify an email address to send from";
Branch: 1 branch
BranchTrueFalseTotal
$msg->{"from"} = $msg->{"from"} || $self->{"from"} or die "You must specify an email address to send from" 0 3 3
Condition66%2/3
$msg->{'from'} || $self->{'from'}
l!l&&r!l&&!r
210
MC/DC0%0/2
$msg->{'from'} || $self->{'from'}
$msg->{'from'} $self->{'from'}
Truth table66%2/3
A || B
A= $msg->{'from'}B= $self->{'from'}
ABresult
1X1
011
000
90 $msg->{to} = $msg->{to}
91 3 ▶ or die "You must specify an email address to send to";
Branch: 1 branch
BranchTrueFalseTotal
$msg->{"to"} = $msg->{"to"} or die "You must specify an email address to send to" 0 3 3
92 3 ▶ if (ref $msg->{to} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg->{"to"} eq "ARRAY") 0 3 3
93 0 $msg->{to} = join(',',@{$msg->{to}});
94 }
95  
96 3 ▶ $msg->{subject} = $msg->{subject} // "";
Condition50%1/2
$msg->{"subject"} // ""
l!l
30
MC/DC0%0/1
$msg->{"subject"} // ""
$msg->{"subject"}
Truth table50%1/2
A
A= $msg->{"subject"}
Aresult
11
00
97 3 ▶ $msg->{text} = $msg->{text} // "";
Condition100%2/2
$msg->{"text"} // ""
l!l
21
MC/DC100%1/1
$msg->{"text"} // ""
$msg->{"text"}
Truth table100%2/2
A
A= $msg->{"text"}
Aresult
11
00
98  
99 3 my $content = _prepare_content($msg);
100  
101 3 my $r = $self->{post}->($self, 'messages', $content);
102  
103 3 _handle_response($r);
104  
105 3 return from_json($r->decoded_content);
106 }
107  
108 =head2 _prepare_content($option__values) : \@content
109  
110 Given a $option__values hashref, transform it to an arrayref suitable for
111 sending as multipart/form-data. The core logic here is that array references
112 are modified from:
113  
114 option => [ value1, value2, ... ]
115  
116 to
117  
118 [ option => value1, option => value2, ... ]
119  
120 =cut
121  
122 sub _prepare_content {
123 3 ▶ my ($option__values) = @_;
Subroutine _prepare_content: called CC=8 SCAR=20.8
124  
125 3 my $content = [];
126 3 my $option__count = {};
127  
128 3 ▶ while (my ($option, $values) = each %$option__values) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$option__values) 18 3 21
129 18 ▶ $option = $ALIAS__OPTION->{$option} || $option;
Condition66%2/3
$ALIAS__OPTION->{$option} || $option
l!l&&r!l&&!r
1170
MC/DC0%0/2
$ALIAS__OPTION->{$option} || $option
$ALIAS__OPTION->{$option} $option
Truth table66%2/3
A || B
A= $ALIAS__OPTION->{$option}B= $option
ABresult
1X1
011
000
130 18 ▶ $values = ref $values ? $values : [$values];
Branch: 1 branch
BranchTrueFalseTotal
ref $values ? : 3 15 18
131  
132 18 for my $value (@$values) {
133 22 $option__count->{$option}++;
134 22 ▶ if ($OPTION__MAXIMUM->{$option} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($OPTION__MAXIMUM->{$option} and $option__count->{$option} > $OPTION__MAXIMUM->{$option}) 1 21 22
Condition100%3/3
$OPTION__MAXIMUM->{$option} and $option__count->{$option} > $OPTION__MAXIMUM->{$option}
!ll&&!rl&&r
1831
MC/DC100%2/2
$OPTION__MAXIMUM->{$option} and $option__count->{$option} > $OPTION__MAXIMUM->{$option}
$OPTION__MAXIMUM->{$option} $option__count->{$option} > $OPTION__MAXIMUM->{$option}
Truth table100%3/3
A and B
A= $OPTION__MAXIMUM->{$option}B= $option__count->{$option} > $OPTION__MAXIMUM->{$option}
ABresult
0X0
100
111
135 $option__count->{$option} > $OPTION__MAXIMUM->{$option}) {
136 1 warn "Reached max number of $option, skipping...";
137 1 last;
138 }
139 21 ▶ $value = [ $value ] if $option eq 'attachment';
Branch: 1 branch
BranchTrueFalseTotal
if $option eq "attachment" 4 17 21
140 21 push @$content, $option => $value;
141 }
142 }
143  
144 3 return $content;
145 }
146  
147 sub _get_route {
148 4 ▶ my ($self, $path) = @_;
Subroutine _get_route: called CC=4 SCAR=19.4
149  
150 4 ▶ if (ref $path eq 'ARRAY'){
Branch: 2 branches
BranchTrueFalseTotal
if (ref $path eq 'ARRAY') { } 0 4 4
elsif (not $path ~~ \@IGNORE_DOMAIN) { } 4 0 4
151 0 my @clean = grep {defined} @$path;
152 unshift @clean, $self->{domain}
153 0 ▶ unless $clean[-1] ~~ @IGNORE_DOMAIN;
Branch: 1 branch
BranchTrueFalseTotal
unless $clean[-1] ~~ \@IGNORE_DOMAIN 0 0 0
154 0 $path = join('/',@clean);
155 } elsif (!($path ~~ @IGNORE_DOMAIN)) {
156 4 $path = $self->{domain} . '/' . $path
157 }
158 4 return $self->{url} . $path;
159 }
160  
161 sub unsubscribes {
162 1 ▶ my ($self, $method, $data) = @_;
Subroutine unsubscribes: called CC=2 SCAR=7.4
Pod documented
163 1 ▶ $method = $method // 'get';
Condition50%1/2
$method // "get"
l!l
10
MC/DC0%0/1
$method // "get"
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
164  
165 1 my $r = $self->{lc($method)}->($self,'unsubscribes',$data);
166 1 _handle_response($r);
167 0 return from_json($r->decoded_content);
168 }
169  
170 sub complaints {
171 0 ▶ my ($self, $method, $data) = @_;
Subroutine complaints: not called CC=2 SCAR=17.9
Pod undocumented
172 0 ▶ $method = $method // 'get';
Condition0%0/2
$method // "get"
l!l
00
MC/DC0%0/1
$method // "get"
$method
Truth table0%0/2
A
A= $method
Aresult
11
00
173  
174 0 my $r = $self->{lc($method)}->($self,'complaints',$data);
175 0 _handle_response($r);
176 0 return from_json($r->decoded_content);
177 }
178  
179 sub bounces {
180 0 ▶ my ($self, $method, $data) = @_;
Subroutine bounces: not called CC=2 SCAR=17.9
Pod documented
181 0 ▶ $method = $method // 'get';
Condition0%0/2
$method // "get"
l!l
00
MC/DC0%0/1
$method // "get"
$method
Truth table0%0/2
A
A= $method
Aresult
11
00
182  
183 0 my $r = $self->{lc($method)}->($self,'bounces',$data);
184 0 _handle_response($r);
185 0 return from_json($r->decoded_content);
186 }
187  
188 sub logs {
189 ## Legacy support.
190 0 ▶ my $self = shift;
Subroutine logs: not called CC=1 SCAR=6.9
Pod undocumented
191 0 return $self->log();
192 }
193  
194 sub AUTOLOAD { ## Handle generic list of requests.
195 4 ▶ our $AUTOLOAD;
Subroutine AUTOLOAD: called CC=4 SCAR=21.7
196 4 my $self = shift;
197 4 my @ObjParts = split(/\:\:/, $AUTOLOAD);
198 4 my $method = pop(@ObjParts);
199 4 ▶ return if $method eq 'DESTROY'; ## Ignore DESTROY.
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 4 0 4
200 0 ▶ unless ($method ~~ @ALL_METHODS) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($method ~~ \@ALL_METHODS) 0 0 0
201 0 die("Not a valid method, \"$method\".");
202 }
203 0 my $mode = 'get';
204 0 ▶ $mode = 'post' if $method ~~ @POST_METHODS;
Branch: 1 branch
BranchTrueFalseTotal
if $method ~~ \@POST_METHODS 0 0 0
205 0 my $r = $self->{$mode}->($self, $method, @_);
206 0 _handle_response($r);
207 0 return from_json($r->decoded_content);
208 }
209  
210 =pod
211  
212  
213 =head1 NAME
214  
215 WWW::Mailgun - Perl wrapper for Mailgun (L<http://mailgun.org>)
216  
217 =head1 SYNOPSIS
218  
219 use WWW::Mailgun;
220  
221 my $mg = WWW::Mailgun->new({
222 key => 'key-yOuRapiKeY',
223 domain => 'YourDomain.mailgun.org',
224 from => 'elb0w <elb0w@YourDomain.mailgun.org>' # Optionally set here, you can set it when you send
225 });
226  
227 #sending examples below
228  
229 # Get stats http://documentation.mailgun.net/api-stats.html
230 my $obj = $mg->stats;
231  
232 # Get logs http://documentation.mailgun.net/api-logs.html
233 my $obj = $mg->logs;
234  
235  
236 =head1 DESCRIPTION
237  
238 Mailgun is a email service which provides email over a http restful API.
239 These bindings goal is to create a perl interface which allows you to
240 easily leverage it.
241  
242 =head1 USAGE
243  
244 =head2 new({key => 'mailgun key', domain => 'your mailgun domain', from => 'optional from')
245  
246 Creates your mailgun object
247  
248 from => the only optional field, it can be set in the message.
249  
250  
251  
252 =head2 send($data)
253  
254 Send takes in a hash of settings
255 Takes all specificed here L<http://documentation.mailgun.net/api-sending.html>
256 'from' is optionally set here, otherwise you can set it in the constructor and it can be used for everything
257  
258 =item Send a HTML message with optional array of attachments
259  
260 $mg->send({
261 to => 'some_email@gmail.com',
262 subject => 'hello',
263 html => '<html><h3>hello</h3><strong>world</strong></html>',
264 attachment => ['/Users/elb0w/GIT/Personal/Mailgun/test.pl']
265 });
266  
267 =item Send a text message
268  
269 $mg->send({
270 to => 'some_email@gmail.com',
271 subject => 'hello',
272 text => 'Hello there'
273 });
274  
275 =item Send a MIME multipart message
276  
277 $mg->send({
278 to => 'some_email@gmail.com',
279 subject => 'hello',
280 text => 'Hello there',
281 html => '<b>Hello there</b>'
282 });
283  
284  
285 =head2 unsubscribes, bounces, spam
286  
287 Helper methods all take a method argument (del, post, get)
288 L<http://documentation.mailgun.net/api_reference.html>
289 'post' optionally takes a hash of properties
290  
291  
292 =item Unsubscribes
293  
294 # View all unsubscribes L<http://documentation.mailgun.net/api-unsubscribes.html>
295 my $all = $mg->unsubscribes;
296  
297 # Unsubscribe user from all
298 $mg->unsubscribes('post',{address => 'user@website.com', tag => '*'});
299  
300 # Delete a user from unsubscriptions
301 $mg->unsubscribes('del','user@website.com');
302  
303 # Get a user from unsubscriptions
304 $mg->unsubscribes('get','user@website.com');
305  
306  
307  
308 =item Complaints
309  
310 # View all spam complaints L<http://documentation.mailgun.net/api-complaints.html>
311 my $all = $mg->complaints;
312  
313 # Add a spam complaint for a address
314 $mg->complaints('post',{address => 'user@website.com'});
315  
316 # Remove a complaint
317 $mg->complaints('del','user@website.com');
318  
319 # Get a complaint for a adress
320 $mg->complaints('get','user@website.com');
321  
322 =item Bounces
323  
324 # View the list of bounces L<http://documentation.mailgun.net/api-bounces.html>
325 my $all = $mg->bounces;
326  
327 # Add a permanent bounce
328 $mg->bounces('post',{
329 address => 'user@website.com',
330 code => 550, #This is default
331 error => 'Error Description' #Empty by default
332 });
333  
334 # Remove a bounce
335 $mg->bounces('del','user@website.com');
336  
337 # Get a bounce for a specific address
338 $mg->bounces('get','user@website.com');
339  
340 =head1 TODO
341  
342 =item Mailboxes
343  
344 =item Campaigns
345  
346 =item Mailing Lists
347  
348 =item Routes
349  
350 =head1 Author
351  
352 George Tsafas <elb0w@elbowrage.com>
353  
354 =head1 Support
355  
356 elb0w on irc.freenode.net #perl
357 L<https://github.com/gtsafas/mailgun.perl>
358  
359  
360 =head1 Resources
361  
362 L<http://documentation.mailgun.net/>
363  
364  
↑ Summary