From 26643bee384e9f0e2ea6ac244ad3695b9a8a9ab3 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Sat, 5 Mar 2011 01:00:39 -0500
Subject: [PATCH] Don't require ActiveSupport's Object#to_query.

---
 lib/geocoder/lookups/base.rb   | 8 ++++++++
 lib/geocoder/lookups/google.rb | 2 +-
 lib/geocoder/lookups/yahoo.rb  | 2 +-
 test/geocoder_test.rb          | 5 +++++
 4 files changed, 15 insertions(+), 2 deletions(-)

diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb
index e4b792ef..92884cc9 100644
--- a/lib/geocoder/lookups/base.rb
+++ b/lib/geocoder/lookups/base.rb
@@ -88,6 +88,14 @@ module Geocoder
           Net::HTTP.get_response(URI.parse(url)).body
         end
       end
+
+      ##
+      # Simulate ActiveSupport's Object#to_query.
+      #
+      def hash_to_query(hash)
+        require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
+        hash.collect{ |p| p.map{ |i| CGI.escape i.to_s } * '=' }.sort * '&'
+      end
     end
   end
 end
diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb
index 61757a0b..52f87238 100644
--- a/lib/geocoder/lookups/google.rb
+++ b/lib/geocoder/lookups/google.rb
@@ -24,7 +24,7 @@ module Geocoder::Lookup
         (reverse ? :latlng : :address) => query,
         :sensor => "false"
       }
-      "http://maps.google.com/maps/api/geocode/json?" + params.to_query
+      "http://maps.google.com/maps/api/geocode/json?" + hash_to_query(params)
     end
   end
 end
diff --git a/lib/geocoder/lookups/yahoo.rb b/lib/geocoder/lookups/yahoo.rb
index 752e8760..efab5c84 100644
--- a/lib/geocoder/lookups/yahoo.rb
+++ b/lib/geocoder/lookups/yahoo.rb
@@ -22,7 +22,7 @@ module Geocoder::Lookup
         :gflags => "AC#{'R' if reverse}",
         :appid => Geocoder::Configuration.yahoo_appid
       }
-      "http://where.yahooapis.com/geocode?" + params.to_query
+      "http://where.yahooapis.com/geocode?" + hash_to_query(params)
     end
   end
 end
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index a0a9ab67..a625e79c 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -110,6 +110,11 @@ class GeocoderTest < Test::Unit::TestCase
     assert !Geocoder.send(:blank_query?, "a")
   end
 
+  def test_hash_to_query
+    g = Geocoder::Lookup::Google.new
+    assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
+  end
+
 
   private # ------------------------------------------------------------------
 
-- 
GitLab