lib/geocoder/lookups/base.rb in geocoder-1.1.3 vs lib/geocoder/lookups/base.rb in geocoder-1.1.4
- old
+ new
@@ -11,32 +11,24 @@
end
end
module Geocoder
module Lookup
+
class Base
##
# Query the geocoding API and return a Geocoder::Result object.
# Returns +nil+ on timeout or error.
#
# Takes a search string (eg: "Mississippi Coast Coliseumf, Biloxi, MS",
# "205.128.54.202") for geocoding, or coordinates (latitude, longitude)
# for reverse geocoding. Returns an array of <tt>Geocoder::Result</tt>s.
#
- def search(query)
-
- # if coordinates given as string, turn into array
- query = query.split(/\s*,\s*/) if coordinates?(query)
-
- if query.is_a?(Array)
- reverse = true
- query = query.join(',')
- else
- reverse = false
- end
- results(query, reverse).map{ |r|
+ def search(query, options = {})
+ query = Geocoder::Query.new(query, options) unless query.is_a?(Geocoder::Query)
+ results(query).map{ |r|
result = result_class.new(r)
result.cache_hit = @cache_hit if cache
result
}
end
@@ -75,18 +67,28 @@
end
##
# Geocoder::Result object or nil on timeout or other error.
#
- def results(query, reverse = false)
+ def results(query)
fail
end
+ def query_url_params(query)
+ query.options[:params] || {}
+ end
+
+ def url_query_string(query)
+ hash_to_query(
+ query_url_params(query).reject{ |key,value| value.nil? }
+ )
+ end
+
##
# URL to use for querying the geocoding engine.
#
- def query_url(query, reverse = false)
+ def query_url(query)
fail
end
##
# Class of the result objects
@@ -109,12 +111,12 @@
end
##
# Returns a parsed search result (Ruby hash).
#
- def fetch_data(query, reverse = false)
- parse_raw_data fetch_raw_data(query, reverse)
+ def fetch_data(query)
+ parse_raw_data fetch_raw_data(query)
rescue SocketError => err
raise_error(err) or warn "Geocoding API connection cannot be established."
rescue TimeoutError => err
raise_error(err) or warn "Geocoding API not responding fast enough " +
"(see Geocoder::Configuration.timeout to set limit)."
@@ -142,13 +144,13 @@
end
##
# Fetches a raw search result (JSON string).
#
- def fetch_raw_data(query, reverse = false)
+ def fetch_raw_data(query)
timeout(Geocoder::Configuration.timeout) do
- url = query_url(query, reverse)
+ url = query_url(query)
uri = URI.parse(url)
if cache and body = cache[url]
@cache_hit = true
else
client = http_client.new(uri.host, uri.port)
@@ -167,23 +169,9 @@
##
# The working Cache object.
#
def cache
Geocoder.cache
- end
-
- ##
- # Is the given string a loopback IP address?
- #
- def loopback_address?(ip)
- !!(ip == "0.0.0.0" or ip.to_s.match(/^127/))
- end
-
- ##
- # Does the given string look like latitude/longitude coordinates?
- #
- def coordinates?(value)
- value.is_a?(String) and !!value.to_s.match(/^-?[0-9\.]+, *-?[0-9\.]+$/)
end
##
# Simulate ActiveSupport's Object#to_query.
# Removes any keys with nil value.