From 1443811f9159658bb207a29ad638fbf1b46a4695 Mon Sep 17 00:00:00 2001 From: Alex Reisner <alex@alexreisner.com> Date: Sun, 2 Sep 2012 15:05:54 -0400 Subject: [PATCH] Make Mapquest inherit from Nominatim. --- lib/geocoder/lookups/mapquest.rb | 25 ++---------- lib/geocoder/lookups/nominatim.rb | 12 ++++-- lib/geocoder/results/mapquest.rb | 63 +------------------------------ test/test_helper.rb | 2 +- 4 files changed, 15 insertions(+), 87 deletions(-) diff --git a/lib/geocoder/lookups/mapquest.rb b/lib/geocoder/lookups/mapquest.rb index 08bdaaeb..0b02807c 100644 --- a/lib/geocoder/lookups/mapquest.rb +++ b/lib/geocoder/lookups/mapquest.rb @@ -1,32 +1,15 @@ require 'geocoder/lookups/base' +require "geocoder/lookups/nominatim" require "geocoder/results/mapquest" module Geocoder::Lookup - class Mapquest < Base + class Mapquest < Nominatim private # --------------------------------------------------------------- - def results(query) - return [] unless doc = fetch_data(query) - doc.is_a?(Array) ? doc : [doc] - end - def query_url(query) - params = { - :format => "json", - :polygon => "1", - :addressdetails => "1", - :"accept-language" => Geocoder::Configuration.language - } - if (query.reverse_geocode?) - method = 'reverse' - lat,lon = query.coordinates - params[:lat] = lat - params[:lon] = lon - else - method = 'search' - params[:q] = query.sanitized_text - end + params = query_url_params(query) + method = query.reverse_geocode? ? "reverse" : "search" "http://open.mapquestapi.com/#{method}?" + hash_to_query(params) end end diff --git a/lib/geocoder/lookups/nominatim.rb b/lib/geocoder/lookups/nominatim.rb index 4ded59b1..3a077369 100644 --- a/lib/geocoder/lookups/nominatim.rb +++ b/lib/geocoder/lookups/nominatim.rb @@ -15,22 +15,26 @@ module Geocoder::Lookup doc.is_a?(Array) ? doc : [doc] end - def query_url(query) + def query_url_params(query) params = { :format => "json", :polygon => "1", :addressdetails => "1", :"accept-language" => Geocoder::Configuration.language } - if (query.reverse_geocode?) - method = 'reverse' + if query.reverse_geocode? lat,lon = query.coordinates params[:lat] = lat params[:lon] = lon else - method = 'search' params[:q] = query.sanitized_text end + params + end + + def query_url(query) + params = query_url_params(query) + method = query.reverse_geocode? ? "reverse" : "search" "http://nominatim.openstreetmap.org/#{method}?" + hash_to_query(params) end end diff --git a/lib/geocoder/results/mapquest.rb b/lib/geocoder/results/mapquest.rb index 8d2b6022..f977a6b5 100644 --- a/lib/geocoder/results/mapquest.rb +++ b/lib/geocoder/results/mapquest.rb @@ -1,66 +1,7 @@ require 'geocoder/results/base' +require 'geocoder/results/nominatim' module Geocoder::Result - class Mapquest < Base - - def house_number - @data['address']['house_number'] - end - - def address - @data['display_name'] - end - - def street - @data['address']['road'] - end - - def city - @data['address']['city'] - end - - def village - @data['address']['villiage'] - end - - def town - @data['address']['town'] - end - - def state - @data['address']['state'] - end - - alias_method :state_code, :state - - def postal_code - @data['address']['postcode'] - end - - def county - @data['address']['county'] - end - - def country - @data['address']['country'] - end - - def country_code - @data['address']['country_code'] - end - - def coordinates - [@data['lat'].to_f, @data['lon'].to_f] - end - - def self.response_attributes - %w[place_id boundingbox license polygonpoints display_name class type stadium suburb] - end - - response_attributes.each do |a| - define_method a do - @data[a] - end - end + class Mapquest < Nominatim end end diff --git a/test/test_helper.rb b/test/test_helper.rb index aa7d6d7a..fcdd1338 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -163,7 +163,7 @@ module Geocoder end end - class Mapquest < Base + class Mapquest < Nominatim private #----------------------------------------------------------------- def fetch_raw_data(query) raise TimeoutError if query.text == "timeout" -- GitLab