lib/geocoder/lookups/base.rb in geocoder-1.2.7 vs lib/geocoder/lookups/base.rb in geocoder-1.2.8
- old
+ new
@@ -166,26 +166,26 @@
# Returns a parsed search result (Ruby hash).
#
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."
+ raise_error(err) or Geocoder.log(:warn, "Geocoding API connection cannot be established.")
rescue Errno::ECONNREFUSED => err
- raise_error(err) or warn "Geocoding API connection refused."
+ raise_error(err) or Geocoder.log(:warn, "Geocoding API connection refused.")
rescue TimeoutError => err
- raise_error(err) or warn "Geocoding API not responding fast enough " +
- "(use Geocoder.configure(:timeout => ...) to set limit)."
+ raise_error(err) or Geocoder.log(:warn, "Geocoding API not responding fast enough " +
+ "(use Geocoder.configure(:timeout => ...) to set limit).")
end
def parse_json(data)
if defined?(ActiveSupport::JSON)
ActiveSupport::JSON.decode(data)
else
JSON.parse(data)
end
rescue => err
- raise_error(ResponseParseError.new(data)) or 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.")
end
##
# Parses a raw search result (returns hash or array).
#
@@ -240,36 +240,41 @@
end
def check_response_for_errors!(response)
if response.code.to_i == 400
raise_error(Geocoder::InvalidRequest) ||
- warn("Geocoding API error: 400 Bad Request")
+ Geocoder.log(:warn, "Geocoding API error: 400 Bad Request")
elsif response.code.to_i == 401
raise_error(Geocoder::RequestDenied) ||
- warn("Geocoding API error: 401 Unauthorized")
+ Geocoder.log(:warn, "Geocoding API error: 401 Unauthorized")
elsif response.code.to_i == 402
raise_error(Geocoder::OverQueryLimitError) ||
- warn("Geocoding API error: 402 Payment Required")
+ Geocoder.log(:warn, "Geocoding API error: 402 Payment Required")
elsif response.code.to_i == 429
raise_error(Geocoder::OverQueryLimitError) ||
- warn("Geocoding API error: 429 Too Many Requests")
+ Geocoder.log(:warn, "Geocoding API error: 429 Too Many Requests")
+ elsif response.code.to_i == 503
+ raise_error(Geocoder::ServiceUnavailable) ||
+ Geocoder.log(:warn, "Geocoding API error: 503 Service Unavailable")
end
end
##
# Make an HTTP(S) request to a geocoding API and
# return the response object.
#
def make_api_request(query)
timeout(configuration.timeout) do
uri = URI.parse(query_url(query))
- args = [uri.host, uri.port]
- args = args.push(uri.user, uri.password) unless uri.user.nil? or uri.password.nil?
- opts = {}
- opts[:use_ssl] = use_ssl?
-
- http_client.start(*args, opts) do |client|
- client.get(uri.request_uri, configuration.http_headers)
+ 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],
+ configuration.basic_auth[:password]
+ )
+ end
+ client.request(req)
end
end
end
def use_ssl?