| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Webservice::Swapi; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 8 |  |  | 8 |  | 562409 | use 5.008001; | 
|  | 8 |  |  |  |  | 103 |  | 
| 4 | 8 |  |  | 8 |  | 54 | use strict; | 
|  | 8 |  |  |  |  | 20 |  | 
|  | 8 |  |  |  |  | 207 |  | 
| 5 | 8 |  |  | 8 |  | 48 | use warnings; | 
|  | 8 |  |  |  |  | 17 |  | 
|  | 8 |  |  |  |  | 261 |  | 
| 6 | 8 |  |  | 8 |  | 50 | use utf8; | 
|  | 8 |  |  |  |  | 27 |  | 
|  | 8 |  |  |  |  | 67 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 8 |  |  | 8 |  | 4810 | use Moo; | 
|  | 8 |  |  |  |  | 97855 |  | 
|  | 8 |  |  |  |  | 40 |  | 
| 9 | 8 |  |  | 8 |  | 16269 | use Types::Standard qw(Str); | 
|  | 8 |  |  |  |  | 665188 |  | 
|  | 8 |  |  |  |  | 85 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | with 'Role::REST::Client'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $VERSION = '0.1.5'; | 
| 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 | 3717 | my ($self) = @_; | 
| 23 |  |  |  |  |  |  |  | 
| 24 | 8 |  | 50 |  |  | 229 | $self->set_persistent_header('User-Agent' => __PACKAGE__ . q| | | 
| 25 |  |  |  |  |  |  | . ($Webservice::Swapi::VERSION || q||)); | 
| 26 | 8 |  |  |  |  | 1441 | $self->server($self->api_url); | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 8 |  |  |  |  | 510 | return $self; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub ping { | 
| 32 | 2 |  |  | 2 | 1 | 1137 | my ($self) = @_; | 
| 33 |  |  |  |  |  |  |  | 
| 34 | 2 |  |  |  |  | 9 | my $response = $self->resources(); | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 2 | 100 |  |  |  | 13233 | return (!exists $response->{films}) ? 0 : 1; | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub resources { | 
| 40 | 5 |  |  | 5 | 1 | 16395 | my ($self, $format) = @_; | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 5 |  |  |  |  | 10 | my $queries; | 
| 43 | 5 | 100 |  |  |  | 23 | $queries->{format} = $format if (defined $format); | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 5 |  |  |  |  | 22 | return $self->_request(undef, undef, $queries); | 
| 46 |  |  |  |  |  |  | } | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | sub schema { | 
| 49 | 1 |  |  | 1 | 1 | 11 | my ($self, $object) = @_; | 
| 50 |  |  |  |  |  |  |  | 
| 51 | 1 |  |  |  |  | 9 | return $self->_request(qq|$object/schema|); | 
| 52 |  |  |  |  |  |  | } | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | sub search { | 
| 55 | 2 |  |  | 2 | 1 | 14880 | my ($self, $object, $keyword, $format) = @_; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 2 |  |  |  |  | 5 | my $queries; | 
| 58 | 2 |  |  |  |  | 8 | $queries->{search} = $keyword; | 
| 59 | 2 | 100 |  |  |  | 9 | $queries->{format} = $format if (defined $format); | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 2 |  |  |  |  | 11 | return $self->_request($object, undef, $queries); | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub get_object { | 
| 65 | 2 |  |  | 2 | 1 | 16524 | my ($self, $object, $id, $format) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 2 |  |  |  |  | 5 | my $queries; | 
| 68 | 2 | 100 |  |  |  | 9 | $queries->{format} = $format if (defined $format); | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 2 |  |  |  |  | 9 | return $self->_request($object, $id, $queries); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub _request { | 
| 74 | 13 |  |  | 13 |  | 13612 | my ($self, $object, $id, $queries) = @_; | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 13 |  |  |  |  | 313 | $self->server($self->api_url); | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 13 |  |  |  |  | 704 | my @paths; | 
| 80 | 13 | 100 |  |  |  | 66 | push @paths, $object if (defined $object); | 
| 81 | 13 | 100 |  |  |  | 74 | push @paths, $id     if (defined $id); | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 13 |  |  |  |  | 47 | my ($url_paths, $url_queries) = (q||, q||); | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 13 |  |  |  |  | 64 | $url_paths = join q|/|, @paths; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 13 | 100 |  |  |  | 43 | if (defined $queries) { | 
| 88 | 7 |  |  |  |  | 15 | my @pairs; | 
| 89 | 7 |  |  |  |  | 15 | foreach my $k (keys %{$queries}) { | 
|  | 7 |  |  |  |  | 30 |  | 
| 90 | 8 |  |  |  |  | 35 | push @pairs, $k . q|=| . $queries->{$k}; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 7 | 100 |  |  |  | 32 | $url_queries .= ($url_paths eq q||) ? q|?| : q|/?|; | 
| 94 | 7 |  |  |  |  | 27 | $url_queries .= join q|&|, @pairs; | 
| 95 |  |  |  |  |  |  | } | 
| 96 |  |  |  |  |  |  |  | 
| 97 | 13 |  |  |  |  | 53 | my $url = $url_paths . $url_queries; | 
| 98 |  |  |  |  |  |  |  | 
| 99 | 13 |  |  |  |  | 63 | my $response = $self->get($url); | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 13 | 100 |  |  |  | 6906679 | return $response->data if ($response->code eq '200'); | 
| 102 |  |  |  |  |  |  |  | 
| 103 | 1 |  |  |  |  | 11 | 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 Carton | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | To setup the development environment and run the test using Carton. | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | $ carton install | 
| 158 |  |  |  |  |  |  | $ export PERL5LIB=$(pwd)/local/lib/perl5/ | 
| 159 |  |  |  |  |  |  | $ export PATH=$HOME/perl5/bin:$(pwd)/local/bin:$PATH | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | To enable Perl::Critic test cases, enable the flag. | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | $ AUTHOR_TESTING=1 carton exec -- prove -Ilib -lv t | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =head2 Minilla | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | To use Minilla instead. This will update the README.md file from the source. | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | $ cpanm Minilla | 
| 170 |  |  |  |  |  |  | $ minil build | 
| 171 |  |  |  |  |  |  | $ minil test | 
| 172 |  |  |  |  |  |  | $ FAKE_RELEASE=1 minil release # testing | 
| 173 |  |  |  |  |  |  | $ minil release # actual | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =head1 METHODS | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =head2 new([%$args]) | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | Construct a new Webservice::Swapi instance. Optionally takes a hash or hash reference. | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | =over 4 | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | =item api_url | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | The URL of the API resource. | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | =back | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | # Instantiate the class. | 
| 190 |  |  |  |  |  |  | my $swapi = Webservice::Swapi->new; | 
| 191 |  |  |  |  |  |  |  | 
| 192 |  |  |  |  |  |  | # Set the API URL. | 
| 193 |  |  |  |  |  |  | my $swapi = Webservice::Swapi->new({api_url => 'http://example.com/api/'}); | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | =head2 get_object($object, [$format]) | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | Get full details of a object or resource. Optionally takes a returned format. | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | # Get the details of different available object using id. | 
| 200 |  |  |  |  |  |  | my $object = $swapi->get_object('films', '1'); | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | # Get the result in different format. | 
| 203 |  |  |  |  |  |  | my $object_json = $swapi->get_object('films', '1', 'json'); | 
| 204 |  |  |  |  |  |  | my $object_wookie = $swapi->get_object('films', '1', 'wookiee'); | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | =head2 ping() | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | Check if the API service or server is responding to a request. | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | my $server_status = $swapi->ping(); | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =head2 resources([$format]) | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | List down all the available objects. Optionally takes a returned format. | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | # Get all available resources or objects. | 
| 217 |  |  |  |  |  |  | my $resources = $swapi->resources(); | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | # Similarly but in different format. | 
| 220 |  |  |  |  |  |  | my $resources_json = $swapi->resources('json'); | 
| 221 |  |  |  |  |  |  | my $resources_wookie = $swapi->resources('wookie'); | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | =head2 schema($object) | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | Show the data structure of a resource or object. | 
| 226 |  |  |  |  |  |  |  | 
| 227 |  |  |  |  |  |  | # Get the schema / structure of a resource or object. | 
| 228 |  |  |  |  |  |  | my $schema = $swapi->schema('people'); | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  | =head2 search($object, $keyword, [$format]) | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | Searching by keywords. Takes both an object and keywords. Optionally takes a returned format. | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | # Search a resource or object by keywords. | 
| 235 |  |  |  |  |  |  | my $results = $swapi->search('people', 'solo'); | 
| 236 |  |  |  |  |  |  |  | 
| 237 |  |  |  |  |  |  | # Or in different format. | 
| 238 |  |  |  |  |  |  | my $results = $swapi->search('people', 'solo', 'json'); | 
| 239 |  |  |  |  |  |  | my $results = $swapi->search('people', 'solo', 'wookiee'); | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 242 |  |  |  |  |  |  |  | 
| 243 |  |  |  |  |  |  | This software is Copyright (c) 2017 by Kian Meng, Ang. | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | This is free software, licensed under: | 
| 246 |  |  |  |  |  |  |  | 
| 247 |  |  |  |  |  |  | The Artistic License 2.0 (GPL Compatible) | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | =head1 AUTHOR | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | Kian-Meng, Ang E<lt>kianmeng@users.noreply.github.comE<gt> | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | =cut | 
| 254 |  |  |  |  |  |  |  |