lib/geocoder/lookups/base.rb in geocoder-1.1.1 vs lib/geocoder/lookups/base.rb in geocoder-1.1.2

- old
+ new

@@ -31,11 +31,15 @@ reverse = true query = query.join(',') else reverse = false end - results(query, reverse).map{ |r| result_class.new(r) } + results(query, reverse).map{ |r| + result = result_class.new(r) + result.cache_hit = @cache_hit if cache + result + } end ## # Return the URL for a map of the given coordinates. # @@ -93,44 +97,40 @@ ## # Raise exception if configuration specifies it should be raised. # Return false if exception not raised. # def raise_error(error, message = nil) - if Geocoder::Configuration.always_raise.include?(error.class) + if Geocoder::Configuration.always_raise.include?( error.is_a?(Class) ? error : error.class ) raise error, message else false end end ## # Returns a parsed search result (Ruby hash). # def fetch_data(query, reverse = false) - begin - parse_raw_data fetch_raw_data(query, reverse) - 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)." - end + parse_raw_data fetch_raw_data(query, reverse) + 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)." end ## # Parses a raw search result (returns hash or array). # def parse_raw_data(raw_data) - begin - if defined?(ActiveSupport::JSON) - ActiveSupport::JSON.decode(raw_data) - else - JSON.parse(raw_data) - end - rescue - warn "Geocoding API's response was not valid JSON." + if defined?(ActiveSupport::JSON) + ActiveSupport::JSON.decode(raw_data) + else + JSON.parse(raw_data) end + rescue + warn "Geocoding API's response was not valid JSON." end ## # Protocol to use for communication with geocoding services. # Set in configuration but not available for every service. @@ -144,17 +144,20 @@ # def fetch_raw_data(query, reverse = false) timeout(Geocoder::Configuration.timeout) do url = query_url(query, reverse) uri = URI.parse(url) - unless cache and body = cache[url] + if cache and body = cache[url] + @cache_hit = true + else client = http_client.new(uri.host, uri.port) client.use_ssl = true if Geocoder::Configuration.use_https - response = client.get(uri.request_uri) + response = client.get(uri.request_uri, Geocoder::Configuration.http_headers) body = response.body if cache and (200..399).include?(response.code.to_i) cache[url] = body end + @cache_hit = false end body end end