Mailboxlayer.pm

filter: Statementstmt 43.1% 38 / 88 Branchbran 20.0% 6 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 52.3% 11 / 21 Podpod 100% 12 / 12 totaltotal 38.5% 67 / 174 CC 23 SCAR 23.3
CC
23
Cov
33%
user_agent
30.0
json_decoder
20.0
check
20.0
CRAP 10.3
↑ Summary Mailboxlayer/Response.pm »
1 package Net::Mailboxlayer;
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 $Net::Mailboxlayer::VERSION = '0.003';
7  
8 2 ▶use URI::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use JSON::MaybeXS qw(JSON);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 ▶use Net::Mailboxlayer::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Net::Mailboxlayer::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 sub new
17 3 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
18 3 my ($class, %props) = @_;
19  
20 # set defaults
21 3 my $self = bless {
22 _endpoint => 'https://apilayer.net/api/check',
23 _smtp => 1,
24 _format => 0,
25 _catch_all => 0,
26 _user_agent_opts => {},
27 _user_agent => undef,
28 _optional_querystring_parts => [qw(smtp format callback catch_all)],
29 _json => JSON->new,
30 }, $class;
31  
32 3 foreach my $prop (qw (endpoint access_key email_address smtp format callback catch_all user_agent_opts user_agent json_decoder))
33 {
34 30 ▶ next if not exists $props{$prop};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $props{$prop} 28 2 30
35 2 $self->$prop($props{$prop});
36 }
37 3 return $self;
38 }
39  
40 sub endpoint
41 0 ▶{
Subroutine endpoint: not called CC=2 SCAR=17.9
Pod documented
42 0 my ($self, $val) = @_;
43 0 ▶ $self->{_endpoint} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
44 0 return $self->{_endpoint};
45 }
46  
47 sub access_key
48 3 ▶{
Subroutine access_key: called CC=2 SCAR=6.9
Pod documented
49 3 my ($self, $val) = @_;
50 3 ▶ $self->{_access_key} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 2 1 3
51 3 return $self->{_access_key};
52 }
53  
54 sub email_address
55 0 ▶{
Subroutine email_address: not called CC=2 SCAR=17.9
Pod documented
56 0 my ($self, $val) = @_;
57 0 ▶ $self->{_email_address} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
58 0 return $self->{_email_address};
59 }
60  
61 sub smtp
62 2 ▶{
Subroutine smtp: called CC=2 SCAR=6.9
Pod documented
63 2 my ($self, $val) = @_;
64 2 ▶ $self->{_smtp} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 1 1 2
65 2 return $self->{_smtp};
66 }
67  
68 sub format
69 0 ▶{
Subroutine format: not called CC=2 SCAR=17.9
Pod documented
70 0 my ($self, $val) = @_;
71 0 ▶ $self->{_format} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
72 0 return $self->{_format};
73 }
74 sub callback
75 0 ▶{
Subroutine callback: not called CC=2 SCAR=17.9
Pod documented
76 0 my ($self, $val) = @_;
77 0 ▶ $self->{_callback} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
78 0 return $self->{_callback};
79 }
80  
81 sub catch_all
82 0 ▶{
Subroutine catch_all: not called CC=2 SCAR=17.9
Pod documented
83 0 my ($self, $val) = @_;
84 0 ▶ $self->{_catch_all} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
85 0 return $self->{_catch_all};
86 }
87  
88 sub user_agent_opts
89 0 ▶{
Subroutine user_agent_opts: not called CC=2 SCAR=17.9
Pod documented
90 0 my ($self, $val) = @_;
91 0 ▶ $self->{_user_agent_opts} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
92 0 return $self->{_user_agent_opts};
93 }
94  
95 sub user_agent
96 0 ▶{
Subroutine user_agent: not called CC=5 SCAR=34.0
Pod documented
97 0 my ($self, $val) = @_;
98  
99 0 ▶ if ($val and blessed $val and $val->can('get'))
Branch: 1 branch
BranchTrueFalseTotal
if ($val and builtin::blessed($val) and $val->can("get")) 0 0 0
Condition0%0/6
$val and builtin::blessed($val) and $val->can("get")
!ll&&!rl&&r
000
$val and builtin::blessed($val)
!ll&&!rl&&r
000
MC/DC0%0/3
$val and builtin::blessed($val) and $val->can("get")
$val builtin::blessed($val) $val->can("get")
Truth table0%0/4
A and B and C
A= $valB= builtin::blessed($val)C= $val->can("get")
ABCresult
0XX0
10X0
1100
1111
100 {
101 0 $self->{_user_agent} = $val;
102 }
103 0 ▶ if (not $self->{_user_agent})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_user_agent"}) 0 0 0
104 {
105 0 $self->{_user_agent} = LWP::UserAgent->new(%{$self->{_user_agent_opts}});
106 }
107 0 return $self->{_user_agent};
108 }
109  
110 sub json_decoder
111 0 ▶{
Subroutine json_decoder: not called CC=4 SCAR=30.0
Pod documented
112 0 my ($self, $val) = @_;
113  
114 0 ▶ if ($val and blessed $val and $val->can('decode'))
Branch: 1 branch
BranchTrueFalseTotal
if ($val and builtin::blessed($val) and $val->can("decode")) 0 0 0
Condition0%0/6
$val and builtin::blessed($val) and $val->can("decode")
!ll&&!rl&&r
000
$val and builtin::blessed($val)
!ll&&!rl&&r
000
MC/DC0%0/3
$val and builtin::blessed($val) and $val->can("decode")
$val builtin::blessed($val) $val->can("decode")
Truth table0%0/4
A and B and C
A= $valB= builtin::blessed($val)C= $val->can("decode")
ABCresult
0XX0
10X0
1100
1111
115 {
116 0 $self->{_json} = $val;
117 }
118 0 return $self->{_json};
119 }
120  
121 sub _build_url
122 0 ▶{
Subroutine _build_url: not called CC=3 SCAR=24.8
123 0 my ($self) = @_;
124  
125 0 my $url = URI::URL->new($self->endpoint);
126  
127 0 my @parts = (
128 access_key => $self->access_key,
129 email => $self->email_address,
130 );
131 0 foreach my $part (@{$self->{_optional_querystring_parts}})
132 {
133 0 ▶ if ($self->$part)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->$part) 0 0 0
134 {
135 0 push @parts, $part => $self->$part;
136 }
137 }
138  
139 0 $url->query_form(@parts);
140  
141 0 return $url;
142 }
143  
144 sub check
145 0 ▶{
Subroutine check: not called CC=4 SCAR=30.0
Pod documented
146 0 my ($self) = @_;
147  
148 0 my $url = $self->_build_url;
149  
150 0 my $response = $self->user_agent->get($url);
151  
152 0 ▶ if ($response->is_error)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) 0 0 0
153 {
154 0 return Net::Mailboxlayer::Error->new(
155 success => 0,
156 error => {
157 code => $response->code,
158 type => $response->status_line,
159 info => $response->message,
160 },
161 );
162 }
163  
164 # todo: try catch here in case we have invalid json
165 0 my $data = $self->json_decoder->decode($response->decoded_content);
166 0 $data->{_response} = $response;
167  
168 0 ▶ if (exists $data->{success} and not $data->{success})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $data->{"success"} and not $data->{"success"}) 0 0 0
Condition0%0/3
exists $data->{"success"} and not $data->{"success"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $data->{"success"} and not $data->{"success"}
exists $data->{"success"} not $data->{"success"}
Truth table0%0/3
A and not(B)
A= exists $data->{"success"}B= not $data->{"success"}
ABresult
0X0
100
111
169 {
170 0 return Net::Mailboxlayer::Error->new(%{$data});
171 }
172  
173 0 return Net::Mailboxlayer::Response->new(%{$data});
174 }
175  
176 2 1;
177  
178 __END__
↑ Summary Mailboxlayer/Response.pm »