From eb737de56f900302f9e7ff4476c5fd137925b8a9 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Sat, 25 Aug 2012 00:01:27 -0400
Subject: [PATCH] Add :ip_lookup config option.

This is in preparation for adding Maxmind as an IP lookup.
---
 lib/geocoder.rb               | 9 ++++++++-
 lib/geocoder/cli.rb           | 5 +++--
 lib/geocoder/configuration.rb | 4 +++-
 test/test_helper.rb           | 4 ++--
 4 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/lib/geocoder.rb b/lib/geocoder.rb
index 76f5ec20..196cf75d 100644
--- a/lib/geocoder.rb
+++ b/lib/geocoder.rb
@@ -53,6 +53,13 @@ module Geocoder
     street_lookups + ip_lookups
   end
 
+  ##
+  # Array of valid Lookup names, excluding :test.
+  #
+  def valid_lookups_except_test
+    valid_lookups - [:test]
+  end
+
   ##
   # All street address lookups, default first.
   #
@@ -77,7 +84,7 @@ module Geocoder
   #
   def lookup(query)
     if ip_address?(query)
-      get_lookup(ip_lookups.first)
+      get_lookup(Configuration.ip_lookup || ip_lookups.first)
     else
       get_lookup(Configuration.lookup || street_lookups.first)
     end
diff --git a/lib/geocoder/cli.rb b/lib/geocoder/cli.rb
index 8822bf62..9a7fa13b 100644
--- a/lib/geocoder/cli.rb
+++ b/lib/geocoder/cli.rb
@@ -32,9 +32,10 @@ module Geocoder
           Geocoder::Configuration.http_proxy = proxy
         end
 
-        opts.on("-s <service>", Geocoder.street_lookups, "--service <service>",
-          "Geocoding service: #{Geocoder.street_lookups * ', '}") do |service|
+        opts.on("-s <service>", Geocoder.valid_lookups_except_test, "--service <service>",
+          "Geocoding service: #{Geocoder.valid_lookups_except_test * ', '}") do |service|
           Geocoder::Configuration.lookup = service.to_sym
+          Geocoder::Configuration.ip_lookup = service.to_sym
         end
 
         opts.on("-t <seconds>", "--timeout <seconds>",
diff --git a/lib/geocoder/configuration.rb b/lib/geocoder/configuration.rb
index 7bbe787d..c2df6d3e 100644
--- a/lib/geocoder/configuration.rb
+++ b/lib/geocoder/configuration.rb
@@ -40,6 +40,7 @@ module Geocoder
     OPTIONS = [
       :timeout,
       :lookup,
+      :ip_lookup,
       :language,
       :http_headers,
       :use_https,
@@ -61,7 +62,8 @@ module Geocoder
 
     def set_defaults
       @timeout      = 3           # geocoding service timeout (secs)
-      @lookup       = :google     # name of geocoding service (symbol)
+      @lookup       = :google     # name of street address geocoding service (symbol)
+      @ip_lookup    = :freegeoip  # name of IP address geocoding service (symbol)
       @language     = :en         # ISO-639 language code
       @http_headers = {}          # HTTP headers for lookup
       @use_https    = false       # use HTTPS for lookup requests? (if supported)
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 8ac6b3e0..a9cf367f 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -282,11 +282,11 @@ class Test::Unit::TestCase
   end
 
   def all_lookups_except_test
-    Geocoder.valid_lookups - [:test]
+    Geocoder.valid_lookups_except_test
   end
 
   def street_lookups
-    all_lookups - [:freegeoip]
+    Geocoder.street_lookups
   end
 
   def is_nan_coordinates?(coordinates)
-- 
GitLab