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