Sha256: 5a343e7553433c4711e6650b890689aaa674f10b48e764a83d7c23bdea69e5cf

Contents?: true

Size: 1.7 KB

Versions: 11

Compression:

Stored size: 1.7 KB

Contents

require 'cgi'
require 'geocoder/lookups/base'
require "geocoder/results/mapquest"

module Geocoder::Lookup
  class Mapquest < Base

    def name
      "Mapquest"
    end

    def required_api_key_parts
      ["key"]
    end

    def query_url(query)
      domain = configuration[:open] ? "open" : "www"
      version = configuration[:version] || 1
      url = "#{protocol}://#{domain}.mapquestapi.com/geocoding/v#{version}/#{search_type(query)}?"
      url + url_query_string(query)
    end

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

    def search_type(query)
      query.reverse_geocode? ? "reverse" : "address"
    end

    def query_url_params(query)
      params = { :location => query.sanitized_text }
      if key = configuration.api_key
        params[:key] = CGI.unescape(key)
      end
      params.merge(super)
    end

    # http://www.mapquestapi.com/geocoding/status_codes.html
    # http://open.mapquestapi.com/geocoding/status_codes.html
    def results(query)
      return [] unless doc = fetch_data(query)
      return doc["results"][0]['locations'] if doc['info']['statuscode'] == 0 # A successful geocode call

      messages = doc['info']['messages'].join

      case doc['info']['statuscode']
      when 400 # Error with input
        raise_error(Geocoder::InvalidRequest, messages) ||
          warn("Mapquest Geocoding API error: #{messages}")
      when 403 # Key related error
        raise_error(Geocoder::InvalidApiKey, messages) ||
          warn("Mapquest Geocoding API error: #{messages}")
      when 500 # Unknown error
        raise_error(Geocoder::Error, messages) ||
          warn("Mapquest Geocoding API error: #{messages}")
      end
      []
    end

  end
end

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
geocoder-1.2.7 lib/geocoder/lookups/mapquest.rb
geocoder-kb-1.2.6 lib/geocoder/lookups/mapquest.rb
geocoder-1.2.6 lib/geocoder/lookups/mapquest.rb
bw-geocoder-1.2.6 lib/geocoder/lookups/mapquest.rb
bw-geocoder-1.2.5 lib/geocoder/lookups/mapquest.rb
geocoder-1.2.5 lib/geocoder/lookups/mapquest.rb
geocoder-1.2.4 lib/geocoder/lookups/mapquest.rb
geocoder-1.2.3 lib/geocoder/lookups/mapquest.rb
geocoder-1.2.2 lib/geocoder/lookups/mapquest.rb
geocoder-1.2.1 lib/geocoder/lookups/mapquest.rb
geocoder-1.2.0 lib/geocoder/lookups/mapquest.rb