Sha256: 08333f7c860a0ea1aedb92b2e770c9e62e715ab766c957a4dab804fa2f3fb465

Contents?: true

Size: 928 Bytes

Versions: 1

Compression:

Stored size: 928 Bytes

Contents

module OpenStreetMap
  class Client
    # Reverse geocoding generates an address from a latitude and longitude
    module Reverse
      def reverse(args = {})
        response = RestClient.get("#{args[:hostname] || BASE_URI}reverse?#{reverse_args_to_url(args)}", user_agent: args[:user_agent] || DEFAULT_USER_AGENT)
        sleep(REQUEST_TIMEOUT)
        args[:format] == 'xml' ? response.body : JSON.parse(response.body)
      rescue
        { 'errors' => 'Bad request' }
      end

      private

      def reverse_args_to_url(args, result = [])
        valid_reverse_args(args).each do |key, value|
          key = 'accept-language' if key == :accept_language
          result << "#{key}=#{value}"
        end
        result.join('&')
      end

      def valid_reverse_args(args)
        args.slice(:format, :lat, :lon, :zoom, :addressdetails, :extratags, :namedetails, :accept_language, :email)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
open_street_map-1.1 lib/open_street_map/client/reverse.rb