From fcdd2d7a16c20da811e33817db3b3487cd09495d Mon Sep 17 00:00:00 2001
From: Andrew Stevens <astevens@tstmedia.com>
Date: Fri, 22 Apr 2011 17:14:05 -0500
Subject: [PATCH] adds Bing support

---
 README.rdoc           |  4 +++-
 lib/geocoder.rb       |  2 +-
 test/geocoder_test.rb | 17 +++++++++++++++++
 test/test_helper.rb   | 17 +++++++++++++++++
 4 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/README.rdoc b/README.rdoc
index 4c50d882..469662e8 100644
--- a/README.rdoc
+++ b/README.rdoc
@@ -227,6 +227,7 @@ Street address geocoding services currently supported (valid settings for the ab
 * Google: <tt>:google</tt>
 * Yahoo: <tt>:yahoo</tt>
 * Geocoder.ca: <tt>:geocoder_ca</tt> (US and Canada only)
+* Bing: <tt>:bing</tt>
 
 Note that the result objects returned by different geocoding services all implement the methods listed above. Beyond that, however, you must be familiar with your particular subclass of <tt>Geocoder::Result</tt> and the geocoding service's result structure:
 
@@ -235,7 +236,7 @@ Note that the result objects returned by different geocoding services all implem
 * Geocoder.ca: (???)
 * Yandex: http://api.yandex.ru/maps/geocoder/doc/desc/concepts/response_structure.xml
 * FreeGeoIP: http://github.com/fiorix/freegeoip/blob/master/README.rst
-
+* Bing: http://msdn.microsoft.com/en-us/library/ff701715.aspx
 === API Keys
 
 To use your Google API key or Yahoo app ID:
@@ -247,6 +248,7 @@ To obtain an API key:
 * Yahoo: https://developer.apps.yahoo.com/wsregapp (not required)
 * Google: http://code.google.com/apis/maps/signup.html (not required)
 * Yandex: http://api.yandex.ru/maps/intro/concepts/intro.xml#apikey (required)
+* Bing: http://www.bingmapsportal.com/
 
 === Timeout
 
diff --git a/lib/geocoder.rb b/lib/geocoder.rb
index ad090b66..a119b32e 100644
--- a/lib/geocoder.rb
+++ b/lib/geocoder.rb
@@ -76,7 +76,7 @@ module Geocoder
   # Array of valid Lookup names.
   #
   def valid_lookups
-    [:google, :yahoo, :geocoder_ca, :yandex, :freegeoip]
+    [:google, :yahoo, :geocoder_ca, :yandex, :freegeoip, :bing]
   end
 
   def version
diff --git a/test/geocoder_test.rb b/test/geocoder_test.rb
index 1d18853e..d3e69d84 100644
--- a/test/geocoder_test.rb
+++ b/test/geocoder_test.rb
@@ -403,6 +403,23 @@ class GeocoderTest < Test::Unit::TestCase
   end
 
 
+  # --- Bing ---
+
+  def test_bing_result_components
+    Geocoder::Configuration.lookup = :bing
+    result = Geocoder.search("Madison Square Garden, New York, NY").first
+    assert_equal "Madison Square Garden, NY", result.address
+    assert_equal "NY", result.state
+    assert_equal "New York", result.city
+  end
+
+  def test_bing_no_results
+    Geocoder::Configuration.lookup = :bing
+    results = Geocoder.search("no results")
+    assert_equal 0, results.length
+  end
+
+
   # --- search queries ---
 
   def test_hash_to_query
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 958fad52..d9a1463a 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -119,6 +119,23 @@ module Geocoder
         read_fixture "freegeoip_74_200_247_59.json"
       end
     end
+    
+    class Bing < Base
+      private #-----------------------------------------------------------------
+      def fetch_raw_data(query, reverse = false)
+        raise TimeoutError if query == "timeout"
+        if reverse
+          read_fixture "bing_reverse.json"
+        else
+          file = case query
+            when "no results";  :no_results
+            else                :madison_square_garden
+          end
+          read_fixture "bing_#{file}.json"
+        end
+      end
+    end
+    
   end
 end
 
-- 
GitLab