From 8164a93136dbfaadd703c4668e15705e11c7e48d Mon Sep 17 00:00:00 2001
From: Scott Vesely <svesely@gmail.com>
Date: Thu, 17 Mar 2011 13:16:24 -0500
Subject: [PATCH] Add :use_https config option.

---
 lib/geocoder/configuration.rb  |  5 +++++
 lib/geocoder/lookups/base.rb   |  8 ++++++++
 lib/geocoder/lookups/google.rb |  2 +-
 test/geocoder_test.rb          | 12 ++++++++++++
 4 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/lib/geocoder/configuration.rb b/lib/geocoder/configuration.rb
index 16accae0..f8fdc4dc 100644
--- a/lib/geocoder/configuration.rb
+++ b/lib/geocoder/configuration.rb
@@ -24,6 +24,10 @@ module Geocoder
     # cache object (must respond to #[], #[]=, and #keys
     def self.cache_prefix; @@cache_prefix; end
     def self.cache_prefix=(obj); @@cache_prefix = obj; end
+
+    # use HTTPS for lookup requests? (true or false)
+    def self.use_https; @@use_https; end
+    def self.use_https=(obj); @@use_https = obj; end
   end
 end
 
@@ -33,3 +37,4 @@ Geocoder::Configuration.language     = :en
 Geocoder::Configuration.yahoo_appid  = nil
 Geocoder::Configuration.cache        = nil
 Geocoder::Configuration.cache_prefix = "geocoder:"
+Geocoder::Configuration.use_https    = false
diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb
index c3a7bb0f..74281f1c 100644
--- a/lib/geocoder/lookups/base.rb
+++ b/lib/geocoder/lookups/base.rb
@@ -78,6 +78,14 @@ module Geocoder
         end
       end
 
+      ##
+      # Protocol to use for communication with geocoding services.
+      # Set in configuration but not available for every service.
+      #
+      def protocol
+        "http" + (Geocoder::Configuration.use_https ? "s" : "")
+      end
+
       ##
       # Fetches a raw search result (JSON string).
       #
diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb
index 3f7144cf..a641ce33 100644
--- a/lib/geocoder/lookups/google.rb
+++ b/lib/geocoder/lookups/google.rb
@@ -25,7 +25,7 @@ module Geocoder::Lookup
         :sensor => "false",
         :language => Geocoder::Configuration.language
       }
-      "http://maps.google.com/maps/api/geocode/json?" + hash_to_query(params)
+      "#{protocol}://maps.google.com/maps/api/geocode/json?" + hash_to_query(params)
     end
   end
 end
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index f55b4265..89cd0ce2 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -4,6 +4,7 @@ class GeocoderTest < Test::Unit::TestCase
 
   def setup
     Geocoder::Configuration.lookup = :google
+    Geocoder::Configuration.use_https = false
   end
 
 
@@ -53,6 +54,17 @@ class GeocoderTest < Test::Unit::TestCase
     $VERBOSE = orig
   end
 
+  def test_uses_https_for_secure_query
+    Geocoder::Configuration.use_https = true
+    g = Geocoder::Lookup::Google.new
+    assert_match /^https:/, g.send(:query_url, {:a => 1, :b => 2})
+  end
+
+  def test_uses_http_by_default
+    g = Geocoder::Lookup::Google.new
+    assert_match /^http:/, g.send(:query_url, {:a => 1, :b => 2})
+  end
+
   def test_distance_to_returns_float
     v = Venue.new(*venue_params(:msg))
     v.latitude, v.longitude = [40.750354, -73.993371]
-- 
GitLab