Acme-Free-API-Geodata-GeoIP

 view release on metacpan or  search on metacpan

lib/Acme/Free/API/Geodata/GeoIP.pm  view on Meta::CPAN


    return $self;
}

sub lookup($self, $ip) {
    my $url = "http://ip-api.com/json/" . $ip;

    my $content = $self->_fetchURL($url);

    my $ok = 0;
    my $decoded;
    eval {
        $decoded = decode_json($content);
        $ok = 1;
    };

    if(!$ok || !defined($decoded)) {
        $self->_debuglog("Failed to decode response. Not a JSON document?");
        $self->_debuglog(Dumper($decoded));
        return;
    }

    #$self->_debuglog(Dumper($decoded));

    return $decoded;
}



# internal helpers
# these are copied from CAVACs vast framework. But we don't want hundreds of dependencies in this example code, only a couple of functions
sub _fetchURL($self, $url) {
    $self->{agent}->get($url);

    if(!$self->{agent}->success()) {
        $self->_debuglog("Network error while fetching URL $url");
        return;
    }

    my $response = $self->{agent}->response();
    if(!defined($response)) {
        $self->_debuglog("Could not get agent response");
        return;
    }

    my $content = $response->decoded_content;
    if(!defined($content) || !length($content)) {
        $self->_debuglog("Could not get response content");
        return;
    }

    #$self->_debuglog(Dumper($content));

    return $content;

}



( run in 0.339 second using v1.01-cache-2.11-cpan-26ccb49234f )