From 8fea0389e9ce01ac1bb494d039b33d400055df2e Mon Sep 17 00:00:00 2001
From: Scott Vesely <svesely@gmail.com>
Date: Thu, 17 Mar 2011 14:00:55 -0500
Subject: [PATCH] Add Google API key config option.

---
 lib/geocoder/configuration.rb  | 19 ++++++++++++-------
 lib/geocoder/lookups/google.rb |  3 ++-
 test/geocoder_test.rb          |  7 +++++++
 3 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/lib/geocoder/configuration.rb b/lib/geocoder/configuration.rb
index f8fdc4dc..38b3bdfa 100644
--- a/lib/geocoder/configuration.rb
+++ b/lib/geocoder/configuration.rb
@@ -17,6 +17,10 @@ module Geocoder
     def self.yahoo_appid; @@yahoo_appid; end
     def self.yahoo_appid=(obj); @@yahoo_appid = obj; end
 
+    # API key (if using Google geocoding service)
+    def self.google_api_key; @@google_api_key; end
+    def self.google_api_key=(obj); @@google_api_key = obj; end
+
     # cache object (must respond to #[], #[]=, and #keys
     def self.cache; @@cache; end
     def self.cache=(obj); @@cache = obj; end
@@ -31,10 +35,11 @@ module Geocoder
   end
 end
 
-Geocoder::Configuration.timeout      = 3
-Geocoder::Configuration.lookup       = :google
-Geocoder::Configuration.language     = :en
-Geocoder::Configuration.yahoo_appid  = nil
-Geocoder::Configuration.cache        = nil
-Geocoder::Configuration.cache_prefix = "geocoder:"
-Geocoder::Configuration.use_https    = false
+Geocoder::Configuration.timeout        = 3
+Geocoder::Configuration.lookup         = :google
+Geocoder::Configuration.language       = :en
+Geocoder::Configuration.yahoo_appid    = nil
+Geocoder::Configuration.google_api_key = nil
+Geocoder::Configuration.cache          = nil
+Geocoder::Configuration.cache_prefix   = "geocoder:"
+Geocoder::Configuration.use_https      = false
diff --git a/lib/geocoder/lookups/google.rb b/lib/geocoder/lookups/google.rb
index a641ce33..7584bf73 100644
--- a/lib/geocoder/lookups/google.rb
+++ b/lib/geocoder/lookups/google.rb
@@ -23,7 +23,8 @@ module Geocoder::Lookup
       params = {
         (reverse ? :latlng : :address) => query,
         :sensor => "false",
-        :language => Geocoder::Configuration.language
+        :language => Geocoder::Configuration.language,
+        :key => Geocoder::Configuration.google_api_key
       }
       "#{protocol}://maps.google.com/maps/api/geocode/json?" + hash_to_query(params)
     end
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index 89cd0ce2..10a45a46 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -232,6 +232,13 @@ class GeocoderTest < Test::Unit::TestCase
     assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
   end
 
+	def test_has_to_query_with_google_api_key
+		Geocoder::Configuration.google_api_key = "MY_KEY"
+    g = Geocoder::Lookup::Google.new
+    assert_match "key=MY_KEY", g.send(:query_url, {:a => 1, :b => 2})
+		Geocoder::Configuration.google_api_key = nil
+	end
+
 
   private # ------------------------------------------------------------------
 
-- 
GitLab