Sha256: bab3dc192152cdf49ab4aa5712eeec2fb39424abf6bb2f82993ab6dbb3ea51be

Contents?: true

Size: 1.36 KB

Versions: 15

Compression:

Stored size: 1.36 KB

Contents

require 'geocoder/lookups/base'
require 'geocoder/results/latlon'

module Geocoder::Lookup
  class Latlon < Base

    def name
      "LatLon.io"
    end

    def required_api_key_parts
      ["api_key"]
    end

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

    def base_query_url(query)
      "#{protocol}://latlon.io/api/v1/#{'reverse_' if query.reverse_geocode?}geocode?"
    end

    def results(query)
      return [] unless doc = fetch_data(query)
      if doc['error'].nil?
        [doc]
      # The API returned a 404 response, which indicates no results found
      elsif doc['error']['type'] == 'api_error'
        []
      elsif doc['error']['type'] == 'authentication_error'
        raise_error(Geocoder::InvalidApiKey) ||
          Geocoder.log(:warn, "LatLon.io service error: invalid API key.")
      else
        []
      end
    end

    def supported_protocols
      [:https]
    end

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

    def query_url_params(query)
      if query.reverse_geocode?
        {
          :token => configuration.api_key,
          :lat => query.coordinates[0],
          :lon => query.coordinates[1]
        }.merge(super)
      else
        {
          :token => configuration.api_key,
          :address => query.sanitized_text
        }.merge(super)
      end
    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
geocoder-1.8.3 lib/geocoder/lookups/latlon.rb
geocoder-1.8.2 lib/geocoder/lookups/latlon.rb
geocoder-1.8.1 lib/geocoder/lookups/latlon.rb
geocoder-1.8.0 lib/geocoder/lookups/latlon.rb
geocoder-1.7.5 lib/geocoder/lookups/latlon.rb
geocoder-1.7.4 lib/geocoder/lookups/latlon.rb
geocoder-1.7.3 lib/geocoder/lookups/latlon.rb
geocoder-1.7.2 lib/geocoder/lookups/latlon.rb
geocoder-1.7.1 lib/geocoder/lookups/latlon.rb
geocoder-1.7.0 lib/geocoder/lookups/latlon.rb
geocoder-1.6.7 lib/geocoder/lookups/latlon.rb
geocoder-1.6.6 lib/geocoder/lookups/latlon.rb
geocoder-1.6.5 lib/geocoder/lookups/latlon.rb
geocoder-1.6.4 lib/geocoder/lookups/latlon.rb
geocoder-1.6.3 lib/geocoder/lookups/latlon.rb