lib/geocoder/lookups/base.rb in geocoder-1.2.8 vs lib/geocoder/lookups/base.rb in geocoder-1.2.9
- old
+ new
@@ -84,10 +84,19 @@
@cache = Cache.new(store, configuration.cache_prefix)
end
@cache
end
+ ##
+ # Array containing the protocols supported by the api.
+ # Should be set to [:http] if only HTTP is supported
+ # or [:https] if only HTTPS is supported.
+ #
+ def supported_protocols
+ [:http, :https]
+ end
+
private # -------------------------------------------------------------
##
# An object with configuration data for this particular lookup.
#
@@ -97,11 +106,10 @@
##
# Object used to make HTTP requests.
#
def http_client
- protocol = "http#{'s' if use_ssl?}"
proxy_name = "#{protocol}_proxy"
if proxy = configuration.send(proxy_name)
proxy_url = !!(proxy =~ /^#{protocol}/) ? proxy : protocol + '://' + proxy
begin
uri = URI.parse(proxy_url)
@@ -181,11 +189,11 @@
ActiveSupport::JSON.decode(data)
else
JSON.parse(data)
end
rescue => err
- raise_error(ResponseParseError.new(data)) or Geocoder.log(:warn, "Geocoding API's response was not valid JSON.")
+ raise_error(ResponseParseError.new(data)) or Geocoder.log(:warn, "Geocoding API's response was not valid JSON: #{data}")
end
##
# Parses a raw search result (returns hash or array).
#
@@ -263,10 +271,11 @@
# return the response object.
#
def make_api_request(query)
timeout(configuration.timeout) do
uri = URI.parse(query_url(query))
+ Geocoder.log(:debug, "Geocoder: HTTP request being made for #{uri.to_s}")
http_client.start(uri.host, uri.port, use_ssl: use_ssl?) do |client|
req = Net::HTTP::Get.new(uri.request_uri, configuration.http_headers)
if configuration.basic_auth[:user] and configuration.basic_auth[:password]
req.basic_auth(
configuration.basic_auth[:user],
@@ -277,10 +286,16 @@
end
end
end
def use_ssl?
- configuration.use_https
+ if supported_protocols == [:https]
+ true
+ elsif supported_protocols == [:http]
+ false
+ else
+ configuration.use_https
+ end
end
def check_api_key_configuration!(query)
key_parts = query.lookup.required_api_key_parts
if key_parts.size > Array(configuration.api_key).size