line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Swapi; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
535201
|
use utf8; |
|
8
|
|
|
|
|
85
|
|
|
8
|
|
|
|
|
52
|
|
4
|
|
|
|
|
|
|
|
5
|
8
|
|
|
8
|
|
4351
|
use Moo; |
|
8
|
|
|
|
|
95014
|
|
|
8
|
|
|
|
|
46
|
|
6
|
8
|
|
|
8
|
|
16875
|
use Types::Standard qw(Str); |
|
8
|
|
|
|
|
644516
|
|
|
8
|
|
|
|
|
109
|
|
7
|
|
|
|
|
|
|
|
8
|
8
|
|
|
8
|
|
10320
|
use strictures 2; |
|
8
|
|
|
|
|
13231
|
|
|
8
|
|
|
|
|
339
|
|
9
|
8
|
|
|
8
|
|
5278
|
use namespace::clean; |
|
8
|
|
|
|
|
91908
|
|
|
8
|
|
|
|
|
54
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Role::REST::Client'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.1.7'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has api_url => ( |
16
|
|
|
|
|
|
|
isa => Str, |
17
|
|
|
|
|
|
|
is => 'rw', |
18
|
|
|
|
|
|
|
default => sub { 'https://swapi.co/api/' }, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub BUILD { |
22
|
8
|
|
|
8
|
0
|
3101
|
my ($self) = @_; |
23
|
|
|
|
|
|
|
|
24
|
8
|
|
50
|
|
|
202
|
$self->set_persistent_header('User-Agent' => __PACKAGE__ . q| | |
25
|
|
|
|
|
|
|
. ($WebService::Swapi::VERSION || q||)); |
26
|
8
|
|
|
|
|
1171
|
$self->server($self->api_url); |
27
|
|
|
|
|
|
|
|
28
|
8
|
|
|
|
|
458
|
return $self; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub ping { |
32
|
2
|
|
|
2
|
1
|
1145
|
my ($self) = @_; |
33
|
|
|
|
|
|
|
|
34
|
2
|
|
|
|
|
9
|
my $response = $self->resources(); |
35
|
|
|
|
|
|
|
|
36
|
2
|
100
|
|
|
|
15916
|
return (!exists $response->{films}) ? 0 : 1; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub resources { |
40
|
5
|
|
|
5
|
1
|
24989
|
my ($self, $format) = @_; |
41
|
|
|
|
|
|
|
|
42
|
5
|
|
|
|
|
10
|
my $queries; |
43
|
5
|
100
|
|
|
|
25
|
$queries->{format} = $format if (defined $format); |
44
|
|
|
|
|
|
|
|
45
|
5
|
|
|
|
|
19
|
return $self->_request(undef, undef, $queries); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub schema { |
49
|
1
|
|
|
1
|
1
|
10
|
my ($self, $object) = @_; |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
6
|
return $self->_request(qq|$object/schema|); |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub search { |
55
|
2
|
|
|
2
|
1
|
19277
|
my ($self, $object, $keyword, $format) = @_; |
56
|
|
|
|
|
|
|
|
57
|
2
|
|
|
|
|
4
|
my $queries; |
58
|
2
|
|
|
|
|
5
|
$queries->{search} = $keyword; |
59
|
2
|
100
|
|
|
|
9
|
$queries->{format} = $format if (defined $format); |
60
|
|
|
|
|
|
|
|
61
|
2
|
|
|
|
|
7
|
return $self->_request($object, undef, $queries); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub get_object { |
65
|
2
|
|
|
2
|
1
|
22203
|
my ($self, $object, $id, $format) = @_; |
66
|
|
|
|
|
|
|
|
67
|
2
|
|
|
|
|
5
|
my $queries; |
68
|
2
|
100
|
|
|
|
9
|
$queries->{format} = $format if (defined $format); |
69
|
|
|
|
|
|
|
|
70
|
2
|
|
|
|
|
8
|
return $self->_request($object, $id, $queries); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub _request { |
74
|
14
|
|
|
14
|
|
18918
|
my ($self, $object, $id, $queries) = @_; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
|
77
|
14
|
|
|
|
|
348
|
$self->server($self->api_url); |
78
|
14
|
|
|
|
|
1065
|
$self->type(qq|application/json|); |
79
|
|
|
|
|
|
|
|
80
|
14
|
|
|
|
|
499
|
my @paths; |
81
|
14
|
100
|
|
|
|
61
|
push @paths, $object if (defined $object); |
82
|
14
|
100
|
|
|
|
64
|
push @paths, $id if (defined $id); |
83
|
|
|
|
|
|
|
|
84
|
14
|
|
|
|
|
46
|
my $endpoint = q||; |
85
|
14
|
|
|
|
|
72
|
$endpoint = join q|/|, @paths; |
86
|
|
|
|
|
|
|
|
87
|
14
|
|
|
|
|
34
|
my $response; |
88
|
14
|
|
|
|
|
97
|
$response = $self->get($endpoint, $queries); |
89
|
|
|
|
|
|
|
|
90
|
14
|
100
|
|
|
|
6463296
|
return $response->data if ($response->code eq '200'); |
91
|
|
|
|
|
|
|
|
92
|
2
|
|
|
|
|
28
|
return; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
1; |
96
|
|
|
|
|
|
|
__END__ |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=encoding utf-8 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 NAME |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
WebService::Swapi - A Perl module to interface with the Star Wars API |
103
|
|
|
|
|
|
|
(swapi.co) webservice. |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 SYNOPSIS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
use WebService::Swapi; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
$swapi = WebService::Swapi->new; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
# Check if API server is up |
112
|
|
|
|
|
|
|
my $resources = $swapi->ping(); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Get information of all available resources |
115
|
|
|
|
|
|
|
my $resources = $swapi->resources(); |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
# View the JSON schema for people resource |
118
|
|
|
|
|
|
|
my $schema = $swapi->schema('people'); |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
# Searching |
121
|
|
|
|
|
|
|
my $results = $swapi->search('people', 'solo'); |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# Get resource item |
124
|
|
|
|
|
|
|
my $item = $swapi->get_object('films', '1'); |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 DESCRIPTION |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
WebService::Swapi is a Perl client helper library for the Star Wars API (swapi.co). |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 DEVELOPMENT |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Source repo at L<https://github.com/kianmeng/webservice-swapi|https://github.com/kianmeng/webservice-swapi>. |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head2 Docker |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
If you have Docker installed, you can build your Docker container for this |
137
|
|
|
|
|
|
|
project. |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
$ docker build -t webservice-swapi . |
140
|
|
|
|
|
|
|
$ docker run -it -v $(pwd):/root webservice-swapi bash |
141
|
|
|
|
|
|
|
# cpanm --installdeps --notest . |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 Milla |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
Setting up the required packages. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
$ cpanm Dist::Milla |
148
|
|
|
|
|
|
|
$ milla listdeps --missing | cpanm |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Check you code coverage. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$ milla cover |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Several ways to run the test. |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
$ milla test |
157
|
|
|
|
|
|
|
$ milla test --author --release |
158
|
|
|
|
|
|
|
$ AUTHOR_TESTING=1 RELEASE_TESTING=1 milla test |
159
|
|
|
|
|
|
|
$ AUTHOR_TESTING=1 RELEASE_TESTING=1 milla run prove t/01_instantiation.t |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Release the module. |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
$ milla build |
164
|
|
|
|
|
|
|
$ milla release |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 METHODS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 new([%$args]) |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Construct a new WebService::Swapi instance. Optionally takes a hash or hash reference. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Instantiate the class. |
173
|
|
|
|
|
|
|
my $swapi = WebService::Swapi->new; |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head3 api_url |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
The URL of the API resource. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
# Instantiate the class by setting the URL of the API endpoints. |
180
|
|
|
|
|
|
|
my $swapi = WebService::Swapi->new({api_url => 'http://example.com/api/'}); |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=head2 get_object($object, [$format]) |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Get full details of a object or resource. Optionally takes a returned format. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
# Get the details of different available object using id. |
187
|
|
|
|
|
|
|
my $object = $swapi->get_object('films', '1'); |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
# Get the result in different format. |
190
|
|
|
|
|
|
|
my $object_json = $swapi->get_object('films', '1', 'json'); |
191
|
|
|
|
|
|
|
my $object_wookie = $swapi->get_object('films', '1', 'wookiee'); |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=head2 ping() |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Check if the API service or server is responding to a request. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
my $server_status = $swapi->ping(); |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head2 resources([$format]) |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
List down all the available objects. Optionally takes a returned format. |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
# Get all available resources or objects. |
204
|
|
|
|
|
|
|
my $resources = $swapi->resources(); |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
# Similarly but in different format. |
207
|
|
|
|
|
|
|
my $resources_json = $swapi->resources('json'); |
208
|
|
|
|
|
|
|
my $resources_wookie = $swapi->resources('wookie'); |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=head2 schema($object) |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
Show the data structure of a resource or object. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
# Get the schema / structure of a resource or object. |
215
|
|
|
|
|
|
|
my $schema = $swapi->schema('people'); |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head2 search($object, $keyword, [$format]) |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
Searching by keywords. Takes both an object and keywords. Optionally takes a returned format. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# Search a resource or object by keywords. |
222
|
|
|
|
|
|
|
my $results = $swapi->search('people', 'solo'); |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
# Or in different format. |
225
|
|
|
|
|
|
|
my $results = $swapi->search('people', 'solo', 'json'); |
226
|
|
|
|
|
|
|
my $results = $swapi->search('people', 'solo', 'wookiee'); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
This software is Copyright (c) 2017 by Kian Meng, Ang. |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
This is free software, licensed under: |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
The Artistic License 2.0 (GPL Compatible) |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head1 AUTHOR |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Kian-Meng, Ang E<lt>kianmeng@users.noreply.github.comE<gt> |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=cut |
241
|
|
|
|
|
|
|
|