From 864fff2914bba35b01132b5f06e835b6afb16425 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Mon, 2 May 2011 19:19:12 -0400
Subject: [PATCH] Get rid of use_proxy configuration setting.

Instead use a proxy if http_proxy or https_proxy is set. Also revise
code style to be more consistent with rest of library.
---
 lib/geocoder/configuration.rb |  7 +++++--
 lib/geocoder/lookups/base.rb  | 24 +++++++++++++-----------
 test/geocoder_test.rb         | 30 ++++++++++--------------------
 3 files changed, 28 insertions(+), 33 deletions(-)

diff --git a/lib/geocoder/configuration.rb b/lib/geocoder/configuration.rb
index 7ee2776b..743920ba 100644
--- a/lib/geocoder/configuration.rb
+++ b/lib/geocoder/configuration.rb
@@ -15,8 +15,11 @@ module Geocoder
         # use HTTPS for lookup requests? (if supported)
         [:use_https, false],
 
-        # use Proxy when http_proxy / https_proxy is set in ENV
-        [:use_proxy, true],
+        # URL of HTTP proxy
+        [:http_proxy, nil],
+
+        # URL of HTTPS proxy
+        [:https_proxy, nil],
 
         # API key for geocoding service
         [:api_key, nil],
diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb
index d3687f89..a6cf96d2 100644
--- a/lib/geocoder/lookups/base.rb
+++ b/lib/geocoder/lookups/base.rb
@@ -55,20 +55,22 @@ module Geocoder
       private # -------------------------------------------------------------
 
       ##
-      # Object used to fetch requests
+      # Object used to make HTTP requests.
       #
       def http_client
-        proxy_url = ENV[Geocoder::Configuration.use_https ? 'https_proxy' : 'http_proxy']
-        return Net::HTTP unless Geocoder::Configuration.use_proxy && proxy_url
-
-        begin
-          uri = URI.parse(proxy_url)
-        rescue URI::InvalidURIError
-          raise ConfigurationError, "The proxy URL in environment (" +
-            "#{Geocoder::Configuration.use_https ? 'https_proxy' : 'http_proxy'} => #{proxy_url}" +
-            ") was not parsed correctly by URI::Parse"
+        secure = Geocoder::Configuration.use_https
+        proxy_name = "http#{'s' if secure}_proxy"
+        if proxy_url = Geocoder::Configuration.send(proxy_name)
+          begin
+            uri = URI.parse(proxy_url)
+          rescue URI::InvalidURIError
+            raise ConfigurationError,
+              "Error parsing HTTP#{'S' if secure} proxy URL: '#{proxy_url}'"
+          end
+          Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
+        else
+          Net::HTTP
         end
-        Net::HTTP::Proxy(uri.host, uri.port, uri.user, uri.password)
       end
 
       ##
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index e3cef93e..416867d9 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -20,34 +20,24 @@ class GeocoderTest < Test::Unit::TestCase
 
   # --- sanity checks ---
 
-  def test_uses_proxy_by_default
-    lookup = Geocoder::Lookup::Base.new
-
-    ENV.delete 'http_proxy'
-    ENV.delete 'https_proxy'
-    assert ! lookup.send(:http_client).proxy_class?
-
-    ENV['http_proxy'] = 'http://localhost'
-    assert_equal 'localhost', lookup.send(:http_client).proxy_address
+  def test_uses_proxy_when_specified
+    Geocoder::Configuration.http_proxy = 'http://localhost'
+    lookup = Geocoder::Lookup::Google.new
+    assert lookup.send(:http_client).proxy_class?
+  end
 
-    Geocoder::Configuration.use_https = true
-    ENV['https_proxy'] = 'https://localhost-ssl'
-    assert_equal 'localhost-ssl', lookup.send(:http_client).proxy_address
+  def test_doesnt_use_proxy_when_not_specified
+    lookup = Geocoder::Lookup::Google.new
+    assert !lookup.send(:http_client).proxy_class?
   end
 
   def test_exception_raised_on_bad_proxy_url
-    ENV['http_proxy'] = ' \\_O< Quack Quack'
+    Geocoder::Configuration.http_proxy = ' \\_O< Quack Quack'
     assert_raise Geocoder::ConfigurationError do
-      Geocoder::Lookup::Base.new.send(:http_client)
+      Geocoder::Lookup::Google.new.send(:http_client)
     end
   end
 
-  def test_uses_direct_connection_when_forced
-    Geocoder::Configuration.use_proxy = false
-    ENV['http_proxy'] = 'http://localhost:8080'
-    assert ! Geocoder::Lookup::Base.new.send(:http_client).proxy_class?
-  end
-
   def test_uses_https_for_secure_query
     Geocoder::Configuration.use_https = true
     g = Geocoder::Lookup::Google.new
-- 
GitLab