1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
require 'geocoder/lookups/base'
require "geocoder/results/yandex"
module Geocoder::Lookup
class Yandex < Base
def name
"Yandex"
end
def map_link_url(coordinates)
"http://maps.yandex.ru/?ll=#{coordinates.reverse.join(',')}"
end
def supported_protocols
[:https]
end
private # ---------------------------------------------------------------
def base_query_url(query)
"#{protocol}://geocode-maps.yandex.ru/1.x/?"
end
def results(query)
return [] unless doc = fetch_data(query)
if err = doc['error']
if err["status"] == 401 and err["message"] == "invalid key"
raise_error(Geocoder::InvalidApiKey) || Geocoder.log(:warn, "Invalid API key.")
else
Geocoder.log(:warn, "Yandex Geocoding API error: #{err['status']} (#{err['message']}).")
end
return []
end
if doc = doc['response']['GeoObjectCollection']
meta = doc['metaDataProperty']['GeocoderResponseMetaData']
return meta['found'].to_i > 0 ? doc['featureMember'] : []
else
Geocoder.log(:warn, "Yandex Geocoding API error: unexpected response format.")
return []
end
end
def query_url_params(query)
if query.reverse_geocode?
q = query.coordinates.reverse.join(",")
else
q = query.sanitized_text
end
params = {
:geocode => q,
:format => "json",
:plng => "#{query.language || configuration.language}", # supports ru, uk, be
:key => configuration.api_key
}
unless (bounds = query.options[:bounds]).nil?
params[:bbox] = bounds.map{ |point| "%f,%f" % point }.join('~')
end
params.merge(super)
end
end
end
|