File Coverage

blib/lib/WebService/Pokemon.pm
Criterion Covered Total %
statement 39 72 54.1
branch 1 10 10.0
condition 1 11 9.0
subroutine 12 15 80.0
pod 2 3 66.6
total 55 111 49.5


line stmt bran cond sub pod time code
1             package WebService::Pokemon;
2              
3 5     5   827773 use 5.008_005;
  5         40  
4 5     5   2389 use strictures 2;
  5         7908  
  5         189  
5 5     5   3277 use namespace::clean;
  5         76998  
  5         30  
6 5     5   1137 use utf8;
  5         10  
  5         38  
7              
8 5     5   2754 use Moo;
  5         34184  
  5         23  
9 5     5   9787 use Types::Standard qw(Bool Str);
  5         363452  
  5         56  
10 5     5   7525 use URI::Fast qw(uri);
  5         17755  
  5         337  
11              
12 5     5   2065 use WebService::Pokemon::APIResourceList;
  5         20  
  5         196  
13 5     5   2347 use WebService::Pokemon::NamedAPIResource;
  5         15  
  5         226  
14              
15             with 'Role::REST::Client';
16              
17 5     5   35 use constant DEFAULT_ITEMS_PER_PAGE => 20;
  5         11  
  5         375  
18 5     5   32 use constant DEFAULT_ITEMS_OFFSET => 0;
  5         12  
  5         3307  
19              
20             our $VERSION = '0.11';
21              
22             has 'api_url' => (
23                 isa => Str,
24                 is => 'rw',
25                 default => sub { 'https://pokeapi.co/api/v2' },
26             );
27              
28             has autoload => (
29                 isa => Bool,
30                 is => 'rw',
31                 default => sub { 0 },
32             );
33              
34             sub BUILD {
35 2     2 0 389     my ($self, $args) = @_;
36              
37 2         4     foreach my $arg (keys %{$args}) {
  2         8  
38 1 50       23         $self->$arg($args->{$arg}) if (defined $args->{$arg});
39                 }
40              
41 2   50     83     $self->set_persistent_header('User-Agent' => __PACKAGE__ . q| |
42                         . ($WebService::Pokemon::VERSION || q||));
43 2         283     $self->server($self->api_url);
44              
45 2         113     return $self;
46             }
47              
48             sub _request {
49 0     0         my ($self, $resource, $id_or_name, $queries) = @_;
50              
51 0 0 0           return if (!defined $resource || length $resource <= 0);
52              
53 0   0           $queries ||= {};
54              
55             # In case the api_url was updated.
56 0               $self->server($self->api_url);
57 0               $self->type(q|application/json|);
58              
59 0               my $endpoint = q||;
60 0               $endpoint .= qq|/$resource|;
61 0 0             $endpoint .= qq|/$id_or_name| if (defined $id_or_name);
62              
63 0               my $response = $self->get($endpoint, $queries);
64 0               my $response_data = $response->data;
65              
66 0               return $response_data;
67             }
68              
69             sub resource {
70 0     0 1       my ($self, $resource, $id_or_name, $limit, $offset) = @_;
71              
72 0               my $queries;
73 0 0             if (!defined $id_or_name) {
74 0   0               $queries->{limit} = $limit || DEFAULT_ITEMS_PER_PAGE;
75 0   0               $queries->{offset} = $offset || DEFAULT_ITEMS_OFFSET;
76              
77 0                   my $response = $self->_request($resource, $id_or_name, $queries);
78              
79 0                   return WebService::Pokemon::APIResourceList->new(
80                         api => $self,
81                         response => $response
82                     );
83                 }
84              
85 0               my $response = $self->_request($resource, $id_or_name, $queries);
86              
87 0               return WebService::Pokemon::NamedAPIResource->new(
88                     api => $self,
89                     response => $response
90                 );
91             }
92              
93             sub resource_by_url {
94 0     0 1       my ($self, $url) = @_;
95              
96 0               my $uri = uri($url);
97              
98 0               my ($resource, $id_or_name);
99              
100 0               my $split_path = $uri->split_path;
101 0 0             if (scalar @{$split_path} == 3) {
  0            
102 0                   $resource = @{$split_path}[-1];
  0            
103                 }
104                 else {
105 0                   $resource = @{$split_path}[-2];
  0            
106 0                   $id_or_name = @{$split_path}[-1];
  0            
107                 }
108              
109 0               return $self->resource(
110                     $resource, $id_or_name,
111                     $uri->param('limit'),
112                     $uri->param('offset'));
113             }
114              
115             1;
116             __END__
117            
118             =encoding utf-8
119            
120             =for stopwords pokemon pokémon pokeapi autoload
121            
122             =head1 NAME
123            
124             WebService::Pokemon - Perl library for accessing the Pokémon data,
125             http://pokeapi.co.
126            
127             =head1 SYNOPSIS
128            
129             use WebService::Pokemon;
130            
131             my $pokemon_api = WebService::Pokemon->new;
132            
133             # By id.
134             my $pokemon = $pokemon_api->resource('berry', 1);
135            
136             # By name.
137             my $pokemon = $pokemon_api->resource('berry', 'cheri');
138            
139             =head1 DESCRIPTION
140            
141             WebService::Pokemon is a Perl client helper library for the Pokemon API (pokeapi.co).
142            
143             =head1 DEVELOPMENT
144            
145             Source repository at L<https://github.com/kianmeng/webservice-pokemon|https://github.com/kianmeng/webservice-pokemon>.
146            
147             How to contribute? Follow through the L<CONTRIBUTING.md|https://github.com/kianmeng/webservice-pokemon/blob/master/CONTRIBUTING.md> document to setup your development environment.
148            
149             =head1 METHODS
150            
151             =head2 new([%$args])
152            
153             Construct a new WebService::Pokemon instance. Optionally takes a hash or hash reference.
154            
155             # Instantiate the class.
156             my $pokemon_api = WebService::Pokemon->new;
157            
158             =head3 api_url
159            
160             The URL of the API resource.
161            
162             # Instantiate the class by setting the URL of the API endpoints.
163             my $pokemon_api = WebService::Pokemon->new({api_url => 'http://example.com/api/v2'});
164            
165             # Or after the object was created.
166             my $pokemon_api = WebService::Pokemon->new;
167             $pokemon_api->api_url('http://example.com/api/v2');
168            
169             =head3 autoload
170            
171             Set this if you want to expand all fields point to external URL.
172            
173             # Instantiate the class by setting the URL of the API endpoints.
174             my $pokemon_api = WebService::Pokemon->new({autoload => 1});
175            
176             # Or after the object was created.
177             my $pokemon_api = WebService::Pokemon->new;
178             $pokemon_api->autoload(1);
179             $pokemon_api->resource('berry');
180            
181             =head2 resource($resource, [$name], [$limit], [$offset])
182            
183             Get the details of a particular resource with optional id or name; limit per
184             page, or offset by the record list.
185            
186             # Get paginated list of available berry resource.
187             my $berry = $pokemon_api->resource('berry');
188            
189             # Or by page through limit and pagination.
190             my $berry = $pokemon_api->resource('berry', undef, 60, 20);
191            
192             # Get by id.
193             my $berry_firmness = $pokemon_api->resource('berry-firmnesses', 1);
194            
195             # Get by name.
196             my $berry_firmness = $pokemon_api->resource('berry-firmnesses', 'very-soft');
197            
198             =head2 resource_by_url($url)
199            
200             Get the details of a particular resource by full URL.
201            
202             # Get paginated list of available berry resource with default item size.
203             my $berries = $pokemon_api->resource_by_url('https://pokeapi.co/api/v2/berry/');
204            
205             # Get paginated list of available berry resource with explicit default item size.
206             my $berries = $pokemon_api->resource_by_url('https://pokeapi.co/api/v2/berry/?limit=20&offset=40');
207            
208             # Get particular berry resource.
209             my $berry = $pokemon_api->resource_by_url('https://pokeapi.co/api/v2/berry/1');
210            
211             =head1 AUTHOR
212            
213             Kian Meng, Ang E<lt>kianmeng@cpan.orgE<gt>
214            
215             =head1 COPYRIGHT AND LICENSE
216            
217             This software is Copyright (c) 2018-2019 by Kian Meng, Ang.
218            
219             This is free software, licensed under:
220            
221             The Artistic License 2.0 (GPL Compatible)
222            
223