Acme-Free-API-Geodata-GeoIP

 view release on metacpan or  search on metacpan

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

    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;

}

sub _debuglog($self, $message) {
    if(!$self->{debug}) {
        return;
    }
    print STDERR $message, "\n";
}


1;
__END__



( run in 1.120 second using v1.01-cache-2.11-cpan-49f99fa48dc )