Sha256: 8606b151f150d73da14381fdd4cc1618de6a26207cd138e0e64fcceeaf1e66a9

Contents?: true

Size: 1.64 KB

Versions: 17

Compression:

Stored size: 1.64 KB

Contents

require 'geocoder/lookups/base'
require 'geocoder/results/pelias'

module Geocoder::Lookup
  class Pelias < Base
    def name
      'Pelias'
    end

    def endpoint
      configuration[:endpoint] || 'localhost'
    end

    def query_url(query)
      query_type = query.reverse_geocode? ? 'reverse' : 'search'
      "#{protocol}://#{endpoint}/v1/#{query_type}?" + url_query_string(query)
    end

    def required_api_key_parts
      ['search-XXXX']
    end

    private

    def query_url_params(query)
      params = {
        api_key: configuration.api_key,
        size: 1
      }.merge(super)

      if query.reverse_geocode?
        lat,lon = query.coordinates
        params[:'point.lat'] = lat
        params[:'point.lon'] = lon
      else
        params[:text] = query.text
      end
      params
    end

    def results(query)
      return [] unless doc = fetch_data(query)

      # not all responses include a meta
      if doc['meta']
        error = doc.fetch('results', {}).fetch('error', {})
        message = error.fetch('type', 'Unknown Error') + ': ' + error.fetch('message', 'No message')
        log_message = 'Pelias Geocoding API error - ' + message
        case doc['meta']['status_code']
          when '200'
            # nothing to see here
          when '403'
            raise_error(Geocoder::RequestDenied, message) || Geocoder.log(:warn, log_message)
          when '429'
            raise_error(Geocoder::OverQueryLimitError, message) || Geocoder.log(:warn, log_message)
          else
            raise_error(Geocoder::Error, message) || Geocoder.log(:warn, log_message)
        end
      end

      doc['features'] || []
    end
  end
end

Version data entries

17 entries across 17 versions & 3 rubygems

Version Path
geocoder-1.4.9 lib/geocoder/lookups/pelias.rb
geocoder-1.4.8 lib/geocoder/lookups/pelias.rb
geocoder-1.4.7 lib/geocoder/lookups/pelias.rb
geocoder-1.4.6 lib/geocoder/lookups/pelias.rb
geocoder-1.4.5 lib/geocoder/lookups/pelias.rb
geocoder-1.4.4 lib/geocoder/lookups/pelias.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/geocoder-1.4.3/lib/geocoder/lookups/pelias.rb
geocoder-1.4.3 lib/geocoder/lookups/pelias.rb
geocoder-1.4.2 lib/geocoder/lookups/pelias.rb
geocoder-1.4.1 lib/geocoder/lookups/pelias.rb
geocoder-1.4.0 lib/geocoder/lookups/pelias.rb
geocoder-1.3.7 lib/geocoder/lookups/pelias.rb
geocoder-1.3.6 lib/geocoder/lookups/pelias.rb
geocoder-1.3.5 lib/geocoder/lookups/pelias.rb
broken-geocoder-1.3.6 lib/geocoder/lookups/pelias.rb
broken-geocoder-1.3.5 lib/geocoder/lookups/pelias.rb
broken-geocoder-1.3.4 lib/geocoder/lookups/pelias.rb