Skip to content
Snippets Groups Projects
Commit 1c0dd3e4 authored by Marcin Olichwirowicz's avatar Marcin Olichwirowicz
Browse files

Cache only properly geocoded data for google lookup

parent c740bbb7
No related branches found
No related tags found
No related merge requests found
...@@ -192,6 +192,10 @@ module Geocoder ...@@ -192,6 +192,10 @@ module Geocoder
"http" + (configuration.use_https ? "s" : "") "http" + (configuration.use_https ? "s" : "")
end end
def valid_response(response)
(200..399).include?(response.code.to_i)
end
## ##
# Fetch a raw geocoding result (JSON string). # Fetch a raw geocoding result (JSON string).
# The result might or might not be cached. # The result might or might not be cached.
...@@ -204,7 +208,7 @@ module Geocoder ...@@ -204,7 +208,7 @@ module Geocoder
check_api_key_configuration!(query) check_api_key_configuration!(query)
response = make_api_request(query) response = make_api_request(query)
body = response.body body = response.body
if cache and (200..399).include?(response.code.to_i) if cache and valid_response(response)
cache[key] = body cache[key] = body
end end
@cache_hit = false @cache_hit = false
......
...@@ -16,6 +16,10 @@ module Geocoder::Lookup ...@@ -16,6 +16,10 @@ module Geocoder::Lookup
"#{protocol}://maps.googleapis.com/maps/api/geocode/json?" + url_query_string(query) "#{protocol}://maps.googleapis.com/maps/api/geocode/json?" + url_query_string(query)
end end
def valid_response(response)
super(response) && JSON.parse(response.body)["status"] == "OK"
end
private # --------------------------------------------------------------- private # ---------------------------------------------------------------
def results(query) def results(query)
......
...@@ -16,4 +16,20 @@ class CacheTest < Test::Unit::TestCase ...@@ -16,4 +16,20 @@ class CacheTest < Test::Unit::TestCase
"Lookup #{l} did not return cached result." "Lookup #{l} did not return cached result."
end end
end end
def test_google_over_query_limit_does_not_hit_cache
Geocoder.configure(:cache => {})
Geocoder.configure(:lookup => :google)
set_api_key!(:google)
Geocoder.configure(:always_raise => :all)
assert_raises Geocoder::OverQueryLimitError do
Geocoder.search("over limit")
end
lookup = Geocoder::Lookup.get(:google)
assert_equal false, lookup.instance_variable_get(:@cache_hit)
assert_raises Geocoder::OverQueryLimitError do
Geocoder.search("over limit")
end
assert_equal false, lookup.instance_variable_get(:@cache_hit)
end
end end
{
"status": "OVER_QUERY_LIMIT",
"results": [ ]
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment