Sha256: 16b676a81289f83a364099c84937de5536d681d1043d8961bd783050e90943d4

Contents?: true

Size: 1.47 KB

Versions: 7

Compression:

Stored size: 1.47 KB

Contents

require 'geocoder/lookups/base'
require 'geocoder/results/amazon_location_service'

module Geocoder::Lookup
  class AmazonLocationService < Base
    def results(query)
      params = query.options.dup

      # index_name is required
      # Aws::ParamValidator raises ArgumentError on missing required keys
      params.merge!(index_name: configuration[:index_name])

      # Aws::ParamValidator raises ArgumentError on unexpected keys
      params.delete(:lookup) 
      
      resp = if query.reverse_geocode?
        client.search_place_index_for_position(params.merge(position: query.coordinates.reverse))
      else
        client.search_place_index_for_text(params.merge(text: query.text))
      end
      
      resp.results.map(&:place)
    end

    private

    def client
      return @client if @client
      require_sdk
      keys = configuration.api_key
      if keys
        @client = Aws::LocationService::Client.new(
          access_key_id: keys[:access_key_id],
          secret_access_key: keys[:secret_access_key],
        )
      else
        @client = Aws::LocationService::Client.new
      end
    end

    def require_sdk
      begin
        require 'aws-sdk-locationservice'
      rescue LoadError
        raise_error(Geocoder::ConfigurationError) ||
          Geocoder.log(
            :error,
            "Couldn't load the Amazon Location Service SDK. " +
            "Install it with: gem install aws-sdk-locationservice -v '~> 1.4'"
          )
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
geocoder-1.8.1 lib/geocoder/lookups/amazon_location_service.rb
geocoder-1.8.0 lib/geocoder/lookups/amazon_location_service.rb
geocoder-1.7.5 lib/geocoder/lookups/amazon_location_service.rb
geocoder-1.7.4 lib/geocoder/lookups/amazon_location_service.rb
geocoder-1.7.3 lib/geocoder/lookups/amazon_location_service.rb
geocoder-1.7.2 lib/geocoder/lookups/amazon_location_service.rb
geocoder-1.7.1 lib/geocoder/lookups/amazon_location_service.rb