diff --git a/lib/geocoder/configuration.rb b/lib/geocoder/configuration.rb index 16accae0bbf05a046d27d1b30645d059928af18f..f8fdc4dcbd24088effca111e12605fa6bab3523f 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 c3a7bb0f8442f3a2c2982c4e1b25616246027870..74281f1cf80f4885045b1e357451624e87cef6f2 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 3f7144cf8ccefa5de5e5c195f7a0ce15ffa5b4a9..a641ce339cf6ecc1a818dae4b46c0389409006f0 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 f55b426517c3f25271a992ecaa790f87d23c365f..89cd0ce25ade2330c4eaf35e081b48d83e570dc7 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]