diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index 28ad577b25df6517557e12c49abe9f1efd1a1a8f..4b8027ec18afb6a6bc3aeb11d9ec7ce0d53db570 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -171,15 +171,19 @@ module Geocoder "(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 + end + ## # Parses a raw search result (returns hash or array). # def parse_raw_data(raw_data) - if defined?(ActiveSupport::JSON) - ActiveSupport::JSON.decode(raw_data) - else - JSON.parse(raw_data) - end + parse_json(raw_data) rescue warn "Geocoding API's response was not valid JSON." end @@ -192,6 +196,10 @@ module Geocoder "http" + (configuration.use_https ? "s" : "") end + def valid_response(response) + (200..399).include?(response.code.to_i) + end + ## # Fetch a raw geocoding result (JSON string). # The result might or might not be cached. @@ -204,7 +212,7 @@ module Geocoder check_api_key_configuration!(query) response = make_api_request(query) body = response.body - if cache and (200..399).include?(response.code.to_i) + if cache and valid_response(response) cache[key] = body end @cache_hit = false diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb index c16bf85f8d23e913acdc10f7cb17a4cdc7e5172e..4daeb977f3f0aa8cd71c281e2a9e82517f5a19af 100644 --- a/lib/geocoder/lookups/google.rb +++ b/lib/geocoder/lookups/google.rb @@ -16,6 +16,10 @@ module Geocoder::Lookup "#{protocol}://maps.googleapis.com/maps/api/geocode/json?" + url_query_string(query) end + def valid_response(response) + super(response) && parse_json(response.body)["status"] == "OK" + end + private # --------------------------------------------------------------- def results(query) diff --git a/test/cache_test.rb b/test/cache_test.rb index edd45bbfae62464244570da05ad58195b5ef1e31..0e45cfcff497d8fd727082cf01611cbc8b9933bb 100644 --- a/test/cache_test.rb +++ b/test/cache_test.rb @@ -16,4 +16,20 @@ class CacheTest < Test::Unit::TestCase "Lookup #{l} did not return cached result." 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 diff --git a/test/fixtures/google_over_limit b/test/fixtures/google_over_limit new file mode 100644 index 0000000000000000000000000000000000000000..353ccf5c0023af2fbbc51ccb25f4fd175648b8f4 --- /dev/null +++ b/test/fixtures/google_over_limit @@ -0,0 +1,4 @@ +{ + "status": "OVER_QUERY_LIMIT", + "results": [ ] +}