Permalink
Browse files

Properly detect error in MaxMind response.

  • Loading branch information...
1 parent 996545a commit a8128d63dcce968195afd5b830a032453444b995 @alexreisner committed Dec 31, 2012
View
22 lib/geocoder/lookups/maxmind.rb
@@ -15,9 +15,12 @@ def query_url(query)
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 @@ def service_code
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 @@ def results(query)
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.")
View
2 lib/geocoder/results/maxmind.rb
@@ -71,7 +71,7 @@ def self.field_names
#
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
View
2 test/fixtures/maxmind_24_24_24_21
@@ -1 +1 @@
-US,
+US
View
2 test/fixtures/maxmind_24_24_24_22
@@ -1 +1 @@
-US,NY,Jamaica,40.6915,-73.8057,
+US,NY,Jamaica,40.6915,-73.8057
View
2 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"
View
2 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
View
2 test/test_helper.rb
@@ -239,7 +239,7 @@ def initialize(name)
class Test::Unit::TestCase
def setup
- Geocoder.configure(:maxmind => {:service => :omni})
+ Geocoder.configure(:maxmind => {:service => :city_isp_org})
end
def teardown

0 comments on commit a8128d6

Please sign in to comment.