Sha256: a8307c326779c94de3b2957cdbfe2f5cb848dbc73500091fa7272fdd32922cc8

Contents?: true

Size: 1.59 KB

Versions: 22

Compression:

Stored size: 1.59 KB

Contents

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 query_url(query)
      "#{protocol}://geocode-maps.yandex.ru/1.x/?" + url_query_string(query)
    end
    
    def supported_protocols
      [:https]
    end

    private # ---------------------------------------------------------------

    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
      {
        :geocode => q,
        :format => "json",
        :plng => "#{query.language || configuration.language}", # supports ru, uk, be
        :key => configuration.api_key
      }.merge(super)
    end
  end
end

Version data entries

22 entries across 22 versions & 3 rubygems

Version Path
geocoder-1.4.4 lib/geocoder/lookups/yandex.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/geocoder-1.4.3/lib/geocoder/lookups/yandex.rb
geocoder-1.4.3 lib/geocoder/lookups/yandex.rb
geocoder-1.4.2 lib/geocoder/lookups/yandex.rb
geocoder-1.4.1 lib/geocoder/lookups/yandex.rb
geocoder-1.4.0 lib/geocoder/lookups/yandex.rb
geocoder-1.3.7 lib/geocoder/lookups/yandex.rb
geocoder-1.3.6 lib/geocoder/lookups/yandex.rb
geocoder-1.3.5 lib/geocoder/lookups/yandex.rb
broken-geocoder-1.3.6 lib/geocoder/lookups/yandex.rb
broken-geocoder-1.3.5 lib/geocoder/lookups/yandex.rb
broken-geocoder-1.3.4 lib/geocoder/lookups/yandex.rb
geocoder-1.3.4 lib/geocoder/lookups/yandex.rb
geocoder-1.3.3 lib/geocoder/lookups/yandex.rb
geocoder-1.3.2 lib/geocoder/lookups/yandex.rb
geocoder-1.3.1 lib/geocoder/lookups/yandex.rb
geocoder-1.3.0 lib/geocoder/lookups/yandex.rb
geocoder-1.2.14 lib/geocoder/lookups/yandex.rb
geocoder-1.2.13 lib/geocoder/lookups/yandex.rb
geocoder-1.2.12 lib/geocoder/lookups/yandex.rb