| line | stmt | bran | cond | sub | pod | time | code | 
| 1 | 1 |  |  | 1 |  | 92247 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 43 |  | 
| 2 | 1 |  |  | 1 |  | 9 | use warnings; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 3 | 1 |  |  | 1 |  | 20 | use 5.12.0; | 
|  | 1 |  |  |  |  | 10 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 703 | use REST::Client; | 
|  | 1 |  |  |  |  | 63688 |  | 
|  | 1 |  |  |  |  | 36 |  | 
| 6 | 1 |  |  | 1 |  | 825 | use JSON; | 
|  | 1 |  |  |  |  | 9029 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 189 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 70 |  | 
| 9 |  |  |  |  |  |  | $VERSION = '1.001'; | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 NAME | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | WebService::IFConfig::Client - Client for Martin Polden's https://ifconfig.co | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | use strict; | 
| 18 |  |  |  |  |  |  | use warnings; | 
| 19 |  |  |  |  |  |  | use 5.12.0; | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | use feature qw/say/; | 
| 22 |  |  |  |  |  |  | use WebService::IFConfig::Client; | 
| 23 |  |  |  |  |  |  | my $ifconfig = WebService::IFConfig::Client->new(); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | say $ifconfig->get_city; | 
| 26 |  |  |  |  |  |  | say $ifconfig->get_country; | 
| 27 |  |  |  |  |  |  | say $ifconfig->get_hostname; | 
| 28 |  |  |  |  |  |  | say $ifconfig->get_ip; | 
| 29 |  |  |  |  |  |  | say $ifconfig->get_ip_decimal; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # Time passes ... | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | # Request again | 
| 34 |  |  |  |  |  |  | $ifconfig->request; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | Calling C<new()> with no arguments, defaults to requesting immediately from https://ifconfig.co. | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | To defer requesting, the data you can pass the argument C<'run' =E<gt> 0>. | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | To use a different server, pass C<'server' =E<gt> $my_server>. | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =cut | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | package WebService::IFConfig::Client; | 
| 45 | 1 |  |  | 1 |  | 581 | use Moose; | 
|  | 1 |  |  |  |  | 632852 |  | 
|  | 1 |  |  |  |  | 10 |  | 
| 46 | 1 |  |  | 1 |  | 7974 | use experimental qw/switch/; | 
|  | 1 |  |  |  |  | 3267 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | =head1 METHODS | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | =head2 WebService::IFConfig::Client->new( 'run' =E<gt> $boolean , 'server' =E<gt> $my_server ); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | Constructor to create an new client. Default values are | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Argument  Default                     Meaning | 
| 55 |  |  |  |  |  |  | --------  -------                     ------- | 
| 56 |  |  |  |  |  |  | run       1                           1 means run immediately. | 
| 57 |  |  |  |  |  |  | 0 means do not run until a request is made. | 
| 58 |  |  |  |  |  |  | server    https://ifconfig.co/json    IPD Server, but you can run your own and provide it here. | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | =cut | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | # Whether to run immediately at construction. Default true. | 
| 63 |  |  |  |  |  |  | has 'run' => ( | 
| 64 |  |  |  |  |  |  | is      => 'ro', | 
| 65 |  |  |  |  |  |  | isa     => 'Bool', | 
| 66 |  |  |  |  |  |  | default => 1, | 
| 67 |  |  |  |  |  |  | reader  => '_run_at_construction' | 
| 68 |  |  |  |  |  |  | ); | 
| 69 |  |  |  |  |  |  |  | 
| 70 |  |  |  |  |  |  | =head2 get_server | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | Get the URL this client is configured to talk to | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | =cut | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | =head2 set_server | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | Set the URL this client is configured to talk to | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | =cut | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | has 'server' => ( | 
| 83 |  |  |  |  |  |  | is      => 'ro', | 
| 84 |  |  |  |  |  |  | isa     => 'Str', | 
| 85 |  |  |  |  |  |  | default => 'https://ifconfig.co/json', | 
| 86 |  |  |  |  |  |  | reader  => 'get_server', | 
| 87 |  |  |  |  |  |  | writer  => 'set_server' | 
| 88 |  |  |  |  |  |  | ); | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | has '_json' => ( | 
| 91 |  |  |  |  |  |  | is      => 'ro', | 
| 92 |  |  |  |  |  |  | isa     => 'HashRef', | 
| 93 |  |  |  |  |  |  | default => sub { {} }, | 
| 94 |  |  |  |  |  |  | reader  => '_get_json', | 
| 95 |  |  |  |  |  |  | writer  => '_set_json' | 
| 96 |  |  |  |  |  |  | ); | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | =head2 get_raw_status | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | Get the HTTP Response Code of the latest request. Returns 0 if no request has been made. | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =cut | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | has '_raw_status' => ( | 
| 105 |  |  |  |  |  |  | is      => 'ro', | 
| 106 |  |  |  |  |  |  | isa     => 'Int', | 
| 107 |  |  |  |  |  |  | default => 0, | 
| 108 |  |  |  |  |  |  | reader  => 'get_raw_status', | 
| 109 |  |  |  |  |  |  | writer  => '_set_raw_status' | 
| 110 |  |  |  |  |  |  | ); | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | sub BUILD { | 
| 113 | 3 |  |  | 3 | 0 | 7697 | my $self = shift; | 
| 114 |  |  |  |  |  |  |  | 
| 115 | 3 | 100 |  |  |  | 189 | $self->request if $self->_run_at_construction; | 
| 116 |  |  |  |  |  |  | } | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head2 get_status | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | Returns 1 if the HTTP Response Code of the latest request was 200, or 0 if not. | 
| 121 |  |  |  |  |  |  | Returns undef if no request has been made. | 
| 122 |  |  |  |  |  |  |  | 
| 123 |  |  |  |  |  |  | =cut | 
| 124 |  |  |  |  |  |  |  | 
| 125 |  |  |  |  |  |  | sub get_status { | 
| 126 | 11 |  |  | 11 | 1 | 97 | my $self = shift; | 
| 127 | 11 |  |  |  |  | 30 | my $answer; | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 11 |  |  |  |  | 664 | given ( $self->get_raw_status ) { | 
| 130 | 11 |  |  |  |  | 53 | $answer = undef when 0; | 
| 131 | 11 |  |  |  |  | 44 | $answer = 1 when 200; | 
| 132 | 4 |  |  |  |  | 11 | default { $answer = 0 } | 
|  | 4 |  |  |  |  | 15 |  | 
| 133 |  |  |  |  |  |  | } | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 11 |  |  |  |  | 176 | return $answer; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | =head2 request | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | Makes a(nother) request from the server. | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | =cut | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | sub request { | 
| 145 | 3 |  |  | 3 | 1 | 25 | my $self   = shift; | 
| 146 | 3 |  |  |  |  | 80 | my $client = REST::Client->new(); | 
| 147 | 3 |  |  |  |  | 8737 | my $json   = JSON->new(); | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | # Reset | 
| 150 | 3 |  |  |  |  | 183 | $self->_set_json({}); | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 3 |  |  |  |  | 206 | $client->GET( $self->get_server ); | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 3 |  |  |  |  | 153903 | $self->_set_raw_status( $client->responseCode() ); | 
| 155 | 3 | 100 |  |  |  | 24 | $self->get_status() | 
| 156 |  |  |  |  |  |  | and $self->_set_json( $json->decode( $client->responseContent() ) ); | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | sub _request_if_not_ok { | 
| 160 | 5 |  |  | 5 |  | 16 | my $self = shift; | 
| 161 | 5 | 50 |  |  |  | 24 | $self->get_status() or $self->request(); | 
| 162 |  |  |  |  |  |  | } | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | sub _elements { | 
| 165 | 5 |  |  | 5 |  | 30 | my ( $self, $element ) = @_; | 
| 166 | 5 |  |  |  |  | 28 | $self->_request_if_not_ok(); | 
| 167 | 5 |  |  |  |  | 263 | return $self->_get_json->{$element}; | 
| 168 |  |  |  |  |  |  | } | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =head2 get_city | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | Get the City name. Forces a request if no valid data. | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | =cut | 
| 175 |  |  |  |  |  |  |  | 
| 176 | 1 |  |  | 1 | 1 | 7 | sub get_city       { return $_[0]->_elements('city'); } | 
| 177 |  |  |  |  |  |  |  | 
| 178 |  |  |  |  |  |  | =head2 get_country | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | Get the Country name. Forces a request if no valid data. | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =cut | 
| 183 |  |  |  |  |  |  |  | 
| 184 | 1 |  |  | 1 | 1 | 6 | sub get_country    { return $_[0]->_elements('country'); } | 
| 185 |  |  |  |  |  |  |  | 
| 186 |  |  |  |  |  |  | =head2 get_hostname | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | Get the Hostname. Forces a request if no valid data. | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | =cut | 
| 191 |  |  |  |  |  |  |  | 
| 192 | 1 |  |  | 1 | 1 | 8 | sub get_hostname   { return $_[0]->_elements('hostname'); } | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =head2 get_ip | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | Get the IP address. Forces a request if no valid data. | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | =cut | 
| 199 |  |  |  |  |  |  |  | 
| 200 | 1 |  |  | 1 | 1 | 5529 | sub get_ip         { return $_[0]->_elements('ip'); } | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | =head2 get_ip_decimal | 
| 203 |  |  |  |  |  |  |  | 
| 204 |  |  |  |  |  |  | Get a decimal representation of the IP. Forces a request if no valid data. | 
| 205 |  |  |  |  |  |  |  | 
| 206 |  |  |  |  |  |  | =cut | 
| 207 |  |  |  |  |  |  |  | 
| 208 | 1 |  |  | 1 | 1 | 8 | sub get_ip_decimal { return $_[0]->_elements('ip_decimal'); } | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | =head1 AUTHOR | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | Nic Doye E<lt>nic@worldofnic.orgE<gt> | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | =head1 BUGS | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | None. None whatsoever. (This is a lie). | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | =head1 LICENSE | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | Copyright 2017 Nicolas Doye | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | Licensed under the Apache License, Version 2.0 (the "License"); | 
| 223 |  |  |  |  |  |  | you may not use this file except in compliance with the License. | 
| 224 |  |  |  |  |  |  | You may obtain a copy of the License at | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | http://www.apache.org/licenses/LICENSE-2.0 | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | Unless required by applicable law or agreed to in writing, software | 
| 229 |  |  |  |  |  |  | distributed under the License is distributed on an "AS IS" BASIS, | 
| 230 |  |  |  |  |  |  | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
| 231 |  |  |  |  |  |  | See the License for the specific language governing permissions and | 
| 232 |  |  |  |  |  |  | limitations under the License. | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | =cut | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | 1; |