From a8128d63dcce968195afd5b830a032453444b995 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Mon, 31 Dec 2012 12:33:32 -0500
Subject: [PATCH] Properly detect error in MaxMind response.
---
lib/geocoder/lookups/maxmind.rb | 22 +++++++++++++++++++---
lib/geocoder/results/maxmind.rb | 2 +-
test/fixtures/maxmind_24_24_24_21 | 2 +-
test/fixtures/maxmind_24_24_24_22 | 2 +-
test/fixtures/maxmind_24_24_24_23 | 2 +-
test/fixtures/maxmind_24_24_24_24 | 2 +-
test/test_helper.rb | 2 +-
7 files changed, 25 insertions(+), 9 deletions(-)
diff --git a/lib/geocoder/lookups/maxmind.rb b/lib/geocoder/lookups/maxmind.rb
index 0be2495c..7e67fae8 100644
--- a/lib/geocoder/lookups/maxmind.rb
+++ b/lib/geocoder/lookups/maxmind.rb
@@ -15,9 +15,12 @@ module Geocoder::Lookup
private # ---------------------------------------------------------------
- def service_code
+ ##
+ # Return the name of the configured service, or raise an exception.
+ #
+ def configured_service!
if s = configuration[:service] and services.keys.include?(s)
- services[s]
+ return s
else
raise(
Geocoder::ConfigurationError,
@@ -28,6 +31,19 @@ module Geocoder::Lookup
end
end
+ def service_code
+ services[configured_service!]
+ end
+
+ def service_response_fields_count
+ Geocoder::Result::Maxmind.field_names[configured_service!].size
+ end
+
+ def data_contains_error?(parsed_data)
+ # if all fields given then there is an error
+ parsed_data.size == service_response_fields_count and !parsed_data.last.nil?
+ end
+
##
# Service names mapped to code used in URL.
#
@@ -45,7 +61,7 @@ module Geocoder::Lookup
return [reserved_result] if query.loopback_ip_address?
doc = fetch_data(query)
if doc and doc.is_a?(Array)
- if doc.last.nil?
+ if !data_contains_error?(doc)
return [doc]
elsif doc.last == "INVALID_LICENSE_KEY"
raise_error(Geocoder::InvalidApiKey) || warn("Invalid MaxMind API key.")
diff --git a/lib/geocoder/results/maxmind.rb b/lib/geocoder/results/maxmind.rb
index 2cc45beb..aa6c7aa9 100644
--- a/lib/geocoder/results/maxmind.rb
+++ b/lib/geocoder/results/maxmind.rb
@@ -71,7 +71,7 @@ module Geocoder::Result
#
def service_name
self.class.field_names.to_a.each do |n,f|
- return n if f.size == @data.size
+ return n if (@data.size..@data.size+1).include?(f.size)
end
nil
end
diff --git a/test/fixtures/maxmind_24_24_24_21 b/test/fixtures/maxmind_24_24_24_21
index 3fde7152..f4cd62e0 100644
--- a/test/fixtures/maxmind_24_24_24_21
+++ b/test/fixtures/maxmind_24_24_24_21
@@ -1 +1 @@
-US,
+US
diff --git a/test/fixtures/maxmind_24_24_24_22 b/test/fixtures/maxmind_24_24_24_22
index 426280ed..b9aee130 100644
--- a/test/fixtures/maxmind_24_24_24_22
+++ b/test/fixtures/maxmind_24_24_24_22
@@ -1 +1 @@
-US,NY,Jamaica,40.6915,-73.8057,
+US,NY,Jamaica,40.6915,-73.8057
diff --git a/test/fixtures/maxmind_24_24_24_23 b/test/fixtures/maxmind_24_24_24_23
index 8ffaf07f..29ecfb21 100644
--- a/test/fixtures/maxmind_24_24_24_23
+++ b/test/fixtures/maxmind_24_24_24_23
@@ -1 +1 @@
-US,NY,Jamaica,,40.6915,-73.8057,501,718,"Road Runner","Road Runner",
+US,NY,Jamaica,,40.6915,-73.8057,501,718,"Road Runner","Road Runner"
diff --git a/test/fixtures/maxmind_24_24_24_24 b/test/fixtures/maxmind_24_24_24_24
index ab20e037..14939030 100644
--- a/test/fixtures/maxmind_24_24_24_24
+++ b/test/fixtures/maxmind_24_24_24_24
@@ -1 +1 @@
-US,"United States",NY,"New York",Jamaica,40.6915,-73.8057,501,718,America/New_York,NA,,"Road Runner","Road Runner",rr.com,"AS11351 Road Runner HoldCo LLC",Cable/DSL,residential,779,99,37,76,,
+US,"United States",NY,"New York",Jamaica,40.6915,-73.8057,501,718,America/New_York,NA,,"Road Runner","Road Runner",rr.com,"AS11351 Road Runner HoldCo LLC",Cable/DSL,residential,779,99,37,76,35
diff --git a/test/test_helper.rb b/test/test_helper.rb
index b3aa68f2..b74017d4 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -239,7 +239,7 @@ end
class Test::Unit::TestCase
def setup
- Geocoder.configure(:maxmind => {:service => :omni})
+ Geocoder.configure(:maxmind => {:service => :city_isp_org})
end
def teardown
--
GitLab