| 1 | # ABSTRACT: Performs queries using Yandex Maps Company Search API | ||
| 2 | |||
| 3 | package Yandex::Geo; | ||
| 4 | $Yandex::Geo::VERSION = '0.07'; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | |||
| 9 | use LWP::UserAgent; | ||
| 10 | use URI; | ||
| 11 | use URI::Escape; | ||
| 12 | use Geo::JSON; | ||
| 13 | use Carp; | ||
| 14 | use utf8; | ||
| 15 | |||
| 16 | use Yandex::Geo::Company; | ||
| 17 | |||
| 18 | |||
| 19 | sub cities_bbox { | ||
| 20 | return { 'ROV' => '39.535873,47.146130~39.842460,47.356752' }; | ||
| 21 | } | ||
| 22 | |||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ( $class, %params ) = @_; | ||
| 26 | |||
| 27 | confess 'Empty apikey' | ||
| 28 | unless defined $params{apikey} && length $params{apikey}; | ||
| 29 | |||
| 30 | my $self = { | ||
| 31 | ua => LWP::UserAgent->new( timeout => 10 ), | ||
| 32 | base_url => 'https://search-maps.yandex.ru/v1/', | ||
| 33 | %params, | ||
| 34 | }; | ||
| 35 | |||
| 36 | bless $self, $class; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | sub get { | ||
| 41 | my ( $self, %params ) = @_; | ||
| 42 | |||
| 43 | my $url_params = $self->_build_params(%params); | ||
| 44 | my $url = URI->new( $self->{base_url} ); | ||
| 45 | $url->query_form($url_params); | ||
| 46 | |||
| 47 | my $response = $self->{ua}->get($url); | ||
| 48 | |||
| 49 | unless ( $response->is_success ) { | ||
| 50 | if ( int( $response->code / 100 ) == 4 ) { | ||
| 51 | die $response->status_line . ': ' . $response->decoded_content; | ||
| 52 | } | ||
| 53 | |||
| 54 | die $response->status_line; | ||
| 55 | } | ||
| 56 | |||
| 57 | Geo::JSON->from_json( $response->decoded_content ); | ||
| 58 | |||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub y_companies { | ||
| 63 | my ( $self, $text, %params ) = @_; | ||
| 64 | |||
| 65 | my $geo_json = $self->get( text => $text, %params ); | ||
| 66 | Yandex::Geo::Company::from_geo_json($geo_json); | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _build_params { | ||
| 70 | my ( $self, %params ) = @_; | ||
| 71 | |||
| 72 | # Set default parameters | ||
| 73 | |||
| 74 | confess 'No search text provided' | ||
| 75 | unless defined $params{text} && length $params{text}; | ||
| 76 | |||
| 77 | my $mess = { | ||
| 78 | apikey => $self->{apikey}, | ||
| 79 | text => $params{text}, | ||
| 80 | type => $self->{type} || $params{type} || 'biz', | ||
| 81 | lang => $self->{lang} || $params{lang} || 'ru_RU', | ||
| 82 | ll => $self->{ll} || $params{ll}, | ||
| 83 | spn => $self->{spn} || $params{spn}, | ||
| 84 | bbox => $self->{bbox} || $params{bbox}, | ||
| 85 | rspn => $self->{rspn} || $params{rspn} || 1, | ||
| 86 | results => $self->{results} || $params{results} || 25, | ||
| 87 | skip => $self->{skip} || $params{skip} | ||
| 88 | }; | ||
| 89 | |||
| 90 | $mess->{bbox} = $self->cities_bbox->{ $self->{only_city} } | ||
| 91 | if defined $self->{only_city}; | ||
| 92 | $mess->{bbox} = $self->cities_bbox->{ $params{only_city} } | ||
| 93 | if defined $params{only_city}; | ||
| 94 | |||
| 95 | # Remove empty params to not overload query | ||
| 96 | |||
| 97 | for my $k ( keys %$mess ) { | ||
| 98 | delete $mess->{$k} unless $mess->{$k}; | ||
| 99 | } | ||
| 100 | |||
| 101 | delete $mess->{only_city}; | ||
| 102 | |||
| 103 | return $mess; | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | 1; | ||
| 108 | |||
| 109 | __END__ |