File Coverage

blib/lib/Webservice/GAMSTOP.pm
Criterion Covered Total %
statement 38 40 95.0
branch 16 22 72.7
condition n/a
subroutine 9 9 100.0
pod 1 1 100.0
total 64 72 88.8


line stmt bran cond sub pod time code
1             package Webservice::GAMSTOP;
2             # ABSTRACT: GAMSTOP API Client Implementation
3              
4 1     1   61255 use strict;
  1         12  
  1         24  
5 1     1   4 use warnings;
  1         2  
  1         20  
6              
7 1     1   471 use Moo;
  1         9718  
  1         6  
8 1     1   1761 use Email::Valid;
  1         109233  
  1         101  
9 1     1   530 use Mojo::UserAgent;
  1         283111  
  1         14  
10              
11 1     1   504 use Webservice::GAMSTOP::Response;
  1         4  
  1         653  
12              
13             our $VERSION = '0.003';
14              
15             =head1 NAME
16              
17             Webservice::GAMSTOP - GAMSTOP API Client Implementation
18              
19             =head1 VERSION
20              
21             version 0.001
22              
23             =head1 SYNOPSIS
24              
25             use Webservice::GAMSTOP;
26             my $instance = Webservice::GAMSTOP->new(
27             api_url => 'gamstop_api_url',
28             api_key => 'gamstop_api_key',
29             # optional (defaults to 5 seconds)
30             timeout => 10,
31             );
32              
33             $instance->get_exclusion_for(
34             first_name => 'Harry',
35             last_name => 'Potter',
36             email => 'harry.potter@example.com',
37             date_of_birth => '1970-01-01',
38             postcode => 'hp11aa',
39             );
40              
41             =head1 DESCRIPTION
42              
43             This module implements a programmatic interface to
44             [GAMSTOP](https://www.gamstop.co.uk/) API.
45              
46             =head1 PRE-REQUISITE
47              
48             Before you can use this module, you'll need to obtain your
49             own "Unique API Key" from [GAMSTOP](https://www.gamstop.co.uk/).
50              
51             =cut
52              
53             =head1 ATTRIBUTES
54              
55             L implements the following attributes
56              
57             =head2 api_url
58              
59             GAMSTOP API endpoint url (REQUIRED)
60              
61             =cut
62              
63             has api_url => (
64             is => 'ro',
65             required => 1,
66             );
67              
68             =head2 api_key
69              
70             GAMSTOP API unique key for operator (REQUIRED)
71              
72             =cut
73              
74             has api_key => (
75             is => 'ro',
76             required => 1,
77             );
78              
79             =head2 timeout
80              
81             Maximum amount of time in seconds establishing a
82             connection may take before getting canceled
83             (OPTIONAL - DEFAULT 5 seconds)
84              
85             =cut
86              
87             has timeout => (
88             is => 'ro',
89             default => 5,
90             );
91              
92             has _ua => (
93             is => 'lazy',
94             );
95              
96             sub _build__ua {
97 1     1   22 return Mojo::UserAgent->new->connect_timeout(shift->timeout);
98             }
99              
100             =head1 METHODS
101              
102             =head2 get_exclusion_for
103              
104             Given user details return L object
105             Note: it dies if an error occur connecting to GAMSTOP API endpoint
106              
107             =head3 Required parameters
108              
109             =over 4
110              
111             =item * first_name : First name of person, only 20 characters are significant
112              
113             =item * last_name : Last name of person, only 20 characters are significant
114              
115             =item * date_of_birth: Date of birth in ISO format (yyyy-mm-dd)
116              
117             =item * email : Email address
118              
119             =item * postcode : Postcode (spaces not significant)
120              
121             =back
122              
123             =head3 Optional parameters
124              
125             =over 4
126              
127             x_trace_id: A freeform field that is put into audit log that can be used
128             by the caller to identify a request. This might be something to indicate
129             the person being checked, a unique request ID, GUID, or a trace ID from
130             a system such as zipkin.
131              
132             =back
133              
134             =head3 Return value
135              
136             =over 4
137              
138             A L object
139              
140             =back
141              
142             =cut
143              
144             sub get_exclusion_for {
145 6     6 1 9262 my ($self, %args) = @_;
146              
147 6 100       21 die "Missing required parameter: first_name." unless (exists $args{first_name});
148 5 100       18 die "Missing required parameter: last_name." unless (exists $args{last_name});
149 4 100       13 die "Missing required parameter: date_of_birth." unless (exists $args{date_of_birth});
150 3 100       12 die "Missing required parameter: email." unless (exists $args{email});
151 2 100       27 die "Missing required parameter: postcode." unless (exists $args{postcode});
152              
153             # validate args
154             die 'Invalid date of birth. Date of birth should be in ISO format (yyyy-mm-dd)'
155 1 50       8 unless $args{'date_of_birth'} =~ /^(?:\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/;
156 1 50       8 die 'Invalid email.' unless Email::Valid->address($args{email});
157 1 50       1644 die 'Invalid postcode.' unless $args{postcode} =~ /^[^+]{0,20}$/;
158              
159             # required parameters
160             my $form_params = {
161             firstName => $args{first_name},
162             lastName => $args{last_name},
163             dateOfBirth => $args{date_of_birth},
164             email => $args{email},
165             postcode => $args{postcode},
166 1         6 };
167              
168             # optional parameters
169 1 50       3 $form_params->{'X-Trace-Id'} = $args{x_trace_id} if $args{x_trace_id};
170              
171 1         22 my $ua = $self->_ua;
172             $ua->on(
173             start => sub {
174 1     1   1405 my ($useragent, $tx) = @_;
175 1         3 $tx->req->headers->header('X-API-Key' => $self->api_key);
176 1         27 });
177              
178 1         11 my $tx = $ua->post($self->api_url => form => $form_params);
179              
180 1 50       14703 if (my $err = $tx->error) {
181 1 50       23 die 'Error - ' . $err->{code} . ' response: ' . $err->{message} if $err->{code};
182 1         26 die 'Error - Connection error: ' . $err->{message};
183             }
184              
185 0           my $headers = $tx->success->headers;
186 0           return Webservice::GAMSTOP::Response->new(
187             exclusion => $headers->header('x-exclusion'),
188             date => $headers->header('date'),
189             unique_id => $headers->header('x-unique-id'),
190             );
191             }
192              
193             1;
194             __END__