Vehicle.pm

filter: Statementstmt 62.2% 51 / 82 Branchbran 59.0% 13 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 81.2% 13 / 16 Podpod 33.3% 1 / 3 totaltotal 63.4% 78 / 123 CC 12 SCAR 20.9
CC
12
Cov
62%
get
30.0
BUILD
7.0
__ANON__
2.0
CRAP 8.1
« Vehicle/Status.pm ↑ Summary
1 package UK::Vehicle;
2  
3 3 ▶use 5.030000;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use UK::Vehicle::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use subs 'timeout';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 use Class::Tiny qw(ves_api_key _ua timeout _url),
10 {
11 _uat_url => "https://uat.driver-vehicle-licensing.api.gov.uk/vehicle-enquiry",
12 _prod_url => "https://driver-vehicle-licensing.api.gov.uk/vehicle-enquiry",
13 _use_uat => 0
14 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 our $VERSION = '0.3';
21  
22 sub BUILD
23 {
24 8 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
25
26 8 $self->_ua(LWP::UserAgent->new);
27 8 $self->_ua->timeout(10);
28 8 $self->_ua->env_proxy;
29
30 8 ▶ croak "parameter 'ves_api_key' must be supplied to new" unless $self->ves_api_key;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ves_api_key 2 6 8
31 6 ▶ croak "parameter 'ves_api_key' should be 40 characters long" unless length($self->ves_api_key) == 40;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->ves_api_key == 40 2 4 6
32 4 ▶ croak "parameter 'ves_api_key' has invalid characters" unless $self->ves_api_key =~ /([a-z]|[A-Z]|[0-9]){40}/; # TODO make more complicated
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ves_api_key =~ /([a-z]|[A-Z]|[0-9]){40}/u 1 3 4
33 3 ▶ if(defined($args->{'timeout'}))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"timeout"}) 2 1 3
34 {
35 2 ▶ croak "Timeout value must be a number in seconds" unless looks_like_number($args->{'timeout'});
Branch: 1 branch
BranchTrueFalseTotal
unless &looks_like_number($args->{"timeout"}) 1 1 2
36 1 $self->timeout($args->{'timeout'});
37 }
38 2 ▶ if($args->{'_use_uat'})
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'_use_uat'}) { } 0 2 2
39 {
40 0 $self->_url($self->_uat_url);
41 }
42 else
43 {
44 2 $self->_url($self->_prod_url);
45 }
46 }
47  
48 sub get
49 {
50 0 ▶ my $self = shift;
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
51 0 my $vrm = shift;
52
53 # Input sanitisation
54 0 $vrm = $self->removeWhitespace($vrm);
55 0 $vrm = uc($vrm);
56 0 ▶ croak "VRM contains an unexpected character" if $vrm !~ /^([A-Z]|[0-9]|\s){1,}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $vrm =~ /^([A-Z]|[0-9]|\s){1,}$/u 0 0 0
57 0 ▶ croak "VRM too long" if length($vrm) > 7;
Branch: 1 branch
BranchTrueFalseTotal
if length $vrm > 7 0 0 0
58  
59 0 my $msg_json = "{\"registrationNumber\": \"$vrm\"}";
60 0 my $req = HTTP::Request->new('POST', $self->_url."/v1/vehicles");
61 0 $req->header('Content-Type' => 'application/json');
62 0 $req->header('Accept' => 'application/json');
63 0 $req->header('x-api-key' => $self->ves_api_key);
64 0 $req->content($msg_json);
65 0 my $response = $self->_ua->request($req);
66 0 my $content = $response->decoded_content();
67 0 my $json;
68 try
69 {
70 0 ▶ $json = decode_json($content);
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 };
72 0 my $message = $response->code." ".$response->message;
73 0 ▶ if($response->is_error)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->is_error) { } 0 0 0
74 {
75 0 $json->{'result'} = 0;
76 0 ▶ if($response->code != 429)
Branch: 1 branch
BranchTrueFalseTotal
if ($response->code != 429) { } 0 0 0
77 { # For most errors the HTTP response contains everything we need to know
78 0 $json->{'message'} = $message;
79 }
80 else
81 { # But for 429 the message is in the body
82 0 $json->{'message'} = $response->code." ".$json->{'message'};
83 }
84 }
85 else
86 {
87 0 $json->{'result'} = 1;
88 0 $json->{'message'} = "success";
89 }
90
91 0 return UK::Vehicle::Status->new($json);
92 }
93  
94 sub timeout($)
95 {
96 2 ▶ my $self = shift;
Subroutine timeout: called CC=2 SCAR=6.9
97 2 my $arg = shift;
98 2 ▶ if ($arg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg) 1 1 2
99 1 $self->_ua->timeout($arg);
100 }
101 2 return $self->_ua->timeout;
102 }
103  
104 sub removeWhitespace($)
105 {
106 0 ▶ my $self = shift;
Subroutine removeWhitespace: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my $s = shift;
108
109 0 $s =~ s/\s+//g;
110  
111 0 return $s;
112 }
113  
114 3 1;
115 __END__
116  
117 =head1 NAME
118  
119 UK::Vehicle - Perl module to query the UK's Vehicle Enquiry Service API
120  
121 =head1 SYNOPSIS
122  
123 use UK::Vehicle;
124 my $tool = new UK::Vehicle:(ves_api_key => '<your-api-key>');
125 my $status = $tool->get('<vehicle-vrm>');
126 $status->result; # 1 for success, 0 for failure
127 $status->message; # 'success' or an error message
128 $status->is_mot_valid();
129 $status->is_vehicle_taxed();
130 etc..
131  
132 =head1 DESCRIPTION
133  
134 This module helps you query the Vehicle Enquiry Service API provided by
135 the UK's DVLA. In order to use it you must have an API key, which you
136 can apply for L<here|https://register-for-ves.driver-vehicle-licensing.api.gov.uk/>
137  
138 You will likely need a decent reason to have an API key. It takes days
139 to get one so you may want to apply now.
140  
141 =head2 EXPORTS
142  
143 None.
144  
145 =head1 CONSTRUCTORS
146  
147 =over 3
148  
149 =item new(ves_api_key => value)
150  
151 =item new(ves_api_key => value, timeout => integer)
152  
153 Create a new instance of this class, passing in the API key you wish to
154 use. This argument is mandatory. Failure to set it upon creation will
155 result in the method croaking.
156  
157 Optionally also set the connect timeout in seconds. Default value is 10.
158  
159 =back
160  
161 =head1 METHODS
162  
163 =over 3
164  
165 =item get(string)
166  
167 my $status = $tool->get("ZZ99ABC");
168 $status->result; # 1 if success, 0 if not
169 $status->message; # "success" if result was 1, error message if not
170 $status->make; # "ROVER" etc.
171  
172 Query the API for the publicly-available information about a vehicle.
173 Returns a L<UK::Vehicle::Status>, which has accessor methods for each of
174 the properties returned by the VES API. For more information, see
175 L<UK::Vehicle::Status>.
176
177 Any spaces in the VRM you provide will be automatically removed. Lower
178 case characters will be changed to upper case. If the
179 VRM you provide contains weird characters, you will get 0 back and an
180 appropriate message. Permitted characters are 0-9, a-z, A-Z.
181  
182 =back
183  
184 =head1 BUGS AND REQUESTS
185  
186 Please report to L<the GitHub repository|https://github.com/realflash/perl-uk-vehicle>.
187  
188 =head1 AUTHOR
189  
190 Ian Gibbs, E<lt>igibbs@cpan.orgE<gt>
191  
192 =head1 COPYRIGHT AND LICENSE
193  
194 Copyright (C) 2022 by Ian Gibbs
195  
196 This library is free software; you can redistribute it and/or modify
197 it under the terms of the GNU GPL version 3.
198  
199 =cut
« Vehicle/Status.pm ↑ Summary