blib/lib/WebService/Mailgun.pm

filter: Statementstmt 93.7% 256 / 273 Branchbran 92.3% 109 / 118 Conditioncond 82.1% 78 / 95 MC/DCmcdc 64.0% 32 / 50 Subroutinesub 93.4% 43 / 46 Podpod 79.1% 19 / 24 totaltotal 88.6% 537 / 606 CC 95 SCAR 20.5
CC
95
Cov
91%
get_message_from_event
16.0
recursive
15.0
mime
14.7
CRAP 7.8
↑ Summary
1 package WebService::Mailgun;
2  
3  
4 15 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 15 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Scalar::Util qw(blessed reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use overload ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 15 our $VERSION = "1.1.0";
19 15 our $API_BASE = 'api.mailgun.net/v3';
20 15 our $API_BASE_EU = 'api.eu.mailgun.net/v3';
21 15 our $API_BASE_AU = 'api.au.mailgun.net/v3';
22  
23 15 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
24 15 new => 1,
25 rw => [qw(api_key domain RaiseError region timeout)],
26 ro => [qw(error error_status error_response)],
27 15 );
28  
29 # The body of a response as a hash reference, or undef when it is not a JSON object.
30 106 ▶sub _json_object {
Subroutine _json_object: called CC=2 SCAR=6.9
31 106 my ($res) = @_;
32  
33 106 ▶ my $json = try { decode_json $res->content };
Subroutine __ANON__: called CC=1 SCAR=0.0
34 106 ▶ return ref $json eq 'HASH' ? $json : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $json eq 'HASH' ? : 99 7 106
35 }
36  
37 107 ▶sub decode_response {
Subroutine decode_response: called CC=9 SCAR=22.0
Pod undocumented
38 107 my ($self, $res) = @_;
39  
40 # Every endpoint answers with a JSON object. A success that is not one (a page from a proxy,
41 # say) is a failure too, so that RaiseError decides whether it dies, as for any other (#35).
42 # An empty body (204 No Content) is a success with nothing in it. The body goes into the
43 # message cut short: a proxy's HTML page would otherwise fill the logs.
44 107 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) 97 10 107
45 97 ▶ my $content = $res->content // '';
Condition50%1/2
$res->content // ""
l!l
970
MC/DC0%0/1
$res->content // ""
$res->content
Truth table50%1/2
A
A= $res->content
Aresult
11
00
46 97 ▶ return {} if $content eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $content eq "" 1 96 97
47 96 my $json = _json_object($res);
48 96 ▶ return $json if $json;
Branch: 1 branch
BranchTrueFalseTotal
if $json 91 5 96
49 5 ▶ my $start = length $content > 200 ? substr($content, 0, 200) . '...' : $content;
Branch: 1 branch
BranchTrueFalseTotal
length $content > 200 ? : 1 4 5
50 5 return $self->_fail("the response is not a JSON object: $start", $res);
51 }
52  
53 10 my $json = _json_object($res);
54 my $message = $json && defined $json->{message} && !ref $json->{message}
55 10 ▶ ? $json->{message} : $res->content;
Branch: 1 branch
BranchTrueFalseTotal
$json && defined $json->{'message'} && !ref($json->{'message'}) ? : 4 6 10
Condition100%6/6
$json and defined $json->{"message"} and not ref $json->{"message"}
!ll&&!rl&&r
514
$json and defined $json->{"message"}
!ll&&!rl&&r
235
MC/DC100%3/3
$json and defined $json->{"message"} and not ref $json->{"message"}
$json defined $json->{"message"} not ref $json->{"message"}
Truth table100%4/4
A and B and not(C)
A= $jsonB= defined $json->{"message"}C= not ref $json->{"message"}
ABCresult
0XX0
10X0
1100
1111
56 10 return $self->_fail($message, $res);
57 }
58  
59 # Records a failure in error and error_status, and dies with RaiseError. Without it, returns
60 # nothing, like the methods do on a failure.
61 22 ▶sub _fail {
Subroutine _fail: called CC=2 SCAR=6.9
62 22 my ($self, $message, $res) = @_;
63  
64 22 $self->{error} = $message;
65 22 $self->{error_status} = $res->status_line;
66 # The response itself, for what error and error_status leave out: the headers of a 429, say,
67 # which tell when to try again (#43).
68 22 $self->{error_response} = _without_request($res);
69 22 ▶ if ($self->RaiseError) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->RaiseError) 2 20 22
70 2 carp $self->error;
71 2 croak $self->error_status;
72 }
73 20 return;
74 }
75  
76 # A copy of a Furl::Response without the request it keeps (request_src, and captured_req_* when
77 # Furl captured them): the URL of the request carries the API key, and its body every attachment.
78 # error_response would otherwise hold both until the next failure.
79 22 ▶sub _without_request {
Subroutine _without_request: called CC=4 SCAR=14.0
80 22 my ($res) = @_;
81  
82 22 ▶ return $res unless blessed($res) && $res->isa('Furl::Response') && reftype($res) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($res) and $res->isa("Furl::Response") and builtin::reftype($res) eq "HASH" 16 6 22
Condition66%4/6
builtin::blessed($res) and $res->isa("Furl::Response") and builtin::reftype($res) eq "HASH"
!ll&&!rl&&r
1606
builtin::blessed($res) and $res->isa("Furl::Response")
!ll&&!rl&&r
0166
MC/DC33%1/3
builtin::blessed($res) and $res->isa("Furl::Response") and builtin::reftype($res) eq "HASH"
builtin::blessed($res) $res->isa("Furl::Response") builtin::reftype($res) eq "HASH"
Truth table50%2/4
A and B and C
A= builtin::blessed($res)B= $res->isa("Furl::Response")C= builtin::reftype($res) eq "HASH"
ABCresult
0XX0
10X0
1100
1111
83 6 my $copy = bless { %$res }, ref $res;
84 6 delete @{$copy}{qw(request_src captured_req_headers captured_req_content)};
85 6 return $copy;
86 }
87  
88 37 ▶sub recursive {
Subroutine recursive: called CC=15 SCAR=27.1
Pod undocumented
89 37 my ($self, $method, $query, $key, $api_uri) = @_;
90  
91 37 ▶ $query //= {};
Condition50%1/2
$query //= {}
l!l
370
MC/DC0%0/1
$query //= {}
$query
Truth table50%1/2
A
A= $query
Aresult
11
00
92 37 ▶ $key //= 'items';
Condition100%2/2
$key //= "items"
l!l
1621
MC/DC100%1/1
$key //= "items"
$key
Truth table100%2/2
A
A= $key
Aresult
11
00
93 37 my @result;
94 my $next;
95 37 ▶ unless($api_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api_uri) 21 16 37
96 21 $api_uri = URI->new($self->api_url($method));
97 21 $api_uri->query_form($query);
98 }
99  
100 # The URLs fetched so far, without the API key: a paging.next that points back to one of them
101 # would go round for ever (#35).
102 37 my %seen;
103 my $last_res;
104 37 while (1) {
105 46 my $plain = $api_uri->clone;
106 46 $plain->userinfo(undef);
107 46 ▶ if ($seen{"$plain"}++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{"$plain"}++) 2 44 46
108 # Not a URL fetched: the failure is told with the response that pointed back.
109 2 $self->_fail("paging.next points back to $plain", $last_res);
110 2 $next = $api_uri;
111 2 last;
112 }
113 44 my $res = $self->client->get($api_uri->as_string);
114 44 $last_res = $res;
115 44 my $json = $self->decode_response($res);
116 # A page without the list of items is a failure, like a request that failed (#35).
117 44 ▶ if ($json && ref $json->{$key} ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($json and ref $json->{$key} ne "ARRAY") 2 42 44
Condition100%3/3
$json and ref $json->{$key} ne "ARRAY"
!ll&&!rl&&r
2402
MC/DC100%2/2
$json and ref $json->{$key} ne "ARRAY"
$json ref $json->{$key} ne "ARRAY"
Truth table100%3/3
A and B
A= $jsonB= ref $json->{$key} ne "ARRAY"
ABresult
0X0
100
111
118 2 $json = $self->_fail("the response has no '$key' list", $res);
119 }
120 43 ▶ unless ($json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json) 3 40 43
121 # The request failed: return the URL of the page that failed, so that the caller can
122 # fetch the rest from there.
123 3 $next = $api_uri;
124 3 last;
125 }
126 40 push @result, @{$json->{$key}};
127 40 ▶ my $page_next = ref $json->{paging} eq 'HASH' && $json->{paging}->{next};
Condition100%3/3
ref $json->{'paging'} eq 'HASH' && $json->{'paging'}{'next'}
!ll&&!rl&&r
22117
MC/DC100%2/2
ref $json->{'paging'} eq 'HASH' && $json->{'paging'}{'next'}
ref $json->{'paging'} eq 'HASH' $json->{'paging'}{'next'}
Truth table100%3/3
A && B
A= ref $json->{'paging'} eq 'HASH'B= $json->{'paging'}{'next'}
ABresult
0X0
100
111
128 # The API key is set on paging.next to fetch it, so it has to be an https URL on the
129 # API's host, as for event($url). Any other (a relative URL, say) is a failure.
130 40 ▶ if ($page_next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page_next) 17 23 40
131 17 $page_next = $self->_api_host_url($page_next);
132 17 ▶ unless ($page_next) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($page_next) 3 14 17
133 3 my ($host) = split m{/}, $self->api_base;
134 3 $self->_fail("paging.next is not an https URL on $host", $res);
135 3 $next = $api_uri;
136 3 last;
137 }
138 # The query of the first page (`subscribed` of list_members, say) goes on every
139 # page: a key that paging.next leaves out is put back, so that the later pages are
140 # not fetched without it (#41). A key it has is left as Mailgun gave it.
141 14 my %has = $page_next->query_form;
142 14 my @missing = grep { !exists $has{$_} } sort keys %$query;
143 14 ▶ $page_next->query_form($page_next->query_form, map { ($_ => $query->{$_}) } @missing)
Branch: 1 branch
BranchTrueFalseTotal
if @missing 2 12 14
144 if @missing;
145 }
146 37 ▶ unless (scalar @{$json->{$key}} && $page_next) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$json->{$key};} and $page_next) 28 9 37
Condition100%3/3
scalar @{$json->{$key};} and $page_next
!ll&&!rl&&r
2539
MC/DC100%2/2
scalar @{$json->{$key};} and $page_next
scalar @{$json->{$key};} $page_next
Truth table100%3/3
A and B
A= scalar @{$json->{$key};}B= $page_next
ABresult
0X0
100
111
147 # The end: an empty page, or one with no paging.next. paging.next of the empty page
148 # is where the items that come later will be; for events this is how Event Polling
149 # goes on (#25). paging.previous would point back over the last page and return it
150 # again. A page with no paging.next gives its own URL instead, never undef: passed
151 # back, `event(undef)` would fetch every event again.
152 28 ▶ $next = $page_next || $api_uri;
Condition66%2/3
$page_next || $api_uri
l!l&&r!l&&!r
5230
MC/DC0%0/2
$page_next || $api_uri
$page_next $api_uri
Truth table66%2/3
A || B
A= $page_nextB= $api_uri
ABresult
1X1
011
000
153 28 last;
154 }
155 9 $api_uri = $page_next;
156 9 $api_uri->userinfo('api:'.$self->api_key);
157 }
158  
159 # The URL goes back without the API key: a caller of Event Polling keeps it (in a database,
160 # say) or logs it, and event($url) sets the key again before fetching it (#38).
161 36 $next = $next->clone;
162 36 $next->userinfo(undef);
163 36 return \@result, $next;
164 }
165  
166 # A copy of $url (a URI or a string) as a URI when it is an https URL on the API's host, or
167 # undef. Only such a URL gets the API key.
168 31 ▶sub _api_host_url {
Subroutine _api_host_url: called CC=3 SCAR=11.0
169 31 my ($self, $url) = @_;
170  
171 31 ▶ my $uri = blessed($url) ? $url->clone : URI->new($url);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($url) ? : 1 30 31
172 31 my ($host) = split m{/}, $self->api_base;
173 31 ▶ return unless _https_host($uri) eq lc($host);
Branch: 1 branch
BranchTrueFalseTotal
unless _https_host($uri) eq lc $host 9 22 31
174 22 return $uri;
175 }
176  
177 # The host of an https URI in lower case, or '' for any other URI. A host with anything but
178 # letters, digits, `.` and `-` is '' too: URI decodes `%2F` in the host while the request goes
179 # out with the string as it is, so `evil.example.com%2Fx.mailgun.net` would pass a check on the
180 # name it ends with (#42).
181 46 ▶sub _https_host {
Subroutine _https_host: called CC=5 SCAR=16.1
182 46 my ($uri) = @_;
183  
184 46 ▶ return '' unless ($uri->scheme // '') eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
unless ($uri->scheme // "") eq "https" 7 39 46
Condition100%2/2
$uri->scheme // ""
l!l
424
MC/DC100%1/1
$uri->scheme // ""
$uri->scheme
Truth table100%2/2
A
A= $uri->scheme
Aresult
11
00
185 39 ▶ my $host = lc($uri->host // '');
Condition100%2/2
$uri->host // ""
l!l
381
MC/DC100%1/1
$uri->host // ""
$uri->host
Truth table100%2/2
A
A= $uri->host
Aresult
11
00
186 39 ▶ return $host =~ /\A[a-z0-9.-]+\z/ ? $host : '';
Branch: 1 branch
BranchTrueFalseTotal
$host =~ /\A[a-z0-9.-]+\z/u ? : 37 2 39
187 }
188  
189 117 ▶sub client {
Subroutine client: called CC=3 SCAR=11.1
Pod documented
190 117 my $self = shift;
191  
192 # The client is made on the first request, so timeout has to be set before it (#40).
193 117 ▶ $self->{_client} //= Furl->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $self->timeout ? : 3 3 6
Condition66%2/3
$self->{"_client"} //= "Furl"->new("agent", 'WebService::Mailgun/' . $VERSION, defined $self->timeout ? ("timeout", $self->timeout) : ())
l!l&&r!l&&!r
11160
MC/DC0%0/2
$self->{"_client"} //= "Furl"->new("agent", 'WebService::Mailgun/' . $VERSION, defined $self->timeout ? ("timeout", $self->timeout) : ())
$self->{"_client"} "Furl"->new("agent", 'WebService::Mailgun/' . $VERSION, defined $self->timeout ? ("timeout", $self->timeout) : ())
Truth table66%2/3
A //= B
A= $self->{"_client"}B= "Furl"->new("agent", 'WebService::Mailgun/' . $VERSION, defined $self->timeout ? ("timeout", $self->timeout) : ())
ABresult
1X1
011
000
194 agent => __PACKAGE__ . '/' . $VERSION,
195 (defined $self->timeout ? (timeout => $self->timeout) : ()),
196 );
197 }
198  
199 141 ▶sub api_base {
Subroutine api_base: called CC=5 SCAR=16.1
Pod undocumented
200 141 my ($self) = @_;
201  
202 141 ▶ if ($self->region) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->region) 18 123 141
203 18 ▶ if (lc($self->region) eq "eu") {
Branch: 3 branches
BranchTrueFalseTotal
if (lc $self->region eq 'eu') { } 6 12 18
elsif (lc $self->region eq 'au') { } 8 4 12
elsif (lc $self->region ne 'us') { } 1 3 4
204 6 return $API_BASE_EU;
205 } elsif (lc($self->region) eq "au") {
206 8 return $API_BASE_AU;
207 } elsif (lc($self->region) ne "us") {
208 1 die "unsupported region '" . $self->region ."'";
209 }
210 }
211 126 return $API_BASE;
212 }
213  
214 59 ▶sub api_url {
Subroutine api_url: called CC=1 SCAR=0.0
Pod undocumented
215 59 my ($self, $method) = @_;
216  
217 59 sprintf 'https://api:%s@%s/%s',
218 $self->api_key, $self->api_base, $method;
219 }
220  
221 27 ▶sub domain_api_url {
Subroutine domain_api_url: called CC=1 SCAR=0.0
Pod undocumented
222 27 my ($self, $method) = @_;
223  
224 27 sprintf 'https://api:%s@%s/%s/%s',
225 $self->api_key, $self->api_base, $self->domain, $method;
226 }
227  
228 # The URL path from its segments, such as ('lists', $address, 'members'). Each segment (a list
229 # address, a member address, a template name) is percent-encoded on its own (#34). What a
230 # segment can hold as it is (the pchar of RFC 3986: `@`, `+` and the like) is left alone, so the
231 # addresses that worked before go out the same; `?`, `#`, `/`, `%` and the rest are encoded, so
232 # that they cannot end the path or add a segment.
233 #
234 # A decoded string (one with the UTF-8 flag) is sent as UTF-8. Any other string is taken as the
235 # bytes to send, as before, so that an address read without decoding is not encoded twice.
236 #
237 # An undefined or empty segment dies: `list(undef)` would otherwise ask for `lists`, every list.
238 49 ▶sub _path {
Subroutine _path: called CC=4 SCAR=13.9
239 49 my @segments = @_;
240  
241 return join '/', map {
242 49 ▶ croak 'an address or a name is required' unless defined $_ && length $_;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and length $_ 4 118 122
Condition100%3/3
defined $_ and length $_
!ll&&!rl&&r
31118
MC/DC100%2/2
defined $_ and length $_
defined $_ length $_
Truth table100%3/3
A and B
A= defined $_B= length $_
ABresult
0X0
100
111
243 118 my $segment = $_;
244 118 ▶ utf8::encode($segment) if utf8::is_utf8($segment);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($segment) 1 117 118
245 118 $segment =~ s/([^A-Za-z0-9\-._~!\$&'()*+,;=:\@])/sprintf '%%%02X', ord $1/ge;
246 118 $segment;
247 } @segments;
248 }
249  
250 # The form-data content of the key-value pairs given to $method (message, mime).
251 #
252 # `to => ['a@example.com', 'b@example.com']` sends `to` twice, as Mailgun takes a key given more
253 # than once. HTTP::Request::Common would take the array reference for a file, and die opening a
254 # file named after the first address (#36). Under the keys in @file_keys an array reference is
255 # still a file ([$path, $filename, ...]), and an array reference of those is several files.
256 19 ▶sub _form_content {
Subroutine _form_content: called CC=6 SCAR=17.9
257 19 my ($method, $pairs, @file_keys) = @_;
258  
259 19 my %file_key = map { $_ => 1 } @file_keys;
260 19 my @content;
261 19 ▶ for (my $i = 0; $i < @$pairs; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$pairs) 55 18 73
262 55 my ($key, $value) = @$pairs[$i, $i + 1];
263 55 ▶ if (ref $value ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value ne "ARRAY") 37 18 55
264 37 push @content, $key => _form_value($value);
265 37 next;
266 }
267 # An empty list would leave the key out, and Mailgun would answer that it is missing.
268 18 ▶ croak "$method(): '$key' is an empty array reference" unless @$value;
Branch: 1 branch
BranchTrueFalseTotal
unless @$value 1 17 18
269 17 ▶ if (!$file_key{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $file_key{$key}) { } 5 12 17
elsif (ref $value->[0] eq 'ARRAY') { } 1 11 12
270 5 push @content, map { ($key => _form_value($_)) } @$value;
271 } elsif (ref $value->[0] eq 'ARRAY') {
272 1 push @content, map { ($key => $_) } @$value;
273 } else {
274 11 push @content, $key => $value;
275 }
276 }
277 18 return \@content;
278 }
279  
280 # A value of the form as bytes. A decoded string (one with the UTF-8 flag) is sent as UTF-8:
281 # HTTP::Request::Common takes only bytes, and dies on a character above 0xFF.
282 53 ▶sub _form_value {
Subroutine _form_value: called CC=4 SCAR=14.3
283 53 my ($value) = @_;
284  
285 53 ▶ return $value if ref $value || !defined $value || !utf8::is_utf8($value);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value or not defined $value or not utf8::is_utf8($value) 47 6 53
Condition50%3/6
ref $value or not defined $value or not utf8::is_utf8($value)
l!l&&r!l&&!r
0476
ref $value or not defined $value
l!l&&r!l&&!r
0053
MC/DC33%1/3
ref $value or not defined $value or not utf8::is_utf8($value)
ref $value not defined $value not utf8::is_utf8($value)
Truth table50%2/4
A or not(B) or not(C)
A= ref $valueB= not defined $valueC= not utf8::is_utf8($value)
ABCresult
1XX1
01X1
0011
0000
286 6 my $bytes = $value;
287 6 utf8::encode($bytes);
288 6 return $bytes;
289 }
290  
291 10 ▶sub message {
Subroutine message: called CC=3 SCAR=11.1
Pod documented
292 10 my ($self, $args) = @_;
293  
294 10 my @pairs;
295 10 ▶ if (ref($args) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 8 2 10
elsif (ref $args eq 'ARRAY') { } 2 0 2
296 8 @pairs = %$args;
297 }
298 elsif (ref($args) eq 'ARRAY') {
299 2 @pairs = @$args;
300 }
301 else {
302 0 die 'unsupport argument. message() need HashRef or ArrayRef.';
303 }
304  
305 10 my $content = _form_content('message', \@pairs, qw(attachment inline));
306 9 my $req = POST $self->domain_api_url('messages'), Content_type => 'form-data', Content => $content;
307  
308 9 my $res = $self->client->request($req);
309 9 $self->decode_response($res);
310 }
311  
312  
313  
314 12 ▶sub mime {
Subroutine mime: called CC=14 SCAR=26.9
Pod documented
315 12 my ($self, $args) = @_;
316  
317 12 ▶ unless (ref($args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args eq "HASH") 0 12 12
318 0 die 'unsupport argument. mime() needs a hash ref.';
319 }
320  
321 # A copy: the hash reference given is left as it is, so that it can be passed again (#39).
322 12 my %args = %$args;
323  
324 12 ▶ croak 'mime(): give message or file, not both' if exists $args{message} && exists $args{file};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{"message"} and exists $args{"file"} 1 11 12
Condition100%3/3
exists $args{"message"} and exists $args{"file"}
!ll&&!rl&&r
381
MC/DC100%2/2
exists $args{"message"} and exists $args{"file"}
exists $args{"message"} exists $args{"file"}
Truth table100%3/3
A and B
A= exists $args{"message"}B= exists $args{"file"}
ABresult
0X0
100
111
325  
326 11 ▶ if (exists $args{message}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args{'message'}) { } 8 3 11
elsif (exists $args{'file'}) { } 3 0 3
327 # The MIME message goes in the form as the content of a file, from memory: no temporary
328 # file to write and remove.
329 8 my $message = $args{message};
330 8 ▶ my $mime = ref $message eq 'SCALAR' ? $$message
Branch: 3 branches
BranchTrueFalseTotal
ref $message eq 'SCALAR' ? : 2 6 8
!ref($message) ? : 4 2 6
builtin::blessed($message) && overload::Method($message, '""') ? : 1 1 2
Condition66%2/3
builtin::blessed($message) and overload::Method($message, "\"\"")
!ll&&!rl&&r
101
MC/DC50%1/2
builtin::blessed($message) and overload::Method($message, "\"\"")
builtin::blessed($message) overload::Method($message, "\"\"")
Truth table66%2/3
A and B
A= builtin::blessed($message)B= overload::Method($message, "\"\"")
ABresult
0X0
100
111
331 : !ref $message ? $message
332 : blessed($message) && overload::Method($message, '""') ? "$message"
333 : croak 'mime(): message must be a string or a reference to a string';
334 # A MIME message is bytes. A decoded string (one with the UTF-8 flag) is sent as UTF-8.
335 7 ▶ $args{message} = [undef, 'message.mime', Content => _form_value($mime // '')];
Condition50%1/2
$mime // ""
l!l
70
MC/DC0%0/1
$mime // ""
$mime
Truth table50%1/2
A
A= $mime
Aresult
11
00
336 }
337 elsif (exists $args{file}) {
338 3 my $file = delete $args{file};
339 3 ▶ croak "cannot find file, " . ($file // 'undef') unless defined $file && -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and -f $file 1 2 3
Condition60%3/5
defined $file and -f $file
!ll&&!rl&&r
012
$file // "undef"
l!l
10
MC/DC50%1/2
defined $file and -f $file
defined $file -f $file
MC/DC0%0/1
$file // "undef"
$file
Truth table66%2/3
A and B
A= defined $fileB= -f $file
ABresult
0X0
100
111
Truth table50%1/2
A
A= $file
Aresult
11
00
340 2 $args{message} = [$file];
341 }
342  
343 9 my $content = _form_content('mime', [%args], 'message');
344 9 my $req = POST $self->domain_api_url('messages.mime'),
345 Content_Type => 'form-data',
346 Content => $content;
347  
348 9 my $res = $self->client->request($req);
349 9 $self->decode_response($res);
350 }
351  
352  
353  
354  
355 # The query of lists() / list_members(): a hash reference, or nothing (#41).
356 23 ▶sub _list_query {
Subroutine _list_query: called CC=4 SCAR=13.9
357 23 my ($method, $query) = @_;
358  
359 23 ▶ return {} unless defined $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 17 6 23
360 # reftype, not ref: a blessed hash (Hash::MultiValue, say) is a hash too.
361 6 ▶ croak "$method() takes a query hash reference" unless (reftype($query) // '') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless (builtin::reftype($query) // "") eq "HASH" 2 4 6
Condition100%2/2
builtin::reftype($query) // ""
l!l
51
MC/DC100%1/1
builtin::reftype($query) // ""
builtin::reftype($query)
Truth table100%2/2
A
A= builtin::reftype($query)
Aresult
11
00
362 4 return { %$query };
363 }
364  
365 17 ▶sub lists {
Subroutine lists: called CC=1 SCAR=0.0
Pod documented
366 17 my ($self, $query) = @_;
367  
368 17 return $self->recursive('lists/pages', _list_query('lists', $query));
369 }
370  
371 0 ▶sub add_list {
Subroutine add_list: not called CC=1 SCAR=6.9
Pod documented
372 0 my ($self, $args) = @_;
373  
374 0 my $res = $self->client->post($self->api_url("lists"), [], $args);
375 0 $self->decode_response($res);
376 }
377  
378 26 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod documented
379 26 my ($self, $address) = @_;
380  
381 26 my $res = $self->client->get($self->api_url(_path('lists', $address)));
382 23 ▶ my $json = $self->decode_response($res) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $self->decode_response($res) or return 18 5 23
383 18 return $json->{list};
384 }
385  
386 1 ▶sub update_list {
Subroutine update_list: called CC=1 SCAR=0.0
Pod documented
387 1 my ($self, $address, $args) = @_;
388  
389 1 my $res = $self->client->put($self->api_url(_path('lists', $address)), [], $args);
390 1 $self->decode_response($res);
391 }
392  
393 7 ▶sub delete_list {
Subroutine delete_list: called CC=1 SCAR=0.0
Pod documented
394 7 my ($self, $address) = @_;
395  
396 7 my $res = $self->client->delete($self->api_url(_path('lists', $address)));
397 7 $self->decode_response($res);
398 }
399  
400 6 ▶sub list_members {
Subroutine list_members: called CC=1 SCAR=0.0
Pod documented
401 6 my ($self, $address, $query) = @_;
402  
403 6 return $self->recursive(_path('lists', $address, 'members', 'pages'),
404 _list_query('list_members', $query));
405 }
406  
407 1 ▶sub add_list_member {
Subroutine add_list_member: called CC=1 SCAR=0.0
Pod documented
408 1 my ($self, $address, $args) = @_;
409  
410 1 my $res = $self->client->post(
411 $self->api_url(_path('lists', $address, 'members')), [], $args);
412 1 $self->decode_response($res);
413 }
414  
415 1 ▶sub add_list_members {
Subroutine add_list_members: called CC=1 SCAR=0.0
Pod documented
416 1 my ($self, $address, $args) = @_;
417  
418 1 my $res = $self->client->post(
419 $self->api_url(_path('lists', $address, 'members.json')), [], $args);
420 1 $self->decode_response($res);
421 }
422  
423 2 ▶sub list_member {
Subroutine list_member: called CC=2 SCAR=7.0
Pod documented
424 2 my ($self, $address, $member) = @_;
425  
426 2 my $res = $self->client->get($self->api_url(_path('lists', $address, 'members', $member)));
427 2 ▶ my $json = $self->decode_response($res) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $self->decode_response($res) or return 2 0 2
428 2 return $json->{member};
429 }
430  
431 1 ▶sub update_list_member {
Subroutine update_list_member: called CC=1 SCAR=0.0
Pod documented
432 1 my ($self, $address, $member, $args) = @_;
433  
434 1 my $res = $self->client->put(
435 $self->api_url(_path('lists', $address, 'members', $member)), [], $args);
436 1 $self->decode_response($res);
437 }
438  
439 2 ▶sub delete_list_member {
Subroutine delete_list_member: called CC=1 SCAR=0.0
Pod documented
440 2 my ($self, $address, $member) = @_;
441  
442 2 my $res = $self->client->delete(
443 $self->api_url(_path('lists', $address, 'members', $member)));
444 1 $self->decode_response($res);
445 }
446  
447 22 ▶sub event {
Subroutine event: called CC=8 SCAR=20.9
Pod documented
448 22 my ($self, $query) = @_;
449  
450 22 my $api_url;
451 22 ▶ if ((blessed($query) && $query->isa('URI')) || (defined $query && !ref $query)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($query) and $query->isa('URI') or defined $query and not ref $query) { } 14 8 22
Condition77%7/9
builtin::blessed($query) and $query->isa("URI") or defined $query and not ref $query
l!l&&r!l&&!r
1138
builtin::blessed($query) and $query->isa("URI")
!ll&&!rl&&r
2101
defined $query and not ref $query
!ll&&!rl&&r
0813
MC/DC50%2/4
builtin::blessed($query) and $query->isa("URI") or defined $query and not ref $query
builtin::blessed($query) $query->isa("URI") defined $query not ref $query
Truth table42%3/7
A and B or C and not(D)
A= builtin::blessed($query)B= $query->isa("URI")C= defined $queryD= not ref $query
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
452 # A URL (the next URL an earlier call returned, for Event Polling) is fetched as it
453 # is. Mailgun's paging URLs carry no credentials, so the API key is set on it: only for
454 # an https URL on the API's own host, so that the key is never sent anywhere else.
455 14 $api_url = $self->_api_host_url($query);
456 14 ▶ unless ($api_url) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api_url) 6 8 14
457 # Without the userinfo: a URL kept from 1.0.0 still carries the API key.
458 6 ▶ my $shown = blessed($query) ? $query->clone : URI->new($query);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($query) ? : 0 6 6
459 6 ▶ $shown->userinfo(undef) if $shown->can('userinfo');
Branch: 1 branch
BranchTrueFalseTotal
if $shown->can("userinfo") 4 2 6
460 6 my ($host) = split m{/}, $self->api_base;
461 6 croak "event() takes a query hash reference or an https URL on $host, not '$shown'";
462 }
463 8 $api_url->userinfo('api:'.$self->api_key);
464 } else {
465 8 $api_url = URI->new($self->domain_api_url("events"));
466 8 $api_url->query_form($query);
467 }
468 16 return $self->recursive("events", {}, "items", $api_url);
469 }
470  
471 21 ▶sub get_message_from_event {
Subroutine get_message_from_event: called CC=16 SCAR=27.7
Pod documented
472 21 my ($self, $event) = @_;
473  
474 # Any event that carries storage.url (stored, and others such as accepted) has a message to
475 # get; one without it dies, as a non-stored event did (#42).
476 21 ▶ my $storage = (reftype($event) // '') eq 'HASH' ? $event->{storage} : undef;
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($event) // '') eq 'HASH' ? : 20 1 21
Condition100%2/2
builtin::reftype($event) // ""
l!l
201
MC/DC100%1/1
builtin::reftype($event) // ""
builtin::reftype($event)
Truth table100%2/2
A
A= builtin::reftype($event)
Aresult
11
00
477 21 ▶ my $url = (reftype($storage) // '') eq 'HASH' ? $storage->{url} : undef;
Branch: 1 branch
BranchTrueFalseTotal
(builtin::reftype($storage) // '') eq 'HASH' ? : 18 3 21
Condition100%2/2
builtin::reftype($storage) // ""
l!l
183
MC/DC100%1/1
builtin::reftype($storage) // ""
builtin::reftype($storage)
Truth table100%2/2
A
A= builtin::reftype($storage)
Aresult
11
00
478 21 ▶ croak "invalid event! this method needs an event with storage.url (a 'stored' event, say)."
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url and !ref($url) || builtin::blessed($url) && $url->isa("URI") 6 15 21
Condition91%11/12
defined $url and length $url and !ref($url) || builtin::blessed($url) && $url->isa("URI")
!ll&&!rl&&r
5115
defined $url and length $url
!ll&&!rl&&r
4116
not ref $url or builtin::blessed($url) and $url->isa("URI")
l!l&&r!l&&!r
1411
builtin::blessed($url) and $url->isa("URI")
!ll&&!rl&&r
101
MC/DC80%4/5
defined $url and length $url and !ref($url) || builtin::blessed($url) && $url->isa("URI")
defined $url length $url not ref $url builtin::blessed($url) $url->isa("URI")
Truth table83%5/6
A and B and not(C) or D and E
A= defined $urlB= length $urlC= not ref $urlD= builtin::blessed($url)E= $url->isa("URI")
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
479 unless defined $url && length $url && (!ref $url || (blessed($url) && $url->isa('URI')));
480  
481 # The API key is set on storage.url, so it has to be an https URL on Mailgun's own domain:
482 # the storage hosts are per region (storage-us-east4.api.mailgun.net, say), so the API's
483 # host alone would refuse them. The API's host (api_base, which a test may point elsewhere)
484 # is taken too. An event kept and read back could have been changed.
485 15 ▶ my $uri = blessed($url) ? $url->clone : URI->new($url);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($url) ? : 1 14 15
486 15 my $host = _https_host($uri);
487 15 my ($api_host) = split m{/}, $self->api_base;
488 15 ▶ unless ($host ne '' && ($host eq 'mailgun.net' || $host =~ /\.mailgun\.net\z/ || $host eq lc $api_host)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($host ne "" and $host eq "mailgun.net" || $host =~ /\.mailgun\.net\z/u || $host eq lc $api_host) 8 7 15
Condition88%8/9
$host ne "" and $host eq "mailgun.net" || $host =~ /\.mailgun\.net\z/u || $host eq lc $api_host
!ll&&!rl&&r
447
$host eq "mailgun.net" or $host =~ /\.mailgun\.net\z/u or $host eq lc $api_host
l!l&&r!l&&!r
614
$host eq "mailgun.net" or $host =~ /\.mailgun\.net\z/u
l!l&&r!l&&!r
065
MC/DC75%3/4
$host ne "" and $host eq "mailgun.net" || $host =~ /\.mailgun\.net\z/u || $host eq lc $api_host
$host ne "" $host eq "mailgun.net" $host =~ /\.mailgun\.net\z/u $host eq lc $api_host
Truth table80%4/5
A and B or C or D
A= $host ne ""B= $host eq "mailgun.net"C= $host =~ /\.mailgun\.net\z/uD= $host eq lc $api_host
ABCDresult
0XXX0
10000
11XX1
101X1
10011
489 8 my $shown = $uri->clone;
490 8 ▶ $shown->userinfo(undef) if $shown->can('userinfo');
Branch: 1 branch
BranchTrueFalseTotal
if $shown->can("userinfo") 7 1 8
491 8 croak "get_message_from_event() takes storage.url only on https and mailgun.net, not '$shown'";
492 }
493 7 $uri->userinfo('api:'.$self->api_key);
494  
495 7 my $res = $self->client->get($uri->as_string);
496 7 $self->decode_response($res);
497 }
498  
499 0 ▶sub delete_templates {
Subroutine delete_templates: not called CC=1 SCAR=6.9
Pod documented
500 0 my ($self) = @_;
501  
502 0 my $res = $self->client->delete($self->domain_api_url("templates"));
503 0 $self->decode_response($res);
504 }
505  
506 2 ▶sub delete_template {
Subroutine delete_template: called CC=1 SCAR=0.0
Pod documented
507 2 my ($self, $name) = @_;
508  
509 2 my $res = $self->client->delete(
510 $self->domain_api_url(_path('templates', $name)));
511 1 $self->decode_response($res);
512 }
513  
514 0 ▶sub add_template {
Subroutine add_template: not called CC=3 SCAR=24.8
Pod documented
515 0 my ($self, $args) = @_;
516  
517 0 my @content;
518 0 ▶ if (ref($args) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 0 0 0
elsif (ref $args eq 'ARRAY') { } 0 0 0
519 0 @content = %$args;
520 }
521 elsif (ref($args) eq 'ARRAY') {
522 0 @content = @$args;
523 }
524 else {
525 0 die 'unsupport argument. add_template() need HashRef or ArrayRef.';
526 }
527  
528 0 my $req = POST $self->domain_api_url('templates'), Content_type => 'form-data', Content => \@content;
529  
530 0 my $res = $self->client->request($req);
531 0 $self->decode_response($res);
532 }
533  
534 15 1;
535 __END__
↑ Summary