Skip to content
Snippets Groups Projects
Commit a8128d63 authored by Alex Reisner's avatar Alex Reisner
Browse files

Properly detect error in MaxMind response.

parent 996545ab
No related branches found
No related tags found
No related merge requests found
......@@ -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.")
......
......@@ -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
......
US,
US
US,NY,Jamaica,40.6915,-73.8057,
US,NY,Jamaica,40.6915,-73.8057
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"
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
......@@ -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
......
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