SmartyStreets.pm

filter: Statementstmt 96.8% 93 / 96 Branchbran 62.5% 10 / 16 Conditioncond 42.3% 11 / 26 MC/DCmcdc 6.2% 1 / 16 Subroutinesub 95.8% 23 / 24 Podpod 60.0% 6 / 10 totaltotal 76.6% 144 / 188 SCAR 31.5
CC
21
Cov
83%
get_decoded_data
10.8
us_endpoint
6.0
verify
5.0
CRAP 23.3
↑ Summary SmartyStreets/Address.pm »
1 package WebService::Async::SmartyStreets;
2 # ABSTRACT: Access SmartyStreet API
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.001';
8  
9 =head1 NAME
10  
11 WebService::Async::SmartyStreets - calls the SmartyStreets API and checks for the validity of the address
12  
13 =head1 SYNOPSIS
14  
15 my $ss = WebService::Async::SmartyStreets->new(
16 # Obtain these from your SmartyStreets account page.
17 # These will be used for US lookups
18 us_auth_id => '...',
19 us_token => '...',
20 # For non-US address lookups, you would also need an international token
21 international_auth_id => '...',
22 international_token => '...',
23 );
24 IO::Async::Loop->new->add($ss);
25  
26 print $ss->verify(
27 city => 'Atlanta',
28 country => 'US',
29 geocode => 1
30 )->get->status;
31  
32 =head1 DESCRIPTION
33  
34 This module provides basic support for the L<SmartyStreets API|https://smartystreets.com/>.
35  
36 Note that this module uses L<Future::AsyncAwait>.
37  
38 =cut
39  
40 2 ▶use parent qw(IO::Async::Notifier);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 2 ▶use mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 2 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use Net::Async::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 2 ▶use WebService::Async::SmartyStreets::Address;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 =head2 verify
59  
60 Makes connection to SmartyStreets API and parses the response into WebService::Async::SmartyStreets::Address.
61  
62 my $addr = $ss->verify(%address_to_check)->get;
63  
64 Takes the following named parameters:
65  
66 =over 4
67  
68 =item * C<country> - country (required)
69  
70 =item * C<address1> - address line 1
71  
72 =item * C<address2> - address line 2
73  
74 =item * C<organization> - name of organization (usually building names)
75  
76 =item * C<locality> - city
77  
78 =item * C<administrative_area> - state
79  
80 =item * C<postal_code> - post code
81  
82 =item * C<geocode> - true or false
83  
84 =back
85  
86 Returns a L<Future> which should resolve to a valid L<WebService::Async::SmartyStreets::Address> instance.
87  
88 =cut
89  
90 # keyword 'async' will cause critic test fail. disable it
91 ## no critic (RequireEndWithOne.pm)
92 4 ▶async sub verify {
Subroutine verify: called CC=5 SCAR=16.2
Pod documented
93 4 my ($self, %args) = @_;
94  
95 4 my $uri = $self->country_endpoint($args{country})->clone;
96  
97 4 $uri->query_param($_ => $args{$_}) for keys %args;
98 $uri->query_param(
99 4 ▶ 'auth-id' => ($self->auth_id($args{country}) // die 'need an auth ID'),
Condition50%1/2
$self->auth_id($args{"country"}) // die("need an auth ID")
l!l
40
MC/DC0%0/1
$self->auth_id($args{"country"}) // die("need an auth ID")
$self->auth_id($args{"country"})
Truth table50%1/2
A
A= $self->auth_id($args{"country"})
Aresult
11
00
100 );
101 $uri->query_param(
102 4 ▶ 'auth-token' => ($self->token($args{country}) // die 'need an auth token'),
Condition50%1/2
$self->token($args{"country"}) // die("need an auth token")
l!l
40
MC/DC0%0/1
$self->token($args{"country"}) // die("need an auth token")
$self->token($args{"country"})
Truth table50%1/2
A
A= $self->token($args{"country"})
Aresult
11
00
103 );
104 4 $uri->query_param(
105 'input-id' => $self->next_id,
106 );
107 4 $log->tracef('GET %s', '' . $uri);
108  
109 4 my $decoded = await get_decoded_data($self, $uri);
110  
111 3 $log->tracef('=> %s', $decoded);
112 3 ▶ $decoded = [$decoded] unless ref($decoded) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $decoded eq "ARRAY" 1 2 3
113  
114 3 return map { WebService::Async::SmartyStreets::Address->new(%$_) } @$decoded;
115 2 }
116  
117 =head2 METHODS - Accessors
118  
119 =cut
120  
121 sub country_endpoint {
122 4 ▶ my ($self, $country) = @_;
Subroutine country_endpoint: called CC=2 SCAR=7.1
Pod undocumented
123 4 ▶ return $self->us_endpoint if uc($country) eq 'US';
Branch: 1 branch
BranchTrueFalseTotal
if uc $country eq "US" 0 4 4
124 4 return $self->international_endpoint;
125 }
126  
127 sub us_endpoint {
128 0 ▶ shift->{us_endpoint} //= URI->new('https://us-street.api.smartystreets.com/street-address');
Subroutine us_endpoint: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
+(shift())->{"us_endpoint"} //= "URI"->new("https://us-street.api.smartystreets.com/street-address")
l!l&&r!l&&!r
000
MC/DC0%0/2
+(shift())->{"us_endpoint"} //= "URI"->new("https://us-street.api.smartystreets.com/street-address")
+(shift())->{"us_endpoint"} "URI"->new("https://us-street.api.smartystreets.com/street-address")
Truth table0%0/3
A //= B
A= +(shift())->{"us_endpoint"}B= "URI"->new("https://us-street.api.smartystreets.com/street-address")
ABresult
1X1
011
000
129 }
130  
131 sub international_endpoint {
132 4 ▶ shift->{international_endpoint} //= URI->new('https://international-street.api.smartystreets.com/verify');
Subroutine international_endpoint: called CC=2 SCAR=7.2
Pod undocumented
Condition66%2/3
+(shift())->{"international_endpoint"} //= "URI"->new("https://international-street.api.smartystreets.com/verify")
l!l&&r!l&&!r
220
MC/DC0%0/2
+(shift())->{"international_endpoint"} //= "URI"->new("https://international-street.api.smartystreets.com/verify")
+(shift())->{"international_endpoint"} "URI"->new("https://international-street.api.smartystreets.com/verify")
Truth table66%2/3
A //= B
A= +(shift())->{"international_endpoint"}B= "URI"->new("https://international-street.api.smartystreets.com/verify")
ABresult
1X1
011
000
133 }
134  
135 sub auth_id {
136 4 ▶ my ($self, $country) = @_;
Subroutine auth_id: called CC=2 SCAR=7.1
Pod documented
137 4 ▶ return $self->{us_auth_id} if uc($country) eq 'US';
Branch: 1 branch
BranchTrueFalseTotal
if uc $country eq "US" 0 4 4
138 4 return $self->{international_auth_id};
139 }
140  
141 sub token {
142 4 ▶ my ($self, $country) = @_;
Subroutine token: called CC=2 SCAR=7.1
Pod documented
143 4 ▶ return $self->{us_token} if uc($country) eq 'US';
Branch: 1 branch
BranchTrueFalseTotal
if uc $country eq "US" 0 4 4
144 4 return $self->{international_token};
145 }
146  
147 =head1 METHODS - Internal
148  
149 =head2 get_decoded_data
150  
151 Calls the SmartyStreets API then decode and parses the response give by SmartyStreets
152  
153 my $decoded = await get_decoded_data($self, $uri)
154  
155 Takes the following parameters:
156  
157 =over 4
158  
159 =item * C<$uri> - URI for endpoint
160  
161 =back
162  
163 More information on the response can be seen in L<SmartyStreets Documentation | https://smartystreets.com/docs/cloud/international-street-api>.
164  
165 Returns a L<Future> which resolves to an arrayref of L<WebService::Async::SmartyStreets::Address> instances.
166  
167 =cut
168  
169 1 ▶async sub get_decoded_data {
Subroutine get_decoded_data: called CC=8 SCAR=23.8
Pod documented
170 1 my $self = shift;
171 1 my $uri = shift;
172  
173 1 my $res;
174 try {
175 1 $res = await $self->ua->GET($uri);
176 1 } catch ($e) {
177 1 ▶ if (blessed($e) and $e->isa('Future::Exception')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($e) and $e->isa("Future::Exception")) 1 0 1
Condition33%1/3
builtin::blessed($e) and $e->isa("Future::Exception")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($e) and $e->isa("Future::Exception")
builtin::blessed($e) $e->isa("Future::Exception")
Truth table33%1/3
A and B
A= builtin::blessed($e)B= $e->isa("Future::Exception")
ABresult
0X0
100
111
178 1 my ($payload) = $e->details;
179  
180 1 ▶ if (blessed($payload) && $payload->can('content')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($payload) and $payload->can("content")) 1 0 1
Condition33%1/3
builtin::blessed($payload) and $payload->can("content")
!ll&&!rl&&r
001
MC/DC0%0/2
builtin::blessed($payload) and $payload->can("content")
builtin::blessed($payload) $payload->can("content")
Truth table33%1/3
A and B
A= builtin::blessed($payload)B= $payload->can("content")
ABresult
0X0
100
111
181 1 my $resp = decode_json_utf8($payload->content);
182 1 ▶ my $errors = $resp->{errors} // [];
Condition50%1/2
$$resp{"errors"} // []
l!l
10
MC/DC0%0/1
$$resp{"errors"} // []
$$resp{"errors"}
Truth table50%1/2
A
A= $$resp{"errors"}
Aresult
11
00
183 1 my ($error) = $errors->@*;
184  
185 1 ▶ if ($error && $error->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error and $$error{"message"}) 1 0 1
Condition33%1/3
$error and $$error{"message"}
!ll&&!rl&&r
001
MC/DC0%0/2
$error and $$error{"message"}
$error $$error{"message"}
Truth table33%1/3
A and B
A= $errorB= $$error{"message"}
ABresult
0X0
100
111
186 1 $log->warnf(sprintf("SmartyStreets HTTP status %d error: %s", $payload->code, $error->{message}));
187 }
188 }
189 }
190  
191 1 die 'Unable to retrieve response.';
192 };
193  
194 0 my $response = decode_json_utf8($res->decoded_content);
195  
196 0 return $response->[0];
197 2 }
198  
199 =head2 configure
200  
201 Configures the instance.
202  
203 Takes the following named parameters:
204  
205 =over 4
206  
207 =item * C<international_auth_id> - auth_id obtained from SmartyStreet
208  
209 =item * C<international_token> - token obtained from SmartyStreet
210  
211 =item * C<us_auth_id> - auth_id obtained from SmartyStreet
212  
213 =item * C<us_token> - token obtained from SmartyStreet
214  
215 =back
216  
217 Note that you can provide US, international or both API tokens - if an API token
218 is not available for a L</verify> call, then it will return a failed L<Future>.
219  
220 =cut
221  
222 sub configure {
223 2 ▶ my ($self, %args) = @_;
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
224 2 for my $k (qw(international_auth_id international_token us_auth_id us_token)) {
225 8 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 4 4 8
226 }
227 2 $self->next::method(%args);
228 }
229  
230 sub next_id {
231 4 ▶ ++(shift->{id} //= 'AA00000000');
Subroutine next_id: called CC=2 SCAR=6.9
Pod undocumented
Condition100%2/2
+(shift())->{"id"} //= "AA00000000"
l!l
22
MC/DC100%1/1
+(shift())->{"id"} //= "AA00000000"
+(shift())->{"id"}
Truth table100%2/2
A
A= +(shift())->{"id"}
Aresult
11
00
232 }
233  
234 =head2 ua
235  
236 Accessor for the L<Net::Async::HTTP> instance which will be used for SmartyStreets API requests.
237  
238 =cut
239  
240 sub ua {
241 1 ▶ my ($self) = @_;
Subroutine ua: called CC=2 SCAR=7.4
Pod documented
242 1 ▶ $self->{ua} //= do {
Condition33%1/3
$self->{"ua"} //= do {
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->{"ua"} //= do { $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "decode_content", 1, "pipeline", 0, "max_connections_per_host", 4, "user_agent", "Mozilla/4.0 (WebService::Async::SmartyStreets; BINARY\@cpan.org; https://metacpan.org/pod/WebService::Async::SmartyStreets)")); $ua }
$self->{"ua"} do { $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "decode_content", 1, "pipeline", 0, "max_connections_per_host", 4, "user_agent", "Mozilla/4.0 (WebService::Async::SmartyStreets; BINARY\@cpan.org; https://metacpan.org/pod/WebService::Async::SmartyStreets)")); $ua }
Truth table33%1/3
A //= B
A= $self->{"ua"}B= do { $self->add_child(my $ua = "Net::Async::HTTP"->new("fail_on_error", 1, "decode_content", 1, "pipeline", 0, "max_connections_per_host", 4, "user_agent", "Mozilla/4.0 (WebService::Async::SmartyStreets; BINARY\@cpan.org; https://metacpan.org/pod/WebService::Async::SmartyStreets)")); $ua }
ABresult
1X1
011
000
243 1 $self->add_child(
244 my $ua = Net::Async::HTTP->new(
245 fail_on_error => 1,
246 decode_content => 1,
247 pipeline => 0,
248 max_connections_per_host => 4,
249 user_agent =>
250 'Mozilla/4.0 (WebService::Async::SmartyStreets; BINARY@cpan.org; https://metacpan.org/pod/WebService::Async::SmartyStreets)',
251 ));
252 1 $ua;
253 }
254 }
255  
256 2 1;
257  
↑ Summary SmartyStreets/Address.pm »