line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Pokemon; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
326782
|
use utf8; |
|
3
|
|
|
|
|
27
|
|
|
3
|
|
|
|
|
15
|
|
4
|
3
|
|
|
3
|
|
1046
|
use strictures 2; |
|
3
|
|
|
|
|
3359
|
|
|
3
|
|
|
|
|
84
|
|
5
|
3
|
|
|
3
|
|
1428
|
use namespace::clean; |
|
3
|
|
|
|
|
23972
|
|
|
3
|
|
|
|
|
13
|
|
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
939
|
use CHI; |
|
3
|
|
|
|
|
59512
|
|
|
3
|
|
|
|
|
61
|
|
8
|
3
|
|
|
3
|
|
20
|
use Digest::MD5 qw(md5_hex); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
142
|
|
9
|
3
|
|
|
3
|
|
13
|
use Moo; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
18
|
|
10
|
3
|
|
|
3
|
|
1775
|
use Sereal qw(encode_sereal decode_sereal); |
|
3
|
|
|
|
|
1925
|
|
|
3
|
|
|
|
|
126
|
|
11
|
3
|
|
|
3
|
|
1134
|
use Types::Standard qw(Str); |
|
3
|
|
|
|
|
160524
|
|
|
3
|
|
|
|
|
22
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
with 'Role::REST::Client'; |
14
|
|
|
|
|
|
|
|
15
|
3
|
|
|
3
|
|
2658
|
use WebService::Pokemon::APIResourceList; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
100
|
|
16
|
3
|
|
|
3
|
|
1047
|
use WebService::Pokemon::NamedAPIResource; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
1235
|
|
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
has 'api_url' => ( |
22
|
|
|
|
|
|
|
isa => Str, |
23
|
|
|
|
|
|
|
is => 'rw', |
24
|
|
|
|
|
|
|
default => sub { 'https://pokeapi.co/api/v2' }, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
has cache => ( |
28
|
|
|
|
|
|
|
is => 'rw', |
29
|
|
|
|
|
|
|
lazy => 1, |
30
|
|
|
|
|
|
|
builder => 1, |
31
|
|
|
|
|
|
|
); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub _build_cache { |
34
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
35
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
0
|
return CHI->new( |
37
|
|
|
|
|
|
|
driver => 'File', |
38
|
|
|
|
|
|
|
namespace => 'restcountries', |
39
|
|
|
|
|
|
|
root_dir => '/tmp/cache/', |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub BUILD { |
44
|
4
|
|
|
4
|
0
|
1583
|
my ($self, $args) = @_; |
45
|
|
|
|
|
|
|
|
46
|
4
|
|
|
|
|
14
|
foreach my $arg (keys %$args) { |
47
|
3
|
50
|
|
|
|
49
|
$self->$arg($args->{$arg}) if (defined $args->{$arg}); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
4
|
|
50
|
|
|
99
|
$self->set_persistent_header('User-Agent' => __PACKAGE__ . q| | |
51
|
|
|
|
|
|
|
. ($WebService::Pokemon::VERSION || q||)); |
52
|
4
|
|
|
|
|
493
|
$self->server($self->api_url); |
53
|
|
|
|
|
|
|
|
54
|
4
|
|
|
|
|
189
|
return $self; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub _request { |
58
|
6
|
|
|
6
|
|
3097
|
my ($self, $resource, $name, $queries) = @_; |
59
|
|
|
|
|
|
|
|
60
|
6
|
100
|
66
|
|
|
50
|
return if (!defined $resource || length $resource <= 0); |
61
|
|
|
|
|
|
|
|
62
|
5
|
|
100
|
|
|
28
|
$queries ||= {}; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
5
|
|
|
|
|
99
|
$self->server($self->api_url); |
66
|
5
|
|
|
|
|
304
|
$self->type(qq|application/json|); |
67
|
|
|
|
|
|
|
|
68
|
5
|
|
|
|
|
148
|
my $endpoint = q||; |
69
|
5
|
|
|
|
|
13
|
$endpoint .= "/" . $resource; |
70
|
5
|
100
|
|
|
|
17
|
$endpoint .= "/" . $name if (defined $name); |
71
|
|
|
|
|
|
|
|
72
|
5
|
|
|
|
|
10
|
my $response_data; |
73
|
5
|
|
|
|
|
142
|
my $cache_key = md5_hex($endpoint . encode_sereal($queries)); |
74
|
|
|
|
|
|
|
|
75
|
5
|
|
|
|
|
77
|
my $cache_response_data = $self->cache->get($cache_key); |
76
|
5
|
50
|
|
|
|
939
|
if (defined $cache_response_data) { |
77
|
0
|
|
|
|
|
0
|
$response_data = decode_sereal($cache_response_data); |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else { |
80
|
5
|
|
|
|
|
38
|
my $response = $self->get($endpoint, $queries); |
81
|
5
|
|
|
|
|
10119613
|
$response_data = $response->data; |
82
|
|
|
|
|
|
|
|
83
|
5
|
|
|
|
|
19470
|
$self->cache->set($cache_key, encode_sereal($response->data)); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
5
|
|
|
|
|
8464
|
return $response_data; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub resource { |
90
|
2
|
|
|
2
|
1
|
1525
|
my ($self, $resource, $id_or_name, $limit, $offset) = @_; |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
5
|
my $queries; |
93
|
2
|
100
|
|
|
|
8
|
if (!defined $id_or_name) { |
94
|
1
|
|
50
|
|
|
7
|
$queries->{limit} = $limit || 20; |
95
|
1
|
|
50
|
|
|
4
|
$queries->{offset} = $offset || 0; |
96
|
|
|
|
|
|
|
|
97
|
1
|
|
|
|
|
3
|
my $response = $self->_request($resource, $id_or_name, $queries); |
98
|
1
|
|
|
|
|
11
|
return WebService::Pokemon::APIResourceList->new(api => $self, response => $response); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
1
|
|
|
|
|
5
|
my $response = $self->_request($resource, $id_or_name, $queries); |
102
|
1
|
|
|
|
|
13
|
return WebService::Pokemon::NamedAPIResource->new(api => $self, response => $response); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
1; |
107
|
|
|
|
|
|
|
__END__ |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=encoding utf-8 |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 NAME |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
WebService::Pokemon - A module to access the Pokémon data through RESTful API |
114
|
|
|
|
|
|
|
from http://pokeapi.co. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SYNOPSIS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
use WebService::Pokemon; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
my $pokemon_api = WebService::Pokemon->new; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# By id. |
123
|
|
|
|
|
|
|
my $pokemon = $pokemon_api->resource('berry', 1); |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# By name. |
126
|
|
|
|
|
|
|
my $pokemon = $pokemon_api->resource('berry', 'cheri'); |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head1 DESCRIPTION |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
WebService::Pokemon is a Perl client helper library for the Pokemon API (pokeapi.co). |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head1 DEVELOPMENT |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Source repo at L<https://github.com/kianmeng/webservice-pokemon|https://github.com/kianmeng/webservice-pokemon>. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 Docker |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
If you have Docker installed, you can build your Docker container for this |
139
|
|
|
|
|
|
|
project. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
$ docker build -t webservice-pokemon . |
142
|
|
|
|
|
|
|
$ docker run -it -v $(pwd):/root webservice-pokemon bash |
143
|
|
|
|
|
|
|
# cpanm --installdeps --notest . |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=head2 Milla |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Setting up the required packages. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
$ milla authordeps --missing | cpanm |
150
|
|
|
|
|
|
|
$ milla listdeps --missing | cpanm |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Check you code coverage. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
$ milla cover |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Several ways to run the test. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
$ milla test |
159
|
|
|
|
|
|
|
$ milla test --author --release |
160
|
|
|
|
|
|
|
$ AUTHOR_TESTING=1 RELEASE_TESTING=1 milla test |
161
|
|
|
|
|
|
|
$ AUTHOR_TESTING=1 RELEASE_TESTING=1 milla run prove t/01_instantiation.t |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Release the module. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
$ milla build |
166
|
|
|
|
|
|
|
$ milla release |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 METHODS |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 new([%$args]) |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Construct a new WebService::Pokemon instance. Optionally takes a hash or hash reference. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
# Instantiate the class. |
175
|
|
|
|
|
|
|
my $pokemon_api = WebService::Pokemon->new; |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=head3 api_url |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
The URL of the API resource. |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
# Instantiate the class by setting the URL of the API endpoints. |
182
|
|
|
|
|
|
|
my $pokemon_api = WebService::Pokemon->new({api_url => 'http://example.com/api/v2'}); |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
# Or after the object was created. |
185
|
|
|
|
|
|
|
my $pokemon_api = WebService::Pokemon->new; |
186
|
|
|
|
|
|
|
$pokemon_api->api_url('http://example.com/api/v2'); |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
=head3 cache |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
The cache directory of the HTTP reponses. By default, all cached data is stored |
191
|
|
|
|
|
|
|
as files in /tmp/cache/. |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
# Default cache engine is file-based storage. |
194
|
|
|
|
|
|
|
my $pokemon_api = WebService::Pokemon->new; |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
# Or we define our custom cache engine with settings. |
197
|
|
|
|
|
|
|
my $pokemon_api = WebService::Pokemon->new( |
198
|
|
|
|
|
|
|
cache => CHI->new( |
199
|
|
|
|
|
|
|
driver => 'File', |
200
|
|
|
|
|
|
|
namespace => 'restcountries', |
201
|
|
|
|
|
|
|
root_dir => $ENV{PWD} . '/tmp/cache/', |
202
|
|
|
|
|
|
|
) |
203
|
|
|
|
|
|
|
); |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# Or after the object was created. |
206
|
|
|
|
|
|
|
my $pokemon_api = WebService::Pokemon->new; |
207
|
|
|
|
|
|
|
$pokemon_api->cache( |
208
|
|
|
|
|
|
|
cache => CHI->new( |
209
|
|
|
|
|
|
|
driver => 'File', |
210
|
|
|
|
|
|
|
namespace => 'restcountries', |
211
|
|
|
|
|
|
|
root_dir => $ENV{PWD} . '/tmp/cache/', |
212
|
|
|
|
|
|
|
) |
213
|
|
|
|
|
|
|
); |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=head2 resource($resource, [$name], [$limit], [$offset]) |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
Get the details of a particular resource with optional id or name; limit per |
218
|
|
|
|
|
|
|
page, or offset by the record list. |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
# Get paginated list of available berry resource. |
221
|
|
|
|
|
|
|
my $berry = $pokemon_api->resource('berry'); |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
# Or by page through limit and pagination. |
224
|
|
|
|
|
|
|
my $berry = $pokemon_api->resource('berry', undef, 60, 20); |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
# Get by id. |
227
|
|
|
|
|
|
|
my $berry_firmness = $pokemon_api->resource('berry-firmnesses', 1); |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# Get by name. |
230
|
|
|
|
|
|
|
my $berry_firmness = $pokemon_api->resource('berry-firmnesses', 'very-soft'); |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
This software is Copyright (c) 2018 by Kian Meng, Ang. |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
This is free software, licensed under: |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=head1 AUTHOR |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
Kian Meng, Ang E<lt>kianmeng@users.noreply.github.comE<gt> |
243
|
|
|
|
|
|
|
|