-
Notifications
You must be signed in to change notification settings - Fork 2
/
Pokemon.pm
237 lines (159 loc) · 5.39 KB
/
Pokemon.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package WebService::Pokemon;
use Mouse;
use Types::Standard qw/Str/;
use strictures 2;
use namespace::clean;
with 'Web::API';
use constant DEFAULT_BASE_API_URL => 'http://pokeapi.co/api/';
use constant DEFAULT_API_VERSION => 'v2';
our $VERSION = '0.06';
has 'api_version' => (
is => 'rw',
isa => Str,
default => sub { DEFAULT_API_VERSION }
);
has 'v2_endpoints' => (
is => 'rw',
default => sub {
{
pokemons => {
method => 'GET',
path => 'pokemon/',
default_attributes => { limit => 20, offset => 0 }
},
pokemon => {
method => 'GET',
require_id => 1,
path => 'pokemon/:id/'
},
berries => {
method => 'GET',
path => 'berry/',
default_attributes => { limit => 20, offset => 0 }
},
berry => {
method => 'GET',
require_id => 1,
path => 'berry/:id/'
},
berry_firmness => {
method => 'GET',
require_id => 1,
path => 'berry-firmness/:id/'
},
berry_flavor => {
method => 'GET',
require_id => 1,
path => 'berry-flavor/:id/'
},
};
},
);
has 'v1_endpoints' => (
is => 'rw',
default => sub {
{
};
},
);
around 'format_response' => sub {
my ($method, $self, $response, $ct, $error) = @_;
my $answer = $self->$method($response, $ct, $error);
return ($answer->{code} == 200)
? $answer->{content}
: undef;
};
sub commands {
my ($self) = @_;
my $api_version = $self->api_version;
return $self->v1_endpoints if ($api_version eq 'v1');
return $self->v2_endpoints if ($api_version eq 'v2');
}
sub BUILD {
my ($self, $args) = @_;
$self->api_version($args->{api_version}) if (defined $args->{api_version});
my $base_url = $args->{base_url} || DEFAULT_BASE_API_URL;
$base_url .= $self->api_version;
$self->user_agent(__PACKAGE__ . ' ' . $VERSION);
$self->base_url($base_url);
$self->content_type('application/json');
$self->debug(1) if ($ENV{LOGGING});
return $self;
}
1;
__END__
=encoding utf-8
=head1 NAME
WebService::Pokemon - A module to access the Pokémon data through RESTful API
from http://pokeapi.co.
=head1 SYNOPSIS
use WebService::Pokemon;
my $pokemon_api = WebService::Pokemon->new;
my $pokemon = $pokemon_api->pokemon(id => 1);
=head1 DESCRIPTION
WebService::Pokemon is a Perl client helper library for the Pokemon API (pokeapi.co).
=head1 DEVELOPMENT
Source repo at L<https://github.com/kianmeng/webservice-pokemon|https://github.com/kianmeng/webservice-pokemon>.
=head2 Docker
If you have Docker installed, you can build your Docker container for this
project.
$ docker build -t webservice-pokemon .
$ docker run -it -v $(pwd):/root webservice-pokemon bash
# cpanm --installdeps --notest .
=head2 Milla
Setting up the required packages.
$ cpanm Dist::Milla
$ milla listdeps --missing | cpanm
Check you code coverage.
$ milla cover
Several ways to run the test.
$ milla test
$ milla test --author --release
$ AUTHOR_TESTING=1 RELEASE_TESTING=1 milla test
$ AUTHOR_TESTING=1 RELEASE_TESTING=1 milla run prove t/01_instantiation.t
$ LOGGING=1 milla run prove t/t/03_pokemon.t
Release the module.
$ milla build
$ milla release
=head1 METHODS
=head2 new([%$args])
Construct a new WebService::Pokemon instance. Optionally takes a hash or hash reference.
# Instantiate the class.
my $pokemon_api = WebService::Pokemon->new;
=head3 base_url
The URL of the API resource.
# Instantiate the class by setting the URL of the API endpoints.
my $pokemon_api = WebService::Pokemon->new({api_url => 'http://example.com/api/v2'});
=head3 api_version
The API version of the API endpoints. By default, the API version was set to
'v2'.
# Instantiate the class by setting the API version.
my $pokemon_api = WebService::Pokemon->new({api_version => 'v1'});
=head2 api_version
Get the current API version of the web service.
my $version = $pokemon_api->api_version();
# Change the API version.
$pokemon_api->api_version('v1');
=head2 pokemon
Get the details of a particular Pokémon either by id or name.
my $pokemon = $pokemon_api->pokemon(id => 1);
my $pokemon = $pokemon_api->pokemon(id => 'bulbasaur');
=head2 berry
Get the details of a particular berry either by id or name.
my $pokemon = $pokemon_api->berry(id => 1);
my $pokemon = $pokemon_api->berry(id => 'cheri');
=head2 berry_firmness
Get the details of a particular berry firmness either by id or name.
my $pokemon = $pokemon_api->berry_firmness(id => 1);
my $pokemon = $pokemon_api->berry_firmness(id => 'very-soft');
=head2 berry_flavor
Get the details of a particular berry flavor either by id or name.
my $pokemon = $pokemon_api->berry_firmness(id => 1);
my $pokemon = $pokemon_api->berry_firmness(id => 'spicy');
=head2 commands
=head1 COPYRIGHT AND LICENSE
This software is Copyright (c) 2018 by Kian Meng, Ang.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
=head1 AUTHOR
Kian Meng, Ang E<lt>kianmeng@users.noreply.github.comE<gt>