lib/geocoder/lookups/base.rb in geocoder-0.9.13 vs lib/geocoder/lookups/base.rb in geocoder-1.0.0
- old
+ new
@@ -1,6 +1,8 @@
require 'net/http'
+require 'uri'
+
unless defined?(ActiveSupport::JSON)
begin
require 'rubygems' # for Ruby 1.8
require 'json'
rescue LoadError
@@ -18,16 +20,11 @@
#
# 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, *args)
- # convert coordinates as separate arguments to an array
- if query.is_a?(Numeric) and args.first.is_a?(Numeric)
- warn "DEPRECATION WARNING: Instead of passing latitude/longitude as separate arguments to the search method, please pass an array: [#{query},#{args.first}]. The old argument format will not be supported in Geocoder v.1.0."
- query = [query, args.first]
- end
+ def search(query)
# if coordinates given as string, turn into array
query = query.split(/\s*,\s*/) if coordinates?(query)
if query.is_a?(Array)
@@ -37,14 +34,44 @@
reverse = false
end
results(query, reverse).map{ |r| result_class.new(r) }
end
+ ##
+ # Return the URL for a map of the given coordinates.
+ #
+ # Not necessarily implemented by all subclasses as only some lookups
+ # also provide maps.
+ #
+ def map_link_url(coordinates)
+ nil
+ end
+
private # -------------------------------------------------------------
##
+ # Object used to make HTTP requests.
+ #
+ def http_client
+ protocol = "http#{'s' if Geocoder::Configuration.use_https}"
+ proxy_name = "#{protocol}_proxy"
+ if proxy = Geocoder::Configuration.send(proxy_name)
+ proxy_url = protocol + '://' + proxy
+ begin
+ uri = URI.parse(proxy_url)
+ rescue URI::InvalidURIError
+ raise ConfigurationError,
+ "Error parsing #{protocol.upcase} proxy URL: '#{proxy_url}'"
+ end
+ Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
+ else
+ Net::HTTP
+ end
+ end
+
+ ##
# Geocoder::Result object or nil on timeout or other error.
#
def results(query, reverse = false)
fail
end
@@ -72,12 +99,12 @@
rescue SocketError
warn "Geocoding API connection cannot be established."
rescue TimeoutError
warn "Geocoding API not responding fast enough " +
"(see Geocoder::Configuration.timeout to set limit)."
+ end
end
- end
##
# Parses a raw search result (returns hash or array).
#
def parse_raw_data(raw_data)
@@ -105,11 +132,11 @@
#
def fetch_raw_data(query, reverse = false)
timeout(Geocoder::Configuration.timeout) do
url = query_url(query, reverse)
unless cache and response = cache[url]
- response = Net::HTTP.get_response(URI.parse(url)).body
+ response = http_client.get_response(URI.parse(url)).body
if cache
cache[url] = response
end
end
response
@@ -132,10 +159,10 @@
##
# Does the given string look like latitude/longitude coordinates?
#
def coordinates?(value)
- !!value.to_s.match(/^[0-9\.\-]+, *[0-9\.\-]+$/)
+ 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.