From 0b8f4d5d1ce02f90b51908d4a4ef4d13eee3d0a7 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Sat, 20 Apr 2013 12:34:55 -0400
Subject: [PATCH] Read MaxMind service name from config

instead of inferring from response length. This is less than ideal but
it fixes issue #396 and, given what appears to be MaxMind's inconsistent
response lengths, it may not be possible to do much better.
---
 lib/geocoder/results/maxmind.rb |  9 ++++-----
 test/services_test.rb           | 10 +++++-----
 2 files changed, 9 insertions(+), 10 deletions(-)

diff --git a/lib/geocoder/results/maxmind.rb b/lib/geocoder/results/maxmind.rb
index aa6c7aa9..6682bfc6 100644
--- a/lib/geocoder/results/maxmind.rb
+++ b/lib/geocoder/results/maxmind.rb
@@ -67,13 +67,12 @@ module Geocoder::Result
 
     ##
     # Name of the MaxMind service being used.
-    # Inferred from format of @data.
     #
     def service_name
-      self.class.field_names.to_a.each do |n,f|
-        return n if (@data.size..@data.size+1).include?(f.size)
-      end
-      nil
+      # it would be much better to infer this from the length of the @data
+      # array, but MaxMind seems to send inconsistent and wide-ranging response
+      # lengths (see https://github.com/alexreisner/geocoder/issues/396)
+      Geocoder.config.maxmind[:service]
     end
 
     def field_names
diff --git a/test/services_test.rb b/test/services_test.rb
index e2df65fa..fd0ac300 100644
--- a/test/services_test.rb
+++ b/test/services_test.rb
@@ -171,27 +171,27 @@ class ServicesTest < Test::Unit::TestCase
   end
 
   def test_maxmind_result_knows_country_service_name
-    Geocoder.configure(:ip_lookup => :maxmind)
+    Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :country})
     assert_equal :country, Geocoder.search("24.24.24.21").first.service_name
   end
 
   def test_maxmind_result_knows_city_service_name
-    Geocoder.configure(:ip_lookup => :maxmind)
+    Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city})
     assert_equal :city, Geocoder.search("24.24.24.22").first.service_name
   end
 
   def test_maxmind_result_knows_city_isp_org_service_name
-    Geocoder.configure(:ip_lookup => :maxmind)
+    Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :city_isp_org})
     assert_equal :city_isp_org, Geocoder.search("24.24.24.23").first.service_name
   end
 
   def test_maxmind_result_knows_omni_service_name
-    Geocoder.configure(:ip_lookup => :maxmind)
+    Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :omni})
     assert_equal :omni, Geocoder.search("24.24.24.24").first.service_name
   end
 
   def test_maxmind_special_result_components
-    Geocoder.configure(:ip_lookup => :maxmind)
+    Geocoder.configure(:ip_lookup => :maxmind, :maxmind => {:service => :omni})
     result = Geocoder.search("24.24.24.24").first
     assert_equal "Road Runner", result.isp_name
     assert_equal "Cable/DSL", result.netspeed
-- 
GitLab