WebshopAPI.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 72 CC 3 SCAR 12.0
CC
3
Cov
0%
new
6.0
lookup
6.0
ua
2.0
CRAP 3.3
1 package Net::PostcodeNL::WebshopAPI;
2  
3 use strict;
4 use warnings;
5  
6 use LWP::UserAgent;
7 use JSON::XS;
8 use URI::Template;
9  
10 use Net::PostcodeNL::WebshopAPI::Response;
11  
12 our $VERSION = '0.2';
13  
14 my $AGENT = __PACKAGE__ . '/' . $VERSION;
15  
16 sub new {
17 my $class = shift;
18 my %args = @_;
19  
20 my $self = {};
21  
22 $self->{user_agent} = $args{user_agent} ? $args{user_agent} : $AGENT;
23  
24 $self->{api_key} = $args{api_key};
25 $self->{api_secret} = $args{api_secret};
26  
27 $self->{api_url} = URI::Template->new($args{api_url} || 'https://api.postcode.nl/rest/addresses{/zipcode,number,addition}');
28  
29 my $ua = LWP::UserAgent->new;
30 $ua->agent($self->{user_agent});
31 $self->{ua} = $ua;
32  
33 return bless $self, $class;
34 }
35  
36 sub ua {
37 my $self = shift;
38 return $self->{ua};
39 }
40  
41 sub lookup {
42 my $self = shift;
43  
44 my ($zipcode, $number, $addition) = @_;
45  
46 my $uri = $self->api_url($zipcode, $number, $addition);
47  
48 my $ua = $self->ua;
49 $ua->credentials($uri->host_port, 'REST Endpoint', $self->{api_key}, $self->{api_secret});
50 my $resp = $ua->get($uri);
51  
52 if ($resp->code == 200 && $resp->header('Content-Type') eq 'application/json') {
53 return Net::PostcodeNL::WebshopAPI::Response->new(decode_json($resp->decoded_content));
54 }
55 return Net::PostcodeNL::WebshopAPI::Response->new(decode_json($resp->decoded_content));
56 }
57  
58 sub api_url {
59 my $self = shift;
60 my ($zipcode, $number, $addition) = @_;
61 return $self->{api_url}->process({zipcode => $zipcode,number=> $number, addition => $addition });
62 }
63  
64 1;
65  
66 =head1 NAME
67  
68 Net::PostcodeNL::WebshopAPI - Postcode.nl Webshop API
69  
70 =head1 SYNOPSYS
71  
72 use Net::PostcodeNL::WebshopAPI;
73  
74 my $api = Net::PostcodeNL::WebshopAPI->new(
75 api_key => 'insert api key',
76 api_secret => 'insert api secret',
77 );
78  
79 my $zipcode = '';
80 my $number = '';
81 my $addition = '';
82  
83 my $r = $api->lookup($zipcode, $number, $addition);
84  
85 if ($r->is_error) {
86 die $r->err_str;
87 }
88 else {
89 say $r->street;
90 say $r->houseNumber;
91 say $r->houseNumberAddition;
92 }
93  
94 =head1 DESCRIPTION
95  
96 Retrieves information about a zipcode from Postcode.nl
97  
98 You need to apply for a key and secret from L<http://api.postcode.nl>.
99  
100 =head1 METHODS
101  
102 =head2 $r = $self->lookup($zipcode, $number, $addition);
103  
104 Returns a L<Net::PostcodeNL::WebshopAPI::Response> for the combination of
105 C<$zipcode>, C<$number> and C<$addition>.
106  
107 =head1 SEE ALSO
108  
109 L<Net::PostcodeNL::WebshopAPI::Response>
110  
111 =head1 AUTHOR
112  
113 Peter Stuifzand <peter@stuifzand.eu>
114  
115 =head1 LICENSE
116  
117 GPL version 3 or later.
118  
119 =cut