blib/lib/WebService/Mailgun.pm

filter: Statementstmt 52.7% 97 / 184 Branchbran 35.4% 17 / 48 Conditioncond 70.8% 17 / 24 MC/DCmcdc 33.3% 4 / 12 Subroutinesub 55.2% 21 / 38 Podpod 79.1% 19 / 24 totaltotal 53.0% 175 / 330 CC 34 SCAR 20.9
CC
34
Cov
51%
mime
42.0
add_template
12.0
event
9.0
CRAP 8.0
↑ Summary
1 package WebService::Mailgun;
2  
3  
4 6 ▶use 5.020;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use Furl;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use HTTP::Request::Common;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 6 our $VERSION = "1.0.0";
19 6 our $API_BASE = 'api.mailgun.net/v3';
20 6 our $API_BASE_EU = 'api.eu.mailgun.net/v3';
21  
22 6 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 new => 1,
24 rw => [qw(api_key domain RaiseError region)],
25 ro => [qw(error error_status)],
26 6 );
27  
28 7 ▶sub decode_response {
Subroutine decode_response: called CC=3 SCAR=11.1
Pod undocumented
29 7 my ($self, $res) = @_;
30  
31 7 ▶ if ($res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->is_success) { } 5 2 7
32 5 return decode_json $res->content;
33 } else {
34 2 my $json;
35 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 2 $json = decode_json $res->content;
37 1 ▶ } catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
38 1 $json = { message => $res->content };
39 2 };
40 2 $self->{error} = $json->{message};
41 2 $self->{error_status} = $res->status_line;
42 2 ▶ if ($self->RaiseError) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->RaiseError) { } 0 2 2
43 0 carp $self->error;
44 0 croak $self->error_status;
45 } else {
46 2 return;
47 }
48 }
49 }
50  
51 4 ▶sub recursive {
Subroutine recursive: called CC=8 SCAR=21.5
Pod undocumented
52 4 my ($self, $method, $query, $key, $api_uri) = @_;
53  
54 4 ▶ $query //= {};
Condition50%1/2
$query //= {}
l!l
40
MC/DC0%0/1
$query //= {}
$query
Truth table50%1/2
A
A= $query
Aresult
11
00
55 4 ▶ $key //= 'items';
Condition50%1/2
$key //= "items"
l!l
40
MC/DC0%0/1
$key //= "items"
$key
Truth table50%1/2
A
A= $key
Aresult
11
00
56 4 my @result;
57 my $next;
58 4 ▶ unless($api_uri) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($api_uri) 0 4 4
59 0 $api_uri = URI->new($self->api_url($method));
60 0 $api_uri->query_form($query);
61 }
62  
63 4 while (1) {
64 6 my $res = $self->client->get($api_uri->as_string);
65 6 my $json = $self->decode_response($res);
66 6 ▶ unless ($json) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($json) 1 5 6
67 # The request failed: return the URL of the page that failed, so that the caller can
68 # fetch the rest from there.
69 1 $next = $api_uri;
70 1 last;
71 }
72 5 ▶ unless (scalar @{$json->{$key}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$json->{$key};}) 3 2 5
73 # paging.next of the empty page: where the items that come later will be. For events
74 # this is how Event Polling goes on (#25). paging.previous would point back over the
75 # last page and return it again.
76 3 ▶ if ($json->{paging} && $json->{paging}->{next}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($json->{"paging"} and $json->{"paging"}{"next"}) 3 0 3
Condition33%1/3
$json->{"paging"} and $json->{"paging"}{"next"}
!ll&&!rl&&r
003
MC/DC0%0/2
$json->{"paging"} and $json->{"paging"}{"next"}
$json->{"paging"} $json->{"paging"}{"next"}
Truth table33%1/3
A and B
A= $json->{"paging"}B= $json->{"paging"}{"next"}
ABresult
0X0
100
111
77 3 $next = URI->new($json->{paging}->{next});
78 3 $next->userinfo('api:'.$self->api_key);
79 }
80 3 last;
81 }
82 2 push @result, @{$json->{$key}};
83 2 $api_uri = URI->new($json->{paging}->{next});
84 2 $api_uri->userinfo('api:'.$self->api_key);
85 }
86  
87 4 return \@result, $next;
88 }
89  
90 7 ▶sub client {
Subroutine client: called CC=2 SCAR=7.1
Pod documented
91 7 my $self = shift;
92  
93 7 ▶ $self->{_client} //= Furl->new(
Condition66%2/3
$self->{"_client"} //= "Furl"->new("agent", 'WebService::Mailgun/' . $VERSION)
l!l&&r!l&&!r
610
MC/DC0%0/2
$self->{"_client"} //= "Furl"->new("agent", 'WebService::Mailgun/' . $VERSION)
$self->{"_client"} "Furl"->new("agent", 'WebService::Mailgun/' . $VERSION)
Truth table66%2/3
A //= B
A= $self->{"_client"}B= "Furl"->new("agent", 'WebService::Mailgun/' . $VERSION)
ABresult
1X1
011
000
94 agent => __PACKAGE__ . '/' . $VERSION,
95 );
96 }
97  
98 9 ▶sub api_base {
Subroutine api_base: called CC=4 SCAR=21.7
Pod undocumented
99 9 my ($self) = @_;
100  
101 9 ▶ if ($self->region) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->region) 0 9 9
102 0 ▶ if (lc($self->region) eq "eu") {
Branch: 2 branches
BranchTrueFalseTotal
if (lc $self->region eq 'eu') { } 0 0 0
elsif (lc $self->region ne 'us') { } 0 0 0
103 0 return $API_BASE_EU;
104 } elsif (lc($self->region) ne "us") {
105 0 die "unsupported region '" . $self->region ."'";
106 }
107 }
108 9 return $API_BASE;
109 }
110  
111 0 ▶sub api_url {
Subroutine api_url: not called CC=1 SCAR=6.9
Pod undocumented
112 0 my ($self, $method) = @_;
113  
114 0 sprintf 'https://api:%s@%s/%s',
115 $self->api_key, $self->api_base, $method;
116 }
117  
118 3 ▶sub domain_api_url {
Subroutine domain_api_url: called CC=1 SCAR=0.0
Pod undocumented
119 3 my ($self, $method) = @_;
120  
121 3 sprintf 'https://api:%s@%s/%s/%s',
122 $self->api_key, $self->api_base, $self->domain, $method;
123 }
124  
125 1 ▶sub message {
Subroutine message: called CC=3 SCAR=12.6
Pod documented
126 1 my ($self, $args) = @_;
127  
128 1 my @content;
129 1 ▶ if (ref($args) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 1 0 1
elsif (ref $args eq 'ARRAY') { } 0 0 0
130 1 @content = %$args;
131 }
132 elsif (ref($args) eq 'ARRAY') {
133 0 @content = @$args;
134 }
135 else {
136 0 die 'unsupport argument. message() need HashRef or ArrayRef.';
137 }
138  
139 1 my $req = POST $self->domain_api_url('messages'), Content_type => 'form-data', Content => \@content;
140  
141 1 my $res = $self->client->request($req);
142 1 $self->decode_response($res);
143 }
144  
145  
146  
147 0 ▶sub mime {
Subroutine mime: not called CC=6 SCAR=37.4
Pod documented
148
149 0 my ($self, $args) = @_;
150  
151 0 ▶ if (ref($args) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 0 0 0
152 # Well, good!
153 }
154 else {
155 0 die 'unsupport argument. mime() needs a hash ref.';
156 }
157
158
159 0 my $tmp = undef;
160  
161 0 ▶ if(exists($args->{message})){
Branch: 2 branches
BranchTrueFalseTotal
if (exists $args->{'message'}) { } 0 0 0
elsif (exists $args->{'file'}) { } 0 0 0
162 0 $tmp = File::Temp->new();
163
164 0 ▶ if(ref $args->{message} eq 'SCALAR'){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'message'} eq 'SCALAR') { } 0 0 0
165 # save from a ref
166 0 print $tmp ${$args->{message}};
167 0 seek $tmp, 0, 0;
168 }
169 else {
170 # Save from a string
171 0 print $tmp $args->{message};
172 0 seek $tmp, 0, 0;
173 }
174 0 $args->{message} = [$tmp->filename];
175 }
176 elsif(exists($args->{file})){
177 0 ▶ if(-f $args->{file}){
Branch: 1 branch
BranchTrueFalseTotal
if (-f $args->{'file'}) { } 0 0 0
178 0 $args->{message} = [$args->{file}];
179 0 delete $args->{file};
180 }
181 else {
182 0 die "cannot find file, " . $args->{file};
183 }
184 }
185
186 # Put it back together:
187 0 my @content = %$args;
188  
189 0 my $req = POST $self->domain_api_url('messages.mime'),
190 Content_Type => 'form-data',
191 Content => \@content;
192
193 0 my $res = $self->client->request($req);
194
195 0 undef $tmp;
196
197 0 $self->decode_response($res);
198  
199 }
200  
201  
202  
203  
204 0 ▶sub lists {
Subroutine lists: not called CC=1 SCAR=6.9
Pod documented
205 0 my $self = shift;
206  
207 0 return $self->recursive('lists/pages');
208 }
209  
210 0 ▶sub add_list {
Subroutine add_list: not called CC=1 SCAR=6.9
Pod documented
211 0 my ($self, $args) = @_;
212  
213 0 my $res = $self->client->post($self->api_url("lists"), [], $args);
214 0 $self->decode_response($res);
215 }
216  
217 0 ▶sub list {
Subroutine list: not called CC=2 SCAR=17.9
Pod documented
218 0 my ($self, $address) = @_;
219  
220 0 my $res = $self->client->get($self->api_url("lists/$address"));
221 0 ▶ my $json = $self->decode_response($res) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $self->decode_response($res) or return 0 0 0
222 0 return $json->{list};
223 }
224  
225 0 ▶sub update_list {
Subroutine update_list: not called CC=1 SCAR=6.9
Pod documented
226 0 my ($self, $address, $args) = @_;
227  
228 0 my $res = $self->client->put($self->api_url("lists/$address"), [], $args);
229 0 $self->decode_response($res);
230 }
231  
232 0 ▶sub delete_list {
Subroutine delete_list: not called CC=1 SCAR=6.9
Pod documented
233 0 my ($self, $address) = @_;
234  
235 0 my $res = $self->client->delete($self->api_url("lists/$address"));
236 0 $self->decode_response($res);
237 }
238  
239 0 ▶sub list_members {
Subroutine list_members: not called CC=1 SCAR=6.9
Pod documented
240 0 my ($self, $address) = @_;
241  
242 0 return $self->recursive("lists/$address/members/pages");
243 }
244  
245 0 ▶sub add_list_member {
Subroutine add_list_member: not called CC=1 SCAR=6.9
Pod documented
246 0 my ($self, $address, $args) = @_;
247  
248 0 my $res = $self->client->post(
249 $self->api_url("lists/$address/members"), [], $args);
250 0 $self->decode_response($res);
251 }
252  
253 0 ▶sub add_list_members {
Subroutine add_list_members: not called CC=1 SCAR=6.9
Pod documented
254 0 my ($self, $address, $args) = @_;
255  
256 0 my $res = $self->client->post(
257 $self->api_url("lists/$address/members.json"), [], $args);
258 0 $self->decode_response($res);
259 }
260  
261 0 ▶sub list_member {
Subroutine list_member: not called CC=2 SCAR=17.9
Pod documented
262 0 my ($self, $address, $member) = @_;
263  
264 0 my $res = $self->client->get($self->api_url("lists/$address/members/$member"));
265 0 ▶ my $json = $self->decode_response($res) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $json = $self->decode_response($res) or return 0 0 0
266 0 return $json->{member};
267 }
268  
269 0 ▶sub update_list_member {
Subroutine update_list_member: not called CC=1 SCAR=6.9
Pod documented
270 0 my ($self, $address, $member, $args) = @_;
271  
272 0 my $res = $self->client->put(
273 $self->api_url("lists/$address/members/$member"), [], $args);
274 0 $self->decode_response($res);
275 }
276  
277 0 ▶sub delete_list_member {
Subroutine delete_list_member: not called CC=1 SCAR=6.9
Pod documented
278 0 my ($self, $address, $member) = @_;
279  
280 0 my $res = $self->client->delete(
281 $self->api_url("lists/$address/members/$member"));
282 0 $self->decode_response($res);
283 }
284  
285 8 ▶sub event {
Subroutine event: called CC=9 SCAR=22.0
Pod documented
286 8 my ($self, $query) = @_;
287  
288 8 my $api_url;
289 8 ▶ 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) { } 6 2 8
Condition77%7/9
builtin::blessed($query) and $query->isa("URI") or defined $query and not ref $query
l!l&&r!l&&!r
152
builtin::blessed($query) and $query->isa("URI")
!ll&&!rl&&r
701
defined $query and not ref $query
!ll&&!rl&&r
025
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
290 # A URL (the next URL an earlier call returned, for Event Polling) is fetched as it
291 # is. Mailgun's paging URLs carry no credentials, so the API key is set on it: only for
292 # an https URL on the API's own host, so that the key is never sent anywhere else.
293 6 ▶ $api_url = blessed($query) ? $query->clone : URI->new($query);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($query) ? : 1 5 6
294 6 my ($host) = split m{/}, $self->api_base;
295 6 ▶ unless (($api_url->scheme // '') eq 'https' && lc($api_url->host) eq lc($host)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (($api_url->scheme // "") eq "https" and lc $api_url->host eq lc $host) 4 2 6
Condition100%5/5
($api_url->scheme // "") eq "https" and lc $api_url->host eq lc $host
!ll&&!rl&&r
312
$api_url->scheme // ""
l!l
42
MC/DC100%2/2
($api_url->scheme // "") eq "https" and lc $api_url->host eq lc $host
$api_url->scheme lc $api_url->host eq lc $host
Truth table100%3/3
A and B
A= $api_url->schemeB= lc $api_url->host eq lc $host
ABresult
0X0
100
111
296 4 croak "event() takes a query hash reference or an https URL on $host, not '$query'";
297 }
298 2 $api_url->userinfo('api:'.$self->api_key);
299 } else {
300 2 $api_url = URI->new($self->domain_api_url("events"));
301 2 $api_url->query_form($query);
302 }
303 4 return $self->recursive("events", {}, "items", $api_url);
304 }
305  
306 0 ▶sub get_message_from_event {
Subroutine get_message_from_event: not called CC=2 SCAR=17.9
Pod documented
307 0 my ($self, $event) = @_;
308  
309 0 ▶ die "invalid event! this method need 'stored' event only." if $event->{event} ne 'stored';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"event"} ne "stored" 0 0 0
310 0 my $uri = URI->new($event->{storage}->{url});
311 0 $uri->userinfo('api:'.$self->api_key);
312  
313 0 my $res = $self->client->get($uri->as_string);
314 0 $self->decode_response($res);
315 }
316  
317 0 ▶sub delete_templates {
Subroutine delete_templates: not called CC=1 SCAR=6.9
Pod documented
318 0 my ($self) = @_;
319  
320 0 my $res = $self->client->delete($self->domain_api_url("templates"));
321 0 $self->decode_response($res);
322 }
323  
324 0 ▶sub delete_template {
Subroutine delete_template: not called CC=1 SCAR=6.9
Pod documented
325 0 my ($self, $name) = @_;
326  
327 0 my $res = $self->client->delete(
328 $self->domain_api_url("templates/$name"));
329 0 $self->decode_response($res);
330 }
331  
332 0 ▶sub add_template {
Subroutine add_template: not called CC=3 SCAR=24.8
Pod documented
333 0 my ($self, $args) = @_;
334  
335 0 my @content;
336 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
337 0 @content = %$args;
338 }
339 elsif (ref($args) eq 'ARRAY') {
340 0 @content = @$args;
341 }
342 else {
343 0 die 'unsupport argument. add_template() need HashRef or ArrayRef.';
344 }
345  
346 0 my $req = POST $self->domain_api_url('templates'), Content_type => 'form-data', Content => \@content;
347  
348 0 my $res = $self->client->request($req);
349 0 $self->decode_response($res);
350 }
351  
352 6 1;
353 __END__
↑ Summary