Geo-Coder-Ovi

 view release on metacpan or  search on metacpan

lib/Geo/Coder/Ovi.pm  view on Meta::CPAN

use Carp qw(croak);
use Encode ();
use JSON;
use LWP::UserAgent;
use URI;

our $VERSION = '0.03';
$VERSION = eval $VERSION;

sub new {
    my ($class, %params) = @_;

    # These will be required at some point in the future.
    # croak qq('appid' and 'token' are required);
    #    unless ($params{appid} and $params{token}) {

    my $self = bless \ %params, $class;

    $self->ua(
        $params{ua} || LWP::UserAgent->new(agent => "$class/$VERSION")
    );

    if ($self->{debug}) {
        my $dump_sub = sub { $_[0]->dump(maxlength => 0); return };
        $self->ua->set_my_handler(request_send  => $dump_sub);
        $self->ua->set_my_handler(response_done => $dump_sub);
        $self->{compress} ||= 0;
    }
    if (exists $self->{compress} ? $self->{compress} : 1) {
        $self->ua->default_header(accept_encoding => 'gzip,deflate');
    }

    return $self;
}

sub response { $_[0]->{response} }

sub ua {
    my ($self, $ua) = @_;
    if ($ua) {
        croak q('ua' must be (or derived from) an LWP::UserAgent')
            unless ref $ua and $ua->isa(q(LWP::UserAgent));
        $self->{ua} = $ua;
    }
    return $self->{ua};
}

sub geocode {
    my ($self, @params) = @_;
    my %params = (@params % 2) ? (location => @params) : @params;
    my $raw = delete $params{raw};

    $_ = Encode::encode('utf-8', $_) for values %params;

    my $location = delete $params{location} or return;

    if (my $language = delete $params{language}) {
        $params{la} = $language;
    }

    my $uri = URI->new('http://where.desktop.mos.svc.ovi.com/NOSe/json');
    $uri->query_form(
        app_id => $self->{appid},
        token  => $self->{token},
        q      => $location,
        vi     => 'where',
        dv     => 'oviMapsAPI',
        lat    => 0,
        lon    => 0,
        %params,
    );

    my $res = $self->{response} = $self->ua->get($uri);
    return unless $res->is_success;

    # Change the content type of the response from 'application/json' so
    # HTTP::Message will decode the character encoding.
    $res->content_type('text/plain');

    my $data = eval { from_json($res->decoded_content) };
    return unless $data;
    return $data if $raw;

    my @results = @{ $data->{results} || [] };
    return wantarray ? @results : $results[0];
}


1;

__END__

=head1 NAME

Geo::Coder::Ovi - Geocode addresses with the Ovi Maps API

=head1 SYNOPSIS

    use Geo::Coder::Ovi;

    my $geocoder = Geo::Coder::Ovi->new(
        appid => 'Your App ID',
        token => 'Your token',
    );
    my $location = $geocoder->geocode(
        location => '102 Corporate Park Dr, Harrison, NY'
    );

=head1 DESCRIPTION

The C<Geo::Coder::Ovi> module provides an interface to the geocoding service
of the Ovi Maps API.

=head1 METHODS

=head2 new

    $geocoder = Geo::Coder::Ovi->new(
        appid => 'Your App ID',
        token => 'Your token',
        # debug => 1,



( run in 1.184 second using v1.01-cache-2.11-cpan-e1769b4cff6 )